web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

While loop

Hossein.K Profile Picture Hossein.K 6,650
WhileThe while loop evaluates a condition and executes statements, depending on
whether the condition is satisfied. The loop continues to check the condition, and
as long as the condition is true, it continues to execute the statements. As soon as
the condition becomes false, the statement is exited. The syntax is as follows:
while (condition)
{
//statement;
}

This is a simple while statement that counts from 1 to 5.int counter = 1;
while (counter <= 5)
{
print counter;
counter ++; // This increments the counter by 1
}
pause;
Result:
1
2
3
4
5
HINT: The previous example uses the counter++ which can increment any
integer by 1. You can also set the increment to any value by using the
variable+=<# to increment> syntax. For example:
counter+=2; //This increments the counter variable by two every time.
Notice the condition is evaluated before the statements are executed. In the
previous example, it means that if the counter variable is greater than five when it
reaches this loop, it does not execute. Therefore, a
while statement can be
executed zero or more times.


Best Regards,
Hossein Karimi

Comments

*This post is locked for comments