You are on page 1of 6

Sarhad University , Peshawar.

(Assignment #2)
Subject:- Programming g Fundamentals
Topic: Loops in C++
Submitted to: Sir Amir Shehzad
Submitted by: Mohibullah (016-A)

Date: 15 Dec, 2019

Department of Software Engineering,


Sarhad University, Peshawar.
C++ while and do...while Loop
Loops are used in programming to repeat a specific block of code.
In computer programming, loop repeats a certain block of code until some
end condition is met.
There are 3 type of loops in C++ Programming:
 for Loop
 while Loop
 do...while Loop

C++ for Loop Syntax
for(initialization Statement; conditions;
update Statement) {
// Statement 1
// Statement 2
..
..
// Statement n
}
How for loop works?

 The initialization statement is executed


only once at the beginning.

 Then, the test expression is evaluated.

 If the test expression is false, for loop is


terminated. But if the test expression is
true, codes inside body of for loop is
executed and update expression is
updated.

 Again, the test expression is evaluated


and this process repeats until the test expression is false
Example 1:- Program to Print odd Numbers between 1 to 20.
#include <iostream>
using namespace std;
int main()
{
for (int num =1;num < 20;num+=2) {
cout << "Odd Number: " << num << endl;
}
return 0;
}

Example 2: Program to Find Sum of first 5 Natural Numbers.


#include <iostream>
using namespace std;
int main()
{
int n = 10, sum = 0;
for (int i = 1; i <= n; ++i) {
sum =sum+ i;
}
cout << "Sum = " << sum;
return 0;
}
C++ while Loop syntax
The syntax of a while loop is:
while (Conditions)
{
// Statement 1
// Statement 2
………………..
………………..
// Statement n;
}
How while loop works?
 The while loop evaluates the test expression.
 If the test expression is true, codes inside the body
of while loop is evaluated.
 Then, the test expression is evaluated again. This
process goes on until the test expression is false.
 When the test expression is false, while loop is terminated

Example 1:- Print a Message 10 times.


#include <iostream>
using namespace std;
int main()
{
int count = 1;
while (count < 10) {
cout << count<<" : Happy Programming" << endl;
count= count+ 1;
}
return 0;
}
Example 2 :- Program to print first 100 natural numbers.
#include <iostream>
using namespace std;
int main()
{
int num = 1;
while (num < =100) {
cout << "Number: " << num << endl;
num = num + 1;
}
return 0;
}

C++ do...while Loop


The do...while loop is a variant of the while loop with one
important difference. The body of do...while loop is
executed once before the test expression is checked.
The syntax of do..while loop is:
do {
// codes;
}
while (testExpression)

How do...while loop works?


 The codes inside the body of loop is executed at least
once. Then, only the test expression is checked.
 If the test expression is true, the body of loop is
executed. This process continues until the test
expression becomes false.
 When the test expression is false, do...while loop is
terminated.
Example 1: Program to print first 100 natural numbers.
#include<iostream>
using namespace std;
int main(){
int i;
i=1;
do{
cout<<i<<endl;
i++;
}while(i<=100);
return 0;
}
Example 2:- Program will terminates if user entered negative number.

#include <iostream>
using namespace std;
int main()
{
int a ;
do {
cout<<"Enter A positive Integer: ";
cin>>a;
cout <<"You Entered "<< a << endl;
} while (a > 0);
return 0;
}

You might also like