0% found this document useful (0 votes)
8 views

L4 - Loop

Uploaded by

vevele3809
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

L4 - Loop

Uploaded by

vevele3809
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

CE 204: Computer Programming Sessional

Loop
1
Ahmed Farhan Ahnaf Siddique

Assistant Professor,
Department of Civil Engineering,
BUET

2
Statement Blocks
• A statement block is a sequence of statements
enclosed by braces{ }, like this:

{int temp, x, y; temp=x; x = y; y = temp;}

• In C++ programs, a statement block can be used


anywhere that a single statement can be used.

• Local variables declared inside a block only exist


during the execution of the block.

• Block can limit the scope of a variable, allowing the


same name to be used for different variables in
different parts of a program.
3
Statement Blocks
#include <iostream>
using namespace std;
int main()
{
int n=44;
cout << "n = " << n << endl;
{
int n; // scope extends over 4 lines
cout << "Enter an integer: ";
cin >> n;
cout << "n = " << n << endl;
}
{
cout<<"n="<<n<<endl;//the first declared n
}
{
int n; // scope extends over 2 lines
cout << "n = " << n << endl;
}
cout << "n = " << n << endl; //the first declared n
} 4
Iteration
• Often, we need to repeat the execution of a portion
of the code for a given number of times or until a
specific condition is met.

• Iteration is the repetition of a statement or block of


statements in a program.

• C++ has three iteration statements: the while


statement, the do ... while statement, and the for
statement.

• Iteration statements are also called loops because of


their cyclic nature. 5
The while Statement
• The syntax for the while statement is
while (condition) statement;
– where condition is an integral expression and
statement is any executable statement.
• The structure of while statement is
while (condition)
{
statements;
}
statements; 6
The while Statement

• If the value of the expression is zero (meaning


“false”) then the statement is ignored, and program
execution immediately jumps to the next statement
that follows the while statement.

• If the value of the expression is nonzero (meaning


“true”) then the statement is executed repeatedly
until the expression evaluates to zero.

• Note that the condition must be enclosed by


parentheses.

7
The while Statement
#include <iostream>
using namespace std;
int main()
{
int n, i=1;
cout << "Enter a positive integer: ";
cin >> n;
long sum=0;
while (i <= n)
sum += i++;
cout << "The sum of the first " << n << "
integers is " << sum << endl;
}
8
The do … while Statement
• The syntax for the do ... while statement is

do statement while (condition);

– where condition is an integral expression and


statement is any executable statement.

• It repeatedly executes the statement and then


evaluates the condition until that condition
evaluates to false.

• The structure of do … while statement is:

do {statement;}

while (condition); 9
The do … while Statement

• The do ... while statement works the same as the


while statement except that its condition is
evaluated at the end of the loop instead of at the
beginning.

• This means that any control variables can be


defined within the loop instead of before it.

• It also means that a do ... while loop will always


iterate at least once, regardless of the value of its
control condition.

10
The do … while Statement
#include <iostream>
using namespace std;
int main()
{
long bound;
cout << "Enter a positive integer: ";
cin >> bound;
cout << "Factorial numbers < " << bound <<
":\n1,1";
long f=1,i=1;
11
The do … while Statement
do
{
f *= ++i; Can you
correct this
cout << "," << f;
program?
}
while (f < bound);
}
• The do … while loop iterates until its control
condition (f < bound) is false.
Output:
Enter a positive integer: 1000000
Factorial numbers < 1000000:
1,1,2,6,24,120,720,5040,40320,362880,3628800
12
The for Statement
• The syntax for the for statement is

for (initialization; condition; update) statement;

– where initialization, condition, and update are


optional expressions, and statement is any
executable statement.

• The three-part (initialization; condition; update)


controls the loop.

• The initialization expression is used to declare


and/or initialize control variable(s) for the loop; it is
evaluated first, before any iteration occurs.
13
The for Statement

• The condition expression is used to determine


whether the loop should continue iterating; it is
evaluated immediately after the initialization; if it is
true, the statement is executed and if it is false
execution continues with the statement after the
loop.

• The update expression is used to update the control


variable(s); it is evaluated after the statement is
executed.

14
The for Statement

• So, the sequence of events that generate the iteration


are:

1. Evaluate the initialization expression;

2. If the value of the condition expression is false,


terminate the loop;

3. If the value of the condition expression is true,


Execute the statement;

4. Evaluate the update expression;

5. Repeat steps 2–4.


15
The for Statement
#include <iostream>
using namespace std;
int main()
{
int sum=0;
cout<<"sum = "<<sum<<endl;
for(int i=1; i<4; i++)
{
cout<<"i = "<<i;
sum +=i;
cout<<"\tsum = "<<sum<<endl;
}
} 16
The for Statement

17
The for Statement
#include <iostream>
using namespace std;
int main()
{
for (int m=95, n=11; m%n > 0; m -= 3, n++)
cout << m << "%" << n << " = " << m%n << endl;
}
Output:
95%11 = 7
92%12 = 8
89%13 = 11
86%14 = 2
83%15 = 8 18
The for Statement
#include <iomanip> // defines setw()
#include <iostream>
using namespace std;
int main()
{
for (int x=1; x <= 12; x++)
{
for (int y=1; y <= 10; y++)
cout << setw(4) << x*y;
cout << endl;
}
}
19
The continue Statement
• The break statement skips the rest of the statements
in the loop’s block, jumping immediately to the next
statement outside of the loop.

• The continue statement is similar, it also skips the


rest of the statements in the loop’s block, but instead
of terminating the loop, it transfers execution to the
next iteration of the loop.

• It continues the loop after skipping the remaining


statements in its current iteration.

20
The continue Statement
#include <iostream>
using namespace std;
int main()
{
int n;
for (;;)
{
cout << "Enter int: "; cin >> n;
if (n%2 == 0) continue;
if (n%3 == 0) break;
cout << "\tBottom of loop.\n";
}
cout << "\tOutside of loop.\n";
} 21
The goto Statement
• The goto statement is another kind of jump
statement, its destination is specified by a label
within the statement.

• A label is simply an identifier followed by a colon


placed in front of a statement.

• Labels work like the case statements inside a switch


statement: they specify the destination of the jump.

• Breaking out of several or all the loops in a nest


requires a goto statement.

22
The goto Statement
#include <iostream>
using namespace std;
int main()
{
const int N=5;
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
for (int k=0; k<N; k++)
{
if (i+j+k>N) goto esc;
else cout << i+j+k << " ";
}
cout << "* ";
}
esc: cout << ". " << endl; //inside i loop, outside j loop
}
} 23
The goto Statement
Output:

01234*12345*2345.

12345*2345.

2345.

345.

45.

• When the goto is reached inside the innermost k


loop, program execution jumps out to the labeled
output statement at the bottom of the outermost i
loop. 24
The goto Statement
• Since that is the last statement in the i loop, the i
loop will go on to its next iteration after executing
that statement.

• When i and j are 0, the k loop iterates 5 times,


printing 0 1 2 3 4 followed by a star *.

• Then j increments to 1 and the k loop iterates 5


times again, printing 1 2 3 4 5 followed by a star *.

• Then j increments to 2 and the k loop iterates 4


times, printing 2 3 4 5.

25
The goto Statement
• But then on the next iteration of the k loop, i = 0, j =
2, and k = 4, so i+j+k = 6, causing the goto statement
to execute for the first time.

• So, the execution jumps immediately to the labeled


output statement, printing a dot and advancing to
the next line.

• Note that both the k loop and the j loop are aborted
before finishing all their iterations.

• Now i = 1 and the middle j loop begins iterating


again with j = 0.
26
The goto Statement
• The k loop iterates 5 times, printing 1 2 3 4 5
followed by a star *.

• Then j increments to 1 and the k loop iterates 4


times, printing 2 3 4 5.

• But then on the next iteration of the k loop, i = 1, j =


2, and k = 3, so i+j+k = 6, causing the goto statement
to execute for the second time.

• Again, execution jumps immediately to the labeled


output statement, printing a dot and advancing to
the next line. 27
Thank You!
28

You might also like