本樣本的FOR迴圈建立一個Mandelbrot映像。
複製代碼 代碼如下: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語句
迴圈結構之DO語句,根據布爾值的測試結果,執行相應代碼,DO語句至少執行一次。 複製代碼 代碼如下: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();
}
}
}
總結
DO語句在迴圈時不管條件是否成立它至少執行一次,頁for迴圈語句如果條件不成立一次不執行了。