Upload
danghuong
View
222
Download
1
Embed Size (px)
Citation preview
Looping statement
It is also called a Repetitive control structure. Sometimes
we require a set of statements to be executed a number of
times by changing the value of one or more variables
each time to obtain a different result. This type of
program execution is called looping. C++ provides the following construct
while loop
do-while loop
for loop
While loop Syntax of while loop while(condition)
{
statement(s);
} The flow diagram indicates that a condition is first evaluated. If the condition
is true, the loop body is executed and the condition is re-evaluated. Hence, the
loop body is executed repeatedly as long as the condition remains true. As
soon as the condition becomes false, it comes out of the loop and goes to the
statement next to the ‘while’ loop.
Write a program to print DPSMIS 5 times by using While loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
while(i<=5)
{
cout<<"\nDPSMIS";
i++;
}
getch();
}
Write a program to print first 5 natural numbers by using While loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
while(i<=5)
{
cout<<i<<"\n";
i++;
}
getch();
}
Write a program to print all the even numbers between 1 to 10 by using while
loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=2;
while(i<=10)
{
cout<<i<<"\n";
i=i+2;
}
getch();
}
Write a program to print all the odd numbers between 10 to 1 using while loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=9;
while(i>=1)
{
cout<<i<<"\n";
i=i-2;
}
getch();
return 0;
}
Write a program to print the following series :
0 1 1 2 3 5 8 13 . . . upto 25 terms.
#include <iostream.h>
#include <conio.h>
int main()
{
Long int a=0,b=1,c,t=1;
cout<<a<<" "<<b;
while(t<=23)
{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
i++;
}
getch();
return 0;
}
Write a program to print the sum of digits of a given number.
#include <iostream.h>
#include <conio.h>
int main()
{
int num,a,sum=0;
cout<<"\nEnter the number\t\t\t";
cin>>num;
while(num>0)
{
a=num%10;
sum=sum+a;
num=num/10;
}
cout<<"\nThe sum of digits of the number is\t"<<sum;
getch();
return 0;
}
Write a program to reverse the number.
#include <iostream.h>
#include <conio.h>
int main()
{
int num,a,rev=0;
cout<<"\nEnter the number\t";
cin>>num;
while(num>0)
{
a=num%10;
rev=rev*10+a; // rev is a variable multiply by 10 for place value
num=num/10;
}
cout<<"\nThe Reverse number is\t"<<rev;
getch();
}
do-while loop Syntax of do-while loop do
{
statements;
} while (condition); Note : That the loop body is always executed at least once. One important
difference between the while loop and the do-while loop the relative ordering
of the conditional test and loop body execution. In the while loop, the loop
repetition test is performed before each execution the loop body; the loop body
is not executed at all if the initial test fail. In the do-while loop, the loop
termination test is Performed after each execution of the loop body. hence, the
loop body is always executed atleast once.
Write a program to print DPSMIS 5 times by using Do…While loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
do {
cout<<"\nDPSMIS";
i++;
}while(i<=5);
getch();
}
Write a program to print first 5 natural numbers by using Do-While loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=1;
do
{
cout<<i<<"\n";
i++;
}while(i<=5);
getch();
return 0;
}
Write a program to print all the even numbers between 1 to 10 by using
do…while loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=2;
do
{
cout<<i<<"\n";
i=i+2;
}while(i<=10);
getch();
return 0;
}
Write a program to print all the odd numbers between 10 to 1 by using
do…while loop.
#include <iostream.h>
#include <conio.h>
int main()
{
int i=9; //Variable ‘I’ is initialized with a value 9
do
{
cout<<i<<"\n";
i=i-2;
}while(i>=1);
getch();
return 0;
}
Write a program to print the following series :
0 1 1 2 3 5 8 13 . . . upto 25 terms.
#include <iostream.h>
#include <conio.h>
int main()
{
long int a=0,b=1,c,i=1;
cout<<a<<" "<<b;
do
{
c=a+b;
cout<<c<<" ";
a=b;
b=c;
i++;
}while(i<=23);
getch();
}
for loop It is a count controlled loop in the sense that the program knows in advance
how many times the loop is to be executed.
syntax of for loop for (initialization; decision; increment/decrement)
{
statement(s);
} The flow diagram indicates that in for loop three operations take place:
Initialization of loop control variable. Ex. int t=1;
Testing of loop control variable. Ex. (t<=10)
Update the loop control variable either by incrementing or
decrementing. Ex. t++;
If the TEST-condition is true, the program executes the body of the loop and
then the value of loop control variable is updated. Again it checks the TEST-
condition and so on. If the TEST-condition is false, it gets out of the loop. Write a program to display name five times.
#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=1; i<=5; i++)
cout<<”\nDPSMIS”;
getch();
return 0;
}
Write a program to find first 10 natural numbers.
#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
cout<<"\n"<<i;
getch();
}
Write a program to input all the even number between 1 to 10.
#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=2; i<=10; i+=2)
cout<<"\n"<<i;
getch();
}
Write a program to input all the odd numbers between 1 to 10.
#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(i=1; i<=10; i+=2)
cout<<"\n"<<i;
getch();
}
Write a program to find the sum of all the even numbers between 1 to 100.
#include <iostream.h>
#include <conio.h>
int main()
{
int i, sum=0;
for(i=2; i<=100; i+=2)
{
sum=sum+i;
}
cout<<"\nSum\t"<<sum;
getch();
}
Write a program to input a number and find its factorial.
#include <iostream.h>
#include <conio.h>
int main()
{
int i,n,fact=1;
cout<<"\nEnter the number\t\t";
cin>>n;
for(i=1; i<=n; i++)
{
fact=fact*i;
}
cout<<"\nFactorial of\t"<<n<<"\tis\t"<<fact;
getch();}
Write a program to find the sum of the following series :
1, 1/2, 1/3, . . . 1/15
#include <iostream.h>
#include <conio.h>
int main()
{
float sum=0,i;
for(i=1; i<=15; i++)
{
sum=sum+1/i;
}
cout<<"\nThe sum of the given series is\t"<<sum;
getch();
}
Write a program to display the given series :
0 1 1 2 3 5 8 13 . . . upto 25 terms.
#include <iostream.h>
#include <conio.h>
int main()
{
int a=0,b=1,c,i;
cout<<a<<" "<<b;
for(i=1; i<=23; i++)
{
c=a+b;
cout<<c<<" ";
a=b
b=c;
}
getch();
}
Print the following pattern :
#include <iostream.h>
#include <conio.h>
int main()
{
int i,n;
cout<<"\nEnter the value of n\t";
cin>>n;
for(i=1; i<=n; i++)
{
cout<<"*\n";
}
getch();
}
Nested loop:
It is a loop can be nested inside of another loop.
Syntax:
Nested for loop:
for ( init; condition; updation )
{
for ( init; condition; updation)
{
statement(s);
}
statement(s); // you can put more statements.
}
Print the following pattern :
#include <iostream.h>
#include <conio.h>
int main()
{
int r,c,i,j;
cout<<"\nHow many rows and columns ?\t";
cin>>r>>c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<"*";
cout<<"\n";
}
getch();
}
#include <iostream.h>
#include <conio.h>
int main()
{
int n,i,j;
cout<<"\nEnter the value of n\t";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n";
for(j=1;j<=i;j++)
cout<<"*";
cout<<"\n";
}
getch();
}
#include <iostream.h>
#include <conio.h>
int main()
{
int n,i,j;
cout<<"\nEnter the value of n\t";
cin>>n;
for(i=n;i>0;i--)
{
cout<<"\n";
for(j=i;j>0;j--)
cout<<"*";
cout<<"\n";
}
getch();
}
Loop Control Statements/ Jump Statements:
Control
Statement
Description Syntax:
break Terminates the loop or switch statement and
transfers execution to the statement
immediately following the loop or switch.
break;
continue Causes the loop to skip the remainder of its
body and immediately retest its condition
prior to reiterating.
continue;
goto
statement
It provides an unconditional jump from the
goto to a labeled statement in the same
function. A label is made of a valid identifier
followed by a colon(:).
goto label;
..
.
label: statement;
exit()
function
The execution of a program can be stopped at
any point with exit ( ) and a status code/value
can be informed to the calling program, where
code is an integer value. The code has a value 0
for correct execution. The value of the code
varies depending upon the operating system.
It requires <stdlib.h> header file.
exit (value) ;
Source Code:
Output
//break #include<iostream.h> #include<conio.h> int main() { clrscr(); for(int i=1;i<=10;i++) { if(i%4==0) break; cout<<i<<"\n"; } getch(); return 0; }
1 2 3
//continue #include<iostream.h> #include<conio.h> int main() { clrscr(); for(int i=1;i<=10;i++) { if(i%4==0) continue; cout<<i<<"\n"; } getch(); return 0; }
1 2 3 5 6 7 9 10
// break and continue #include<iostream.h> #include<conio.h> int main() { clrscr(); int x=0,y,sum=0; cout<<”Enter a number::”; cin>>y; while(1) { x+=1; if(x>y) break; if(y%x!=0) continue; sum=sum+x; }
Enter a number::10 Sum of factores:: 18
cout<<”Sum of factores::”<<sum; getch(); return 0; }
//goto statement #include<iostream.h> #include<conio.h> int main() { clrscr(); const int maxInput = 3; int i; double number, average, sum=0.0; for(i=1; i<=maxInput; i++) { cout<<"Enter a number: "; cin>>number; // for negative number, control moves to label jump if(number < 0.0) goto jump; sum += number; // sum = sum+number; } jump: average=sum/(i-1); cout<<"Sum = "<< sum<<endl; cout<<"Average "<<average; getch(); return 0; }
Enter a number:3 Enter a number:-1 Sum = 3 Average = 3
//exit function #include<iostream.h> #include<conio.h> #include<stdlib.h> // for exit() int main() { clrscr(); cout<<"executed"; exit(0); cout<<"program doesn’t exist"; getch(); return 0; }
executed