0% found this document useful (0 votes)
52 views

Control Loop Statements, Jump Statements

Loops allow repeating actions while a condition is true. The main loop statements are while, for, and do-while. The while statement checks the condition first before executing the loop body. The for statement allows initializing and updating a loop counter. The do-while statement executes the body first before checking the condition. Jump statements like break, continue, return and goto can alter normal loop flow by exiting the loop or skipping iterations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Control Loop Statements, Jump Statements

Loops allow repeating actions while a condition is true. The main loop statements are while, for, and do-while. The while statement checks the condition first before executing the loop body. The for statement allows initializing and updating a loop counter. The do-while statement executes the body first before checking the condition. Jump statements like break, continue, return and goto can alter normal loop flow by exiting the loop or skipping iterations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 8

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.

 While there are more items on my shopping list


Purchase next item and cross it off my list
Loops
• The main idea of a loop is to repeat an action or a series of actions.

The concept of a loop


Loops
• But, when to stop looping?
• In the following flowchart, the action is executed over and over again.
It never stop – This is called an infinite loop
• Solution – put a condition to tell the loop either continue looping or
stop.
Loops
• A loop has two parts – body
and condition

• Body – a statement or a block


True
of statements that will be Body
repeated.

• Condition – is used to control


the iteration – either to
Condition
continue or stop iterating.

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

for (Initialization; Condition; Updating )


{
Condition
Repeated_Actions;
} True

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

Comparing for and while loops


The do-while Statement in C
• The syntax of do-while statement in C:
do
statement
while (loop repetition condition);
condition
• The statement is first executed.
• If the loop repetition condition is true, the
statement is repeated.
• Otherwise, the loop is exited.
5-15
do…while statement

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

Note that, the first line (n=10) is


actually not a part of the loop statement.

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: These two for statments are equivalent.

for (n=10; n>0; n=n-1) for (n=10; n>0; n=n-1)


{ printf(“%d “, n);
printf(“%d ”, n);
}
Jump statements
• You h ave le ar n th at, t he r ep e tition o f a lo op is con tr olled b y th e loo p co n dit io n.
• C p ro vid es a no th e r wa y to co n tro l t h e loo p , b y usin g ju mp sta te men ts.
• The re a re f ou r ju mp sta te men ts:
break statement
• It causes a loop to terminate

Example:

for (n=10; n>0; n=n-1) Output:


{
if (n<8) break; 10 9 8
printf(“%d “, n);
}

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.

The continue statement


continue statement
Example:

for (n=10; n>0; n=n-1) Output:


{
if (n%2==1) continue; 10 8 6 4 2
printf(“%d “, n);
}

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

You might also like