The For loop for this example creates a Mandelbrot image.
Copy Code code as follows:
Using System;
Namespace A
{
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 of the loop structure executes the corresponding code according to the test result of the Boolean value, and the DO statement executes at least once.
Copy Code code as follows:
Using System;
Namespace A
{
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 would you like to have?");
Targetbalance=convert.todouble (Console.ReadLine ());
int totalyears=0;
Todo
{
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 ();
}
}
}
Summarize
The DO statement executes at least once at loop time, regardless of whether the condition is set up, and the page for Loop statement does not execute if the condition is not set.