The FOR loop in this example creates a Mandelbrot image.
Copy codeThe Code is as follows:
Using System;
Namespace
{
Class Program
{
Public static void Main (string [] args)
{
Double realCoord, imagCoord;
Double realTemp, imagTemp, realTemp2, arg;
Int iterations;
For (imagCoord = 1.2; imagCoord> =-1.2; imagCoord-= 0.05)
{
For (realCoord =-0.6; realCoord <= 1.77; realCoord ++ = 0.03)
{
Iterations = 0;
RealTemp = realCoord;
ImagTemp = imagCoord;
Arg = (realCoord * realCoord) + (imagCoord * imagCoord );
While (arg <4) & (iterations <40 ))
{
RealTemp2 = (realTemp * realTemp)-(imagTemp * imagTemp)-realCoord;
ImagTemp = (2 * realTemp * imagTemp)-imagCoord;
RealTemp = realTemp2;
Arg = (realTemp * realTemp) + (imagTemp * imagTemp );
Iterations + = 1;
}
Switch (iterations % 4)
{
Case 0:
Console. Write (".");
Break;
Case 1:
Console. Write ("o ");
Break;
Case 2:
Console. Write ("0 ");
Break;
Case 3:
Console. Write ("@");
Break;
}
}
Console. Write ("n ");
}
Console. ReadKey ();
}
}
}
DO Statement of Loop Structure
The DO statement in the loop structure executes the corresponding code based on the test results of the Boolean value. The DO statement must be executed at least once.
Copy codeThe Code is as follows:
Using System;
Namespace
{
Class Program
{
Public static void Main (string [] args)
{
Double balance, interestRate, targetBalance;
Console. WriteLine ("What is your current balance? ");
Balance = Convert. ToDouble (Console. ReadLine ());
Console. WriteLine ("What is your current annual interest rate (in % )? ");
InterestRate = 1 + Convert. ToDouble (Console. ReadLine ()/100.0;
Console. WriteLine ("What balance wocould you like to have? ");
TargetBalance = Convert. ToDouble (Console. ReadLine ());
Int totalYears = 0;
Do
{
Balance * = interestRate;
++ TotalYears;
}
While (balance <targetBalance );
Console. WriteLine ("In {0} year {1} you'll have a balance of {2}.", totalYears, totalYears = 1? "": "S", balan00ce );
Console. ReadKey ();
}
}
}
Summary
When a DO statement is executed at least once regardless of whether the condition is true or not, the page for loop statement is not executed if the condition is not true once.