0% found this document useful (0 votes)
9 views31 pages

Loops

Uploaded by

miyata9308
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views31 pages

Loops

Uploaded by

miyata9308
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Loop/iteration constructs

Week 6 NUML Lahore


Loops
• Loops cause a section of your program to
be repeated a certain number of times
• loops are used to repeat a block of code.

• Repeats until the condition remains true


• Terminates when the condition becomes
false
• For example, let's say we want to show a
message 100 times. Then instead of writing the
print statement 100 times, we can use a loop. 2
Loops in C++
1. while loop
2. do while loop
3. for loop

3
Flowchart of while Loop

4
While Loop- Syntax

• A while loop evaluates the condition


• If the condition evaluates to true, the code inside the while loop is
executed.
• The condition is evaluated again.
• This process continues until the condition is false.
• When the condition evaluates to false, the loop terminates.

5
Example 1: Display Numbers from 1 to 5

// C++ Program to print numbers from 1 to 5


#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop from 1 to 5
while (i <= 5) {
cout << i << " ";
++i;
}
return 0;
}

6
Example 3: Write a program to print the table of 10
using a while loop
#include<iostream>
using namespace std;
int main()
{
int t=1;
cout<<"Table of 10"<<endl;
while(t!=11)
{
cout<<"10 X "<<t<<"="<<10*t<<endl;
t=t+1;
}
return(0);
}

7
Example 2: Sum of Positive Numbers Only
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// take input from the user
cout << "Enter a number: ";
cin >> number;
while (number >= 0) {
// add all positive numbers
sum += number;
// take input again if the number is positive
cout << "Enter a number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
}
8
do...while Loop - Syntax

• The body of the loop is executed at first. Then the condition is


evaluated.
• If the condition evaluates to true, the body of the loop inside
the do statement is executed again.
• The condition is evaluated once again.
• If the condition evaluates to true, the body of the loop inside

the do statement is executed again.

• This process continues until the condition evaluates to false. Then the
loop stops. 9
Flowchart of do...while Loop

10
Example 5: Sum of Positive Numbers Only
#include <iostream>
using namespace std;
int main() {
int number = 0;
int sum = 0;
do {
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
}
11
Example 4: Display Numbers from 1 to 5
// C++ Program to print numbers from 1 to 5

#include <iostream>
using namespace std;
int main() {
int i = 1;
// do...while loop from 1 to 5
do {
cout << i << " ";
++i;
}
while (i <= 5);
return 0;
}

12
Infinite while Loop/ Do…While Loop

13
Loops
Counter-controlled Loops
• Depends on the value of a variable known as counter
variable. The value of the variable is incremented or
decremented in each iteration.
Example: for loop

Sentinel-Controlled Loops / Conditional loop


• A loop that terminates when something happens inside
the loop body indicating that loop should be exited. Also
known as conditional loops
Example: while and do while loops
Assignment 3 (10 Marks) Due next class
Explain operation type and associativity of each with an example
Excluding Bitwise Operators

15
for loop - Syntax

16
Example: for loop - Syntax

Initialization Test Condition


expression Increment expression

for (int j=0; j<10; j++)


cout << j * j <<endl;
cout << j*2 <<endl;
cout << j*j*j <<endl;

17
for loop – Flow Chart

18
Example 1: for loop
Printing Numbers From 1 to 5
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " ";
}
return 0;
}
19
Example 2: for loop
// C++ Program to display a text 5 times

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << "Hello World! " << endl;
}
return 0;
}
20
Example 3: for loop
\\Find the sum of first n Natural Numbers
#include <iostream>
using namespace std;
int main()
{
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i)
{
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;
}

21
Example 4: for loop
#include <iostream>
using namespace std;
int main()
{
int j;

for (j=0; j<10; j++)


cout << j * j <<endl;

return 0;
22
Example 5: for loop
- Get a number from user and calculate its factorial:

For any positive number n, it's factorial is given by:


• factorial = 1*2*3...*n
• Factorial of negative number cannot be found and
factorial of 0 is 1.
In this program below, user is asked to enter a
positive integer. Then the factorial of that number is
computed and displayed in the screen.

23
Cont…

24
Example: for loop
- Write a program that ask the user to enter a
number. The program should print the Cube of all
integers starting from 1 to the Number.
E.g.,
Enter a Number: 4
1 1
2 6
3 27
4 64
25
Cont….

26
for loop - Variable Visibility
int main()
{
int j;
for(j=0; j<10; j++) {
int k=0;
k = j*j;
cout<<“\nValue of k: “<<k;
}
// k = 23; Cannot do this!
} 27
for loop - Variable Visibility
int main()
{
Loop body
for(int j=0; j<5; j++)
cout<<“\nValue of j: “<<j;

cout<<“\nValue of j: “<<j; // ERROR


}

28
for loop – optional expressions
int j=0;
for(; j<10; j++)
cout<<“\nHello world“;
int j=0;
for(; j<10;)
{
cout<<“\nHello world“;
j++;
}

for(; ;) Infinite loop


(it never terminates)
cout<<“\nHello world“; 29
Infinite for loop
If the condition in a for loop is always true, it runs forever (until
memory is full).

For example,

In the above program, the condition is always true which will then run
the code for infinite times.

30
Break, continue and goto statement
• Break statement is • Goto statement is
used to terminate used to jump the
the loop execution control to
immediately a specific portion of
• Continue statement your protram
is used to skip a
looping iteration

31

You might also like