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

Lecture 6 Loops

Uploaded by

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

Lecture 6 Loops

Uploaded by

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

C++

LOOPS

Lecture 6
Display your Name
Loops

• Loops cause a section of program to be repeated


certain number of times.
• As long as condition remains true, the repetition
continues, when the condition becomes false, the
loop ends and the control passes to the statement
following the loop.
There are three kinds of loops in C/C++.
i) for loop
ii) while loop
iii) do while loop
The for loop

• The for loop executes a section of code a fixed number


of times.
• “for loop” is used normally when we know, before entering
the loop, that how many times we want to execute the
code.
For loop (syntax)

START STOP JUMP

for ( init; condition; increment )


{
statement(s);
}
WORK THAT YOU WANT TO PERFORM
• for(int i=1 ; i<=10; i=i+1)
•{
• cout<<“my name”;

•}
PRINT TABLE OF 2

for(int i=1;i<=10;i++)
{
cout<<2*i;
}
for(int i=0;i<=20;i=i+2)
{
cout<<i;
}
HOME TASK

• Take the value from the user and print its table.
• Take values from 1 to 5 and perform their addition.
Display your name 10 times
using for loop
Example

#include <iostream>
using namespace std;
int main ()
{ // for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
cout << "value of a: " << a << endl;
}
return 0;
}
For live demo click
https://www.tutorialspoint.com/compile_cpp_online.php
Sum of numbers using for loop
#include <iostream>
using namespace std;
int main(void)
{
int sum=0;
int num=1;
for (int i=0; i<5; i++)
{
sum+=num;
cout<<" Sum is "<< sum<<"\n";
}
}
Sum of numbers using for loop
#include <iostream>
using namespace std;
int main(void)
{
int sum=0;
int num=1;
for (int i=0; i=5; i++)
{
sum+=num;
cout<<" Sum is "<< sum<<"\n";
}
}
Task

Get the sum of five numbers by taking the numbers from the
user.
The for loop
#include <iostream.h>
Output
#include <conio.h> 1 10
void main(void) 29
{ clrscr();
int sum=0; 38
for(int a=1;a<=10;a++)
{ 47
cout<<a<<"\t"<<(11-a)<<endl;
sum += a;
56
} 65
cout<<"Sum is "<<sum;
getch(); 74
83
}

92
10 1
Sum is 55
Display elements of array using for loop

#include <iostream>
using namespace std;
int main()
{
int arr[]={21,9,56,99, 202};
/* set the value of variable i to 0 as the array
index starts with 0 */
for(int i=0; i<5; i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
Operation of for loop
#include <iostream>
int main()
{
for ( int counter = 10; counter >= 0; counter-- )
cout << counter << " \n";
}
Display 2 table using for loop
Factorial !
#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int num, fact=1;
cout<<"Enter a number ";
cin>>num;
for(int a=num;a>0;a--)
{
fact *= a;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
}
TASK

a.Vary the control variable from 100 down to 1 in increments of -1 .


b.Vary the control variable from 7 to 77 in 7 steps.
c.Vary the control variable from 20 down to 2 in steps of -2.
d.Vary the control variable over the following sequence of values: 2, 5,
8, 11, 14, 17, 20.
e.Vary the control variable over the following sequence of values: 99,
88, 77, 66`, 55, 44, 33, 22, 11, 0.
The while loop

• Normally in “for loop” we have an idea that


how many times we want to execute a section
of code
• but “while loop” is used when even before
starting the loop we have no idea that how
many times a section of code will be
executed.
• Like for loop, while loop contains a test
expression but there is no initialization or
increment/decrement expression etc.
The while loop

Output
#include <iostream.h> 1 10
#include <conio.h> 29
38
void main(void) 47
{ clrscr(); 56
65
int a=1;
74
while(a<=10) 83
{ 92
cout<<a<<"\t"<<(11-a)<<endl; 10 1
a++;
}
getch();
}
Operation of while loop
The while loop

#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int a=0;
while(a!=100)
{
cout<<a<<"\t"<<(100-a)<<endl;
a=a+25;
}
getch();
}
The while loop

#include<iostream.h>
#include<conio.h>
void main(void)
{ clrscr();
int a=15;
while(a<1 || a>10)
{
cout<<"Enter a value (1-10) ";
cin>>a;
}
cout<<"Value entered is between 1 - 10";
getch();
}
The do while loop

• The do while loop is used when we want to guarantee that


the loop body should execute at least once, whatever the
initial state of the test expression contains.
• In do while loop, the test expression is placed at the end of
the loop.
Output
The do while loop Value Square Cube
1 1 1
#include<conio.h> 2 4 8
3 9 27
void main(void) 4 16 64
{ clrscr(); 5 25 125
6 36 216
int a=1; 7 49 343
cout<<"Value\tSquare\tCube"<<endl; 8 64 512
do 9 81 729
{ 10 100 1000
cout<<a<<"\t"<<a*a<<"\t"<<a*a*a<<endl;
a++;

}while(a<=10);

getch();
}
Operation of do while loop
Output
Enter Table value 4
The do
#include<iostream.h>
while loop 4x1=4
4x2=8
#include<conio.h> 4 x 3 = 12
4 x 4 = 16
void main(void) 4 x 5 = 20
4 x 6 = 24
{ char ch; int t; 4 x 7 = 28
4 x 8 = 32
do 4 x 9 = 36
{ clrscr(); 4 x 10 = 40
Continue n
cout<<"Enter Table value "; Thanks for using program
cin>>t;
for(int a=1;a<=10;a++)
cout<<t<<" x "<<a<<" = "<<t*a<<endl;
cout<<“Press Y to Continue ";
ch=getche();
}while(ch=='Y' || ch=='y');
cout<<"\nThanks for using program";
}
Output
Enter Table value 4
4x1=4
4x2=8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Continue n
Thanks for using program

You might also like