Chapter 4.0
Chapter 4.0
Chapter 4.0
LOOPING
STATEMENTS
DEC20012
Looping Statements
4.1 Remember Looping statements
4.1.1 Define Looping statement
4.1.2 List types of Looping Statement
4.1.3 Define FOR, WHILE, DO-WHILE loop statements
4.1.4 Identify need for BREAK, CONTINUE and GOTO
statements
4.1.5 Describe structure of FOR, nested FOR, WHILE, DO-
WHILE loop statements
Looping Statements
4.2 Understand Looping statements.
4.2.1 Differentiate FOR, nested FOR, WHILE, DO-WHILE loop
statements
4.2.2 Explain working of BREAK statement
An action or a series
of actions
An action or a series
of actions
Looping
• A loop has two parts –
body and condition
• Body – a statement or Body
a block of statements
that will be repeated.
• Condition – is used to
Condition
control the iteration – True
• Updating Updating
- is used to update the condition.
Loop
statements
False
Condition Body
True
Updating
Body
True
Condition
Updating
False
Parts of a loop
• Example: These flowcharts print numbers 10 down to 1
Comparison
• while :
– while tests a condition at the beginning of the loop
– condition must first be true for the loop to run even once
• do while :
– do/while tests a condition at the end of the loop
– loop will run at least once
• for :
– for facilitates initializing and incrementing the variable that
controls the loop
– Especially helpful for:
– Looping for a known number of times
for looping
The C for statement lets you specify the initialization, test,
and update operations of a structured loop in a single
statement. The for statement is created as follows:
where:
init_exp: is an expression that is evaluated before the loop is entered.
cond_exp: is an expression that is evaluated before each pass through the
loop.
update_exp: is an expression that is evaluated at the end of each pass through
the loop, after the loop body has been executed, and just before looping
back to evaluate cond_exp again.
for looping
The C for statement lets you specify the initialization, test,
and update operations of a structured loop in a single
statement. The for statement is created as follows:
C syntax
for looping
The C for statement lets you specify the initialization, test,
and update operations of a structured loop in a single
statement. The for statement is created as follows:
flowchat
for looping
Example 1 - Display number 1 to 10
for looping
Example 2 - Display asterisk
for looping
Example 2 - Display asterisk
for looping
Example 3 - Display asterisk (based on user REQUEST!)
for looping
Example 4 - Display sin(x)
for looping
Exercise 1 - Display 2, 4, 6, …., 40
Exercise 2 - Display answer for log10(x) for 1 ≤ x ≤ 100 with step 1.0
math.h
Table : List of functions in math.h
Function Return value
acos(d) Arc cosine of d
asin(d) Arc sine d
atan(d) Arc sine d
atan2(d1,d2) Arc tangent of d1/d2
ceil(d) Smallest integer greater than d
cos(d) Cosine of d
exp(d) e (i.e.,2.718…) raised to the power d
fabs(d) Absolute value of d
floor(d) Largest integer smaller than d
fmod(d1,d2) Remainder of d1/d2
log(d) Natural logarithm of (d>0)
log10(d) Base logarithm of d (d>0)
pow(d1,d2) D1 raised to the power of d2
sin(d) Sine of d
sinh(d) Hyperbolic sine of d
sqrt(d) Square root of d
tan(d) Tangent of d
tanh(d) Hyperbolic tangent of d
while looping
The while loop can be used if you don’t know how many times a loop
must run (sometimes) until the condition is met. The statement of
while is:
while looping
• while loop/repetetion statement : “For example,----- ”
– Pseudo-code:
– C syntax:
– Flowchart:
while looping
Example 1 – Display 20 to 0. (Decrement numbers)
while looping
Example 2 – We add 0+1+2+3+4+5+6+…..+100
while looping
Exercise 1 – Write a program to display an output of the factorial
number.
– C syntax:
– Flowchart:
do..while looping
Example 1 - count -10 to 10
do..while looping
Exercise 1 – print 30 star (*) by using do..while loop.
do..while looping
• Difference between while and do while loop
– The do while statement is similar to the while statement except
that its termination condition is at the end of the body of the loop
only. Thus, you want to use a do statement, if you want to
perform the body of the loop at least once, regardless of the
condition.
do..while looping
• Difference between while and do while loop
– The do while statement is similar to the while statement except
that its termination condition is at the end of the body of the loop
only. Thus, you want to use a do statement, if you want to
perform the body of the loop at least once, regardless of the
condition.
nested for
• A for loop inside another for loop is called nested for
loop.
• Syntax of Nested for loop:
for (initialization; condition; increment/decrement)
{
statement(s);
for (initialization; condition; increment/decrement)
{
statement(s);
... ... ...
}
... ... ...
}
nested for
• Example: C program to print all the composite numbers from 2 to a certain
number entered by user.
nested for
• Example: C program to print multiplication table from 1 to 5
Output:
jump statement
Jump statements
• In addition to the sequence, repetition and selection , C
also provides jump statements.
• The statements allow program control to be transferred
from one part of the program to another program
unconditionally.
• There are four jump statements:
break statement
Break statement – The break statement in C programming
language has following two usage:
– When the break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next
statement following the loop.
– It can be used to terminate a case in the switch statement (covered in
the previous chapter).
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
• break statement:
– It performs a one-way transfer of control to another line of code;
– The set of identifier names following a goto has its own name space so the
names do not interfere with other identifiers. Labels cannot be redeclared.
continue statement
Continue – The continue statement in C programming language
works somewhat like the break statement. Instead of forcing
termination, however, continue forces the next iteration of the
loop to take place, skipping any code in between.
continue statement
• continue statement:
– The continue statement, when executed in a while, for or do…while
statement, skips the remaining statements in the body of that control
statement and performs the next iteration of the loop.
continue statement
Example:
n = 10; Output:
while (n>0)
{ 10 9 9 9 9 9 …………
printf(“%d ”,n);
if (n%2==1) continue; The loop then prints number 9
n = n –1; over and over again. It never
} stops.
continue statement
start
Example:
n=10
n = 10;
while (n>0) Transfer to the
loop condition
{
false
printf(“%d ”,n); n>0
if (n%2==1) continue;
true
n = n –1;
} print n
true
n%2==1
false
n=n-1
stop
goto statement
goto - The goto statement is used to alter the normal sequence of
program execution by transferring control to some other part of the
program unconditionally. In its general form, the goto statement is
written as goto label;
where the label is an identifier that is used to label the target
statement to which the control is transferred.
goto statement
• goto statement:
– It performs a one-way transfer of control to another line of
code;
– The set of identifier names following a goto has its own name
space so the names do not interfere with other identifiers.
– Labels cannot be redeclared.
goto statement