0% found this document useful (0 votes)
35 views24 pages

Lecture 8 Looping Statements

This document is a lecture on looping statements that covers while loops, do/while loops, and for loops. It includes examples of each loop type and explains their syntax and usage. Jump statements like break, continue, and goto are also discussed. The objectives are to write and analyze problems using looping statements. Various exercises are provided to help students practice writing programs that use different looping structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views24 pages

Lecture 8 Looping Statements

This document is a lecture on looping statements that covers while loops, do/while loops, and for loops. It includes examples of each loop type and explains their syntax and usage. Jump statements like break, continue, and goto are also discussed. The objectives are to write and analyze problems using looping statements. Various exercises are provided to help students practice writing programs that use different looping structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Looping Statements

Lecture 8
PROGRAMMING 1

Engr. Jennelyn P. Cabale


Tasks

Pre Task Dev C++

While Task Lecture Demonstration

Post Task Exercises

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 2


Objectives
• Write and analyze problems using looping statements

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 3


The while statement
• Syntax:
initialization ;
while ( condition )
{ statement(s) ;
inc/dec ;
}

• The loop iterates while the condition is true.

• When the condition becomes false, program control passes to the line after
the loop code.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 4


Example 1:

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 5


Example 2 : Write a program that accept a number and display its sequence
starting from 1.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 6


Example 3 : Write a program that accept a number and display its cube equivalent.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 7


Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 8
The do/while statement
• Syntax:
initialization;
do
{ statement(s) ;
inc/dec ;
} while ( condition );

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 9


Example 4

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 10


Example 5: Write a program that accept a number and display its sequence starting
from 1.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 11


Example 6: THE FACTORIAL FUNCTION Write a program that accept a number
and display its factorial value.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 12


The for Loop
• Syntax:
for (initialization; condition; increment)
statement ;

• Initialization is usually an assignment statement that is used to set the


loop control variable.
• Condition is a relational expression that determines when the loop will
exit.
• Increment defies how the loop control variable will change each time the
loop is repeated.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 13


How it works
1. Initialization is executed. Generally, this declares a counter variable, and sets it to
some initial value. This is executed a single time, at the beginning of the loop.

2. Condition is checked. If it is true, the loop continues; otherwise, the loop ends,
and statement is skipped, going directly to step 5.

3. Statement is executed. As usual, it can be either a single statement or a block


enclosed in curly braces { }.

4. Increase is executed, and the loop gets back to step 2.

5. The loop ends: execution continues by the next statement after it.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 14


• The three fields in a for-loop are optional.

• They can be left empty, but in all cases the semicolon signs between
them are required.

• For example, for(;n<10;) is a loop without initialization or increase and


for (;n<10;++n) is a loop with increase, but no initialization (maybe
because the variable was already initialized before the loop).

• A loop with no condition is equivalent to a loop with true as condition


(i.e., an infinite loop).

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 15


• Because each of the fields is executed in a particular time in the life cycle of a
loop, it may be useful to execute more than a single expression as any of
initialization, condition, or statement.

• Unfortunately, these are not statements, but rather, simple expressions, and
thus cannot be replaced by a block.

• As expressions, they can, however, make use of the comma operator (,): This
operator is an expression separator, and can separate multiple expressions
where only one is generally expected.

• For example, using it, it would be possible for a for loop to handle two counter
variables, initializing and increasing both:
for ( n=0, i=100 ; n!=i ; ++n, --i ) { // whatever here... }
Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 16
Jump Statements
• Jump statements allow altering the flow of a program by performing
jumps to specific locations.

• The break statement


–break leaves a loop, even if the condition for its end is not fulfilled. It can be used to
end an infinite loop, or to force it to end before its natural end.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 17


• The continue statement
–causes the program to skip the rest of the loop in the current iteration, as if the end
of the statement block had been reached, causing it to jump to the start of the
following iteration.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 18


• The goto statement
–goto allows to make an absolute jump to another point in the program.
–This unconditional jump ignores nesting levels, and does not cause any automatic
stack unwinding. Therefore, it is a feature to use with care, and preferably within
the same block of statements, especially in the presence of local variables.
–The destination point is identified by a label, which is then used as an argument for
the goto statement.
–A label is made of a valid identifier followed by a colon (:).

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 19


Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 20
Example 10: Write a program that accept a number and display its sequence starting
from 1.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 21


Example 11: THE EXTREME VALUES IN A SEQUENCE Write a program that accept a number
and display its smallest and largest numbers in the sequence.

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 22


Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 23
THE END

Engr. Jenn P. Cabale Lecture 8 - LOOPING STATEMENTS 27

You might also like