Usage of for and do loop statements in asp.net

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.