The usage of for and do loops is Shared in asp.net

  • 2020-05-17 05:04:03
  • OfStack

The FOR loop in this example creates an Mandelbrot image.
 
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 for loop structure
The DO statement of the loop structure executes the corresponding code according to the test result of the Boolean value, and DO statement executes at least once.
 
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; 
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(); 
} 
} 
} 

conclusion
The DO statement executes at least once in a loop, regardless of whether the condition holds, and the for statement does not execute once if the condition does not hold.

Related articles: