Computer
Programming
• Course Code: CSC-113
• Course Instructor: Mehreen Tariq
• Email:
[email protected]DO{} WHILE() Loop
do-while: a posttest loop – execute
the loop, then test the expression
General Format:
do
statement; // or
block in { }
while (expression);
Note that a semicolon is required after
(expression)
#include<iostream>
using namespace std;
int main()
{
cout << "Do While Loop
Started!";
int i=1;
Example do
{
cout << "\nHello";
i++;
} while (i <= 5);
cout << "\nDo While Loop
Ended!";
system("pause");
FOR() Loop
• Useful for counter-controlled loop
• General Format:
for(initialization; test; update)
statement; // or block in { }
• No semicolon after the update expression or after the )
For Loop (How it Works)
for(initialization; test; update)
statement; // or block in { }
1. Perform initialization
2. Evaluate test expression
• If true, execute statement
• If false, terminate loop execution
3. Execute update, then re-evaluate test expression
#include<iostream>
using namespace std;
int main()
{
cout << "For Loop Started!";
int i;
Example for (i = 1; i <= 5; i++)
{
cout << "\nHello";
}
cout << "\nFor Loop Ended!";
system("pause");
return 0;
}
Start
Display “For Loop
Started”
Assign i=1
Flow Yes
Chart i<=5 Display “\nHello” i++
No
Display “\ Loop
Ended” nWhile
End
Pretest Loop
• The for loop tests its test expression before each iteration, so
it is a pretest loop.
• The following loop will never iterate:
for (count = 11; count <= 10; count++)
cout << "Hello" << endl;
For Loop Modification
• You can have multiple statements in the initialization
expression. Separate the statements with a comma,
Initialization Expression
int x, sum;
for (x=1, sum=0; x <= 5; x++)
{
• sum= x+sum;
cout << x << " plus " << y << " equals " << sum << endl;
}
For Loop Modification
• You can also have multiple statements in the test expression.
Separate the statements with a comma:
Test Expression
int x, y;
for (x=1, y=1; x <= 5; x++,y++)
{
cout<<x<<"plus "<<y<<"equals"<<x+y<<
endl;
}
For Loop Modification
• You can omit the initialization expression if it has already been done:
int sum = 0, num = 1;
for (; num <= 10; num++)
sum += num;
• You can declare variables in the initialization expression:
int sum = 0;
for (int num = 0; num <= 10; num++)
sum += num;
The scope of the variable num is the for loop.
Nested Loops
• A nested loop is a loop inside the body of another loop
• Inner (inside), outer (outside) loops:
for (row=1; row<=3; row++) //outer
for (col=1; col<=3; col++)//inner
cout << row * col << endl;
Nested Loops - Notes
• Inner loop goes through all repetitions for each repetition of outer
loop
• Inner loop repetitions complete sooner than outer loop
• Total number of repetitions for inner loop is product of number of
repetitions of the two loops.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int columns = 3;
for (int i = 1; i <= rows; ++i)
Example {
for (int j = 1; j <= columns;
++j)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
* * *
* * *
Output * * *
* * *
* * *
Activity #1
Write a C++ program using nested for loop to display following pattern.
1
12
123
1234
12345
Activity #2
Write a C++ program using nested for loop to display following pattern.
Enter Pattern Limit: 5
1
12
123
1234
12345
4321
321
21
1
Break vs Continue
Break Continue
Continue statement only stops the current iteration of
Break statement stops the entire process of the loop.
the loop.
Continue doesn’t terminate the next iterations; it
Break also terminates the remaining iterations.
resumes with the successive iterations.
Break statement can be used with switch statements Continue statement can be used with loops but not
and with loops switch statements.
In the break statement, the control exits from the In the continue statement, the control remains within
loop. the loop.
It is used to stop the execution of the loop at a specific
It is used to skip a particular iteration of the loop.
condition.
Home Work
1) Deciding Which Loop to Use?
2) Activity 1
3) Activity 2
Reference:
Tony Gaddis, “Starting out with C++”, 6th Edition, Pearson -> Chapter 05
Summary
• DO{} WHILE() Loop,