Control Statements
Control Statements
Loops
• Loops can execute a block of code as long as a
specified condition is reached.
• Loops save time, reduce errors, and they make
code more readable.
while loop
• The while loop loops through a block of code
as long as a specified condition is true:
• Syntax
while (condition) {
}
Example
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
do while loop
• The do/while loop is a variant of
the while loop.
• This loop will execute the code block once,
before checking if the condition is true, then it
will repeat the loop as long as the condition is
true.
Example
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
for loop
• The for loop in C Language provides a
functionality/feature to repeat a set of
statements a defined number of times. The for
loop is in itself a form of an entry-controlled
loop.
• Unlike the while loop and do…while loop, the
for loop contains the initialization, condition,
and updating statements as part of its syntax.
Syntax of for loop
for(initialization; check/test expression;
updation)
{
// body consisting of multiple statements
}
Example
int i;