Chapter 4.0

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 55

CHAPTER 4:

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

4.3 Apply Looping statements


4.3.1 Construct program(s) that use FOR, nested FOR, WHILE,
DO-WHILE loop statements
4.3.2 Construct program(s) that use BREAK and GOTO
statements

4.4 Use loop statement to demonstrate the I/O operation


Definition
of looping/ repetition

• Loops provide a way to repeat commands and control


how many times they are repeated.
• C provides three types of looping structures in the form
of statements.
– for statement
– while statement
– do…while statement
C. Repetition/iteration structure
(looping)
• The main idea of a loop is to repeat an
action or a series of actions.

An action or a series
of actions

Figure 1: The concept of a loop 5


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

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

either to continue or False


stop iterating.
Types of looping
Pretest loop
• Two forms of loop –
pretest loop and post-
test loop. Condition
False
• Pretest loop
Here. Must
True
- the condition is tested be True

first, before we start


executing the body. Body
- The body is executed if
the condition is true.
- After executing the
body, the loop repeats
Types of looping
Post-test loop
• Post-test loop
- the condition is tested
later, after executing
the body.
Body
- If the condition is true,
the loop repeats,
otherwise it terminates.
- The body is always
Condition
executed at least once. True

The iterating False


part must be
True
Parts of a loop
• Initialization
- is used to prepare a loop before it
can start –usually, here we initialize
the condition.
Initialization

- The initialization must be written


outside of the loop – before the first
execution of the body. True Body

• Updating Updating
- is used to update the condition.

- If the condition is not updated, it


always true => the loop always Condition
repeats- an infinite loop.
False
- The updating part is written inside the
loop – it is actually a part of the body.
Loop statements
• C provides three loop statements:

Loop
statements

while for do…while


Pretest loop Pretest loop Post-test loop
Definition
• while loop statement
– A while statement is like a repeating if statement.
– Like an if statement, if the test condition is true: the statements 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.
Definition
• do…while loop statement
– 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.

• for loop statement


– for loop is very flexible based on the combination of the three expression is
used. 
– The counter can be not only counted up but also counted down.  You can count
by twos, threes and so on. You can count by not only number but also
character. Let's take a look at the example program below:
Parts of a loop
• Beside the body and condition, a loop may have
two other parts – Initialization and Updating
Pretest loop Post-test loop
• for • do…while
• while Initialization Initialization

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.

Example, if you put a number 4, the program will calculate 4x3x2x1.

if you put number 7, it will do 7x6x5x4x3x2x1.

By using while loop, write this program!


do..while looping
• do..while loop/repetetion statement :

– 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:

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


{
if (n%2==1) continue; 10 8 6 4 2
printf(“%d ”,n);
}
#include <stdio.h>
#include <stdio.h> void main ()
void main () {
{ int n;
int n;
n = 10;
for (n=10; n>0; n=n-1) while (n>0)
{ {
if (n%2==1) continue; printf(“%d ”,n);
printf(“%d ”,n); if (n%2==1) continue;
} n = n –1;
system (“pause”); }
} system (“pause”);
}
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

You might also like