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

Lecture 5_Loops

The document provides an overview of loop constructs in C++, detailing the types of loops such as for, while, and do loops. It explains counter-controlled and sentinel-controlled loops, along with syntax examples and exercises for calculating factorials and cubes. Additionally, it includes example programs demonstrating the use of while loops for character input and counting operations.

Uploaded by

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

Lecture 5_Loops

The document provides an overview of loop constructs in C++, detailing the types of loops such as for, while, and do loops. It explains counter-controlled and sentinel-controlled loops, along with syntax examples and exercises for calculating factorials and cubes. Additionally, it includes example programs demonstrating the use of while loops for character input and counting operations.

Uploaded by

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

Loop constructs

Dr. Sana Aurangzeb

[email protected]
Loops
• Loops cause a section of your program
to be repeated a certain number of
times
• Repeats until the condition remains true
• Terminates when the condition becomes
false
Loops in C++
1. for loop
2. while loop
3. do loop
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 loops
(1) for loop
#include <iostream>
using namespace std;
int main()
{
int j;

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


cout << “Pakistan”<<endl;
}
return 0;
}
(1) for loop
#include <iostream>
using namespace std;
int main()
{
int j;

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


cout << j * j <<endl;

return 0;
}
(1) for loop - Syntax

Initialization Test Condition


expression Increment expression

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

cout << Pakistan <<endl;


(1) 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;
(for loop) -- Class Exercise-1
- Get a number form user and calculate its factorial
(for loop) -- Class Exercise-1
- Get a number form user and calculate its factorial
void main(){
int i, n, factorial = 1;
cout<<"Enter a positive integer: ";
cin>>n;
for (i = 1; i <= n; ++i) {
factorial *= i; // factorial = factorial * i;
}
cout<< "Factorial of "<<n<<" = "<<factorial;
}
(for loop) -- Class Exercise-2
- 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 8
3 27
4 64
(for loop) -- Class Exercise-2
int i, num, cube;
cout << "\n\n Display the cube of the numbers upto a
given integer:\n";
cout << "Input the number of terms : ";
cin >> num;
for (i = 1; i <= num; i++) {
cube = i * i * i;
cout << "Number is : " << i << " and the cube of " << i
<< " is: " << cube << endl;
}
for loop – Multiple Expressions

Multiple Initialization Multiple Increment/Dec


Only one
expressions expressions
Test Condition

for (int j=0, k=9; j<10; j++,k--)


cout << j * j <<endl;
cout<< k*k <<endl;
(1) for loop - Variable Visibility
void 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!
}
(1) 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“;
(1) for loop - Variable Visibility
void main()
{
Loop body
for(int j=0; j<5; j++)
cout<<“\nValue of j: “<<j;

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


}
while loop
• for loop does something a fixed number of times.

• If you don’t know how many times you want to do


something before you start the loop?

• In this case a different kind of loop may be used:


the while loop
while loop - syntax
Loop body contain
single statement

Loop body contain


Multiple statement
Example: Tracing a while Loop
Initialize count

int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
(count < 2) is true
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop

int count = 0;
Print “Welcome to C++”
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop

int count = 0;
while (count < 2)
{ Increase count by 1
count is 1 now
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
(count < 2) is still true since
int count = 0; count is 1

while (count < 2)


{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop

int count = 0;
while (count < 2) Print “Welcome to C++”
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop

int count = 0;
while (count < 2)
Increase count by 1
{ count is 2 now

cout << "Welcome to C++!";


count++;
}
Example: Tracing a while Loop
(count < 2) is false since count is 2
int count = 0; now

while (count < 2)


{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop

int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++; The loop exits. Execute the next
statement after the loop.
}
Example Program
-Writea program to print character entered by user,
terminate the program when ‘z’ is pressed.
Example Program
-Writea program to print character entered by user,
terminate the program when ‘z’ is pressed.

#include <iostream>
using namespace std;

int main( )
{
char ch='0';
cout<<"Enter characters, z to terminate..\n";
while(ch!='z'){
cin>>ch;
}
cout<<“Program ended…”
}
(while loop) -- Class Exercise-1
-Writea program to get characters from the user. In the
end of the program should count total characters
(entered by the user). The program should stop taking
input when 0 is pressed.
(while loop) -- Class Exercise-1
#include <iostream>
using namespace std;
int main(){
char ch;
int count=0;
cout<<"Enter characters, 0 to terminate..\n";
while(ch!='0'){
cin>>ch;
count++;
}
cout<<"total characters entered: "<<count;
cout<<"Program ended…";
}
(while loop) -- Class Exercise-3
- Writea program that inputs a value in an integer number
from user. For this number the program returns the count
for how many times can we divide this number by 2 to
get down to 1”.

int count = 0; int num; cin>>num;


//count how many divisions we've done
while (num > 1)
{
num = num / 2;
count++;
}
cout<<“\nWe have to divide: “<<count<<“ times”;
OR
while (num %2==0)
{
num = num / 2;
count++;
}
cout << "\nWe can divide : " << count << " times";
Self Test
• int count = 3;
while (count-- > 0)
cout << count << " ";
• int count = 3;
while (--count > 0)
cout << count << " ";

You might also like