You are on page 1of 20

Programming Logic and Design

TOPIC TITLE: C++ LOOP STATEMENTS


SPECIFIC OBJECTIVES:

At the end of the topic session the students are


expected to:
1. Describe what is C++ loop statement.
2. Describe the general form.at of for loop
statement
3. Develop a C++ program using C++for
loop statement.
MATERIALS/EQUIPMENT:
1. Power Point Presentation
2. PLD Module #14

TOPIC PRESENTATION:

Flow of discussion for C++ loop statement:


1. Introduce C++ loop statement.
2. Discuss the syntax of for loop statement.
3. Discuss sample C++ program using for
loop statement.

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 1 of 20


Programming Logic and Design

C++ Loop Statement

Loop allows us to execute a block of code or


statement(s) several number of times. C++
provides the following types of loop to handle the
looping requirements in program.
/* C++ Loops Example */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the
number, 10 times:\n";
for(l=0; l<10; l++)
{
num++;
cout<<num<<"\n";
}
getch();
}

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 2 of 20


Programming Logic and Design

Here is the sample run of the above C++


program

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 3 of 20


Programming Logic and Design

/* C++ Loops Program */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l=0;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the
number, 10 times:\n";
while(l<10)
{
num++;
cout<<num<<"\n";
l++;
}
getch();
}

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 4 of 20


Programming Logic and Design

Here is the sample run of this C++ program,


producing the same result as above

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 5 of 20


Programming Logic and Design

/* C++ Loops Example */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, l=0;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nIncrementing & Printing the
number, 10 times:\n";
do
{
num++;
cout<<num<<"\n";
l++;
}
while(l<10);
getch();
}

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 6 of 20


Programming Logic and Design

Here is the sample run of the above C++


program, also producing the same output as the
above two programs produced:

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 7 of 20


Programming Logic and Design

The iteration (for, while, and do-while loop)


statements allows a set of instruction to be
performed repeatedly until a certain condition is
fulfilled. The iteration statements are also called
loops or looping statements. C++ provides three
kinds of loops :
 for loop
 while loop
 do-while loop
All three loop constructs of C++ repeat a set of
statements as long as a specified condition
remains true. This specified condition is generally
referred to as a loop control. For all three loop
statements, a true condition is any nonzero
value. A zero value indicates a false condition.
Loop Parts

Every loops has its elements that control and


govern its execution. Generally, a loop has four
elements that have different purposes. These
elements are as :
 Initialization Expression(s)
 Test Expression
 Update Expression(s)
 Loop's Body

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 8 of 20


Programming Logic and Design

Initialization Expression(s)

Before entering in a loop, its control variable(s)


must be initialized. The initialization of the control
variable(s) takes place under initialization
expression(s). The initialization expression(s)
give(s) the loop variable(s) their first value(s).
The initialization expression(s) is executed only
once, in the beginning of the loop.
Test Expression

The test expression is an expression whose truth


value decides whether the loop-body will be
executed or not. If the test expression evaluates
to true i.e., 1, the loop-body gets executed,
otherwise the loop is terminated.
In an entry-controlled loop, the test-expression is
evaluated before exiting from the loop. In C++,
the for loop and while loop are entry-controlled
loops and do-while loop is exit-controlled loop.
Update Expression(s)

The update expression(s) change the value(s) of


loop variable(s). The update expression(s) is
executed; at the end of the loop after the loop-
body is executed.

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 9 of 20


Programming Logic and Design

Loop's Body

The statements that are executed repeatedly (as


long as the test-expression is nonzero) from the
body of the loop. In an entry-controlled loop, first
test-expression is evaluated and if it is nonzero,
the body-of-the-loop is executed; if the test-
expression evaluates to be zero, the loop is
terminated. In an exit-controlled loop, the body-
of-the-loop is executed first and then the test-
expression is evaluated. If it evaluates to be zero,
the loop is terminated otherwise repeated.
Now let's discuss the C++ loops: for, while
and do-while.
Syntax of for loop
for(initialization; condition ; increment/decrement)

{
C++ statement(s);
}
Flow of Execution of the for Loop
As a program executes, the interpreter
always keeps track of which statement is
about to be executed. We call this the
control flow, or the flow of execution of the
program.

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 10 of 20


Programming Logic and Design

First step: In for loop, initialization happens


first and only once, which means that the
initialization part of for loop only executes
once.
Second step: Condition in for loop is
evaluated on each loop iteration, if the
condition is true then the statements inside
for for loop body gets executed. Once the
condition returns false, the statements in for
loop does not execute and the control gets
transferred to the next statement in the
program after for loop.
Third step: After every execution of for
loop’s body, the increment/decrement part of
for loop executes that updates the loop
counter.
Fourth step: After third step, the control
jumps to second step and condition is re-
evaluated.
The steps from second to fourth repeats
until the loop condition returns false.

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 11 of 20


Programming Logic and Design

C++ for Loop


The for loop is the easiest to understand of
the C++ loops. All its loop-control elements
are gathered in one place (on the top of the
loop), while in the other loop construction of
C++, they (top-control elements) are
scattered about the program.

C++ for Loop Example

The following example program illustrates the


use of for statement :
/* C++ Iteration Statements - C++ for Loop */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
cout<<i<<" ";
}
getch();
}

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 12 of 20


Programming Logic and Design

When the above program is compile and


executed, it will produce the following output:

The following lines explain the working of the


above given for loop program :
 Firstly, initialization expression is executed,
i.e., i=1 which gives the first value 1 to variable
i.
 Then, the test expression is evaluated i.e., i <=
10 which results into true i.e., 1.
 Since, the test expression is true, the body-of-
the-loop i.e., cout << i << " ";
is executed which prints the current value of i
then a single space.
 After executing the loop-body, the update
expression i.e., i++ is executed which
increments the value of i. (After first execution
of the loop, the value of i becomes 2 after the
execution of i++, since initially i was 1).

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 13 of 20


Programming Logic and Design

 AFter the update expression is executed, the


test-expression is again evaluated. If it is true
the sequence is repeated from the step no 3,
otherwise the loop terminates.
Note - Use for loop when you have to repeat a
block of statements specific number of times.

Example 1: Fibonacci Series up to n number of


terms
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr;
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 14 of 20


Programming Logic and Design

cout << "Fibonacci Series: ";


for (int i = 1; i <= n; ++i)
{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 15 of 20


Programming Logic and Design

}
getch();
}

Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

C++ Infinite Loop

Although any loop statement can be used to create


an infinite loop (endless loop) yet for is traditionally
used for this purpose. An infinite for loop can be
created by omitting the test-expression as shown
below :
for(i=25; ; i--)
{
cout << "This loop runs forever..!!" ;
}
Here is an example of infinite loop in C++
/* C++ Loops - C++ Infinite Loop */

#include<iostream.h>
#include<conio.h>

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 16 of 20


Programming Logic and Design

void main()
{
clrscr();
for( ; ; )
{
cout<<"This loop will run
forever..!!";
}
getch();
}

When the conditional expression is absent, it is


assumed to be true. You may have an
initialization and increment expression, but C++
programmers more commonly use the for(;;)
construct to signify an infinite loop. If you try to
run the above C++ program, then the output
window will come out like this, printing
continuously

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 17 of 20


Programming Logic and Design

NOTE - You can terminate an infinite loop by


pressing Ctrl+C keys. Let's take another
program, prints stars infinite times
/* C++ Loops - C++ Infinite Loop */

#include<iostream.h>
#include<conio.h>
void main()

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 18 of 20


Programming Logic and Design

{
clrscr();
for(;;)
{
cout<<"* ";
}
getch();
}
After running this C++ program, output window
will look like this, printing stars infinite times.

REFERENCES:

 C++: From Problem Analysis to

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 19 of 20


Programming Logic and Design

Program Design

 Introduction to C++ Programming

Logic

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 20 of 20

You might also like