Loops are used in programming to repeat a block of code until a specific condition is met. There are three loops in C programming:
1. while loop
2. for loop
3. do...while loop
while loop
The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.
Syntax of while loop :
while (testExpression)
{
//codes
}
Flowchart:
for loop
for loop is similar to while, it's just written differently. for statements are often used to process lists such a range of numbers:
Syntax of for loop:
for (initializationStatement; testExpression; updateStatement)
{
// codes
}
Flowchart:
do...while loop
do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.
Syntax of do...while loop:
do
{
// codes
}
while (testExpression);
break -- exit form loop or switch.
No comments:
Post a Comment