Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 5 Looping Statements

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

CHAPTER 5:

LOOPING
STATEMENTS
DEC2012
Looping Statements
5.1 Know the looping statements.
5.1.1 Define Looping statement.
5.1.2 List type of looping statement.
5.1.3 Define for, do..while and while loop statements
5.1.4 Identify the need for break, continue and goto statements.
5.1.5 Describe the structure of for, do..while and while loop
statements.
Looping Statements
5.2 Understand the looping statements.
5.2.1 Differentiate the for, do..while and while loop statements
5.2.2 Explain the working of break statement.

5.3 Apply the looping statements.


5.3.1 Construct program(s) that use for, do..while and while
loop statements
5.3.2 Construct program(s) that use break and goto statement.
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
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:
Loop statements
• C provides three loop statements:

Loop
statements

while for do…while


Pretest loop Pretest loop Post-test loop
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
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
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.
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
• 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.
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