Control Loop Statements, Jump Statements
Control Loop Statements, Jump Statements
Repetition
(also known as Looping)
Repetition Statement
• A repetition statement allows you to specify
that an action is to be repeated while some
condition remains true.
False
Loop statements
• C provides three loop statements:
The while Statement in C
• The syntax of while statement in C:
while (loop repetition condition)
condition
statement;
• Loop repetition condition is the condition
which controls the loop.
• The statement is repeated as long as the loop
repetition condition is true.
true
• A loop is called an infinite loop if the loop
repetition condition is always true.
5-7
while statement
while flowchart
while(Condition)
Condition
{
Statements;
} True
Repeated_Actions False
while statement
Example: This while statement prints numbers 10 down to 1
Note that, the first line (n=10) is
actually not a part of the loop statement.
n=10
n=10;
while (n>0)
{
printf(“%d “, n); is n>0?
n=n-1;
} True
Print
n
False
n = n -1
Output:
10 9 8 7 6 5 4 3 2 1
The for Statement in C
• The syntax of for statement in C:
for (initialization expression;
expression
loop repetition condition;
condition
update expression)
expression
statement;
• The initialization expression set the initial value of the
loop control variable.
• The loop repetition condition test the value of the
loop control variable.
• The update expression update the loop control
variable.
5-10
for statement
for flowchart
Initialization
Repeated_Actions
False
Updating
for statement
Example: This for statement prints numbers 10 down to 1
for (n=10;n>0;n=n-1)
n=10
{
printf(“%d “, n);
}
is n>0?
True
Output: Print
n
False
10 9 8 7 6 5 4 3 2 1
n = n -1
Nested Loops
• Nested loops consist of an outer loop with one
or more inner loops.
loops
• e.g.,
Outer loop
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){
… Inner loop
}
}
• The above loop will run for 100*50 iterations.
5-13
for vs. while statements
do True Repeated_Actions
{
Repeated_Actions;
} while (Condition);
Condition
False
do-while loop is used to ensure that the statements within the
loop are executed at least once.
do…while statement
Example: This do…while statement prints numbers 10 down to 1
n=10
n=10;
do
{ Print
n
printf(“%d “, n); True
n=n-1;
n = n -1
} while (n>0);
is n>0?
Output:
False
10 9 8 7 6 5 4 3 2 1
Loop statements
• If the body part has only one statement, then the bracket
symbols, { } may be omitted.
Example:
Break statement takes the execution control out of the loop. The break
statement, when executed in a while, for, do…while or switch statement,
causes an immediate exit from that statement.
continue statement
• In while and do…while loops, the continue statement transfers the control to the loop
condition.
• In for loop, the continue statement transfers the control to the updating part.
Continue statement skips the execution of the statements after it and takes the
control to the beginning of the loop.
continue statement
Example:
n = 10; Output:
while (n>0)
{ 10 9 9 9 9 9 …………
printf(“%d “, n);
The loop then prints number 9 over
if (n%2==1) continue; and over again. It never stops.
n = n –1;
}
Return statement
• Exits the function.
• return exits immediately from the currently
executing function to the calling routine,
optionally returning a value. The syntax is:
• return [expression];
• For example,
int sqr (int x) { return (x*x);}
goto
• Unconditionally transfer control.
• goto may be used for transfering control from
one place to another. The syntax is:
• goto identifier; Control is unconditionally
transferred to the location of a local label
specified by identifier. For example,
• Again:
• ...
• goto Again;
goto statement
• It is used to translate connector symbols – jump to another part inside a program.
• But, it is not recommended to use - it may cause unstructured programs.
Example:
n=10;
A:
n=10 printf(“%d “, n);
n = n -1;
A
Print
n
if (n>0) goto A;
n = n -1
Output:
is n>0? True A
10 9 8 7 6 5 4 3 2 1
False