Decision Making & Looping: Program Loop: Control Statement
Decision Making & Looping: Program Loop: Control Statement
A sequence of statements are executed until some termination conditions satisfied. Program loop: body of loop. control statement -> tests certain conditions & then directs repeated execution of statements within the body of loop. Two types: Based on position of control statement. 1) Entry controlled loop: control are tested before the start of the loop. If false body will not be executed. 2) Exit controlled loop: test is performed at the end of the body. i.e body of loop executed at least once.
3/22/2013 CS&E Dept
False
True
3/22/2013
CS&E Dept
While statement
Basic format: while (test condition) { body of the loop } * Entry controlled loop statement. Test condition evaluated & if it is true, then body of the loop is executed. After execution, the test condition is again evaluated & if it is true, the body is executed again. This Procedure is repeated until test condition becomes false, & control transferred out of the loop. i.e body of loop may not executed if the condition is false at the very first attempt.
3/22/2013
CS&E Dept
Example
#include <iostream.h> void main() { int counter; counter =0; while (counter < 5) {
cout << "I love computer science << endl; counter = counter + 1;
}
3/22/2013 CS&E Dept
Example
#include <iostream.h> void main() { int counter; int sum; sum=0; //initialize sum counter=0; while (counter<100) { sum=sum+counter; counter = counter +1; } cout<<sum; }
3/22/2013 CS&E Dept
The do statement
General form: do { body of the loop } while (test condition); Exit controlled loop. After do statement, program executes the body of the Loop. At the end of the loop, the test condition in the while is evaluated. If it is true, body of the loop is executed once again & this process continues as long as the condition is true. When condition becomes false, the loop will be terminated. Body of the loop executed at least once. do while loop can be nested. 3/22/2013 CS&E Dept
Example
#include <iostream.h> void main() { int counter; int sum; sum=0; //initialize sum counter=0; do { sum=sum+counter; counter = counter +1; } while (counter<100) cout<<sum; }
3/22/2013 CS&E Dept
Explanation
1. The expression initial_value1 is evaluated, usually an assignment statement that sets a variable to a particular value. 2.The expression condition is evaluated. It is typically a relational expression. 3. If condition evaluates as false (zero), the for statement terminates and execution passes to the first statement following the for statement that is the next_statement. 4. If condition evaluates as true (non zero), the subsequent C++ statements are executed. (i.e body of the loop) 5. The expression increment is executed, and execution returns to step no. 2.
3/22/2013 CS&E Dept
Example
#include <iostream.h> void main() { int counter; int sum; sum=0; //initialize sum for(counter=0;counter<100;counter++) { sum=sum+counter; counter = counter +1; } cout<<sum; }
3/22/2013 CS&E Dept
Continued
4. It is also permissible to use expressions in the initialization & increment sections. Eg: for (x=(m+n)/2; x>0; x=x/2) 5. In for loop one or more sections can be omitted. Eg: i) m=5; for(;m!=50;) { cout<<m; m=m+5; } ii) for (;;) iii) for (i=1000;i<100;i=i-1) ; . iv) for (i=1000;i<100;i=i-1) ; no syntax error treated as null statement.
3/22/2013 CS&E Dept
.
} ..
.
} while() ..
for (.) for {. If(condition) break; Exit From loop . Exit From inner loop
CS&E Dept
{. for(..) { If(condition)
break;
} ..
} ..
}
..
3/22/2013
.
} ..
3/22/2013 CS&E Dept
.
} while() ..
. } ..
3/22/2013
CS&E Dept
#include<iostream.h> #include<conio.h> #include<math.h> //Sine of an angle upto given accuracy i.e |term|<accuracy void main() { int i=1; double sum,angle,term,accr; const double pi=22.0/7; clrscr(); cout<<"enter angle and accuracy"; cin>>angle>>accr; angle=angle*pi/180;term=angle;sum=angle; while(fabs(term)>accr) { term=-term*angle*angle/((2*i)*(2*i+1)); sum=sum+term; i++; }cout<<"\n the sine angle is "<<sum;cout<<"\n actual value is "<<sin(angle); getch(); CS&E Dept } 3/22/2013
#include<iostream.h> #include<conio.h> #include<math.h> //Cosine of a number upto given accuracy void main() { int i=0;const double pi=22.0/7; float sum,angle,term,accr;clrscr(); cout<<"\n enter angle and accuracy"; cin>>angle>>accr; angle=angle*pi/180; term=1;sum=1; while(fabs(term)>accr) { term=-term*angle*angle/((2*i+1)*(2*i+2)); sum=sum+term; i++; } cout<<"\n the cosine of angle is "<<sum; cout<<"\n actual value is "<<cos(angle); getch(); 3/22/2013 CS&E Dept }
#include<iostream.h> #include<conio.h> #define true 1 #define false 0 //To check whether the number is prime or not
void main() { int n,i,prime;clrscr(); cout<<"enter number";cin>>n; if(n==1) cout<<"\n the number is not prime"; else if ((n==2)||(n==3)) cout<<"\n the number is prime"; else { for(i=2;i<=n/2;i++) { prime = true; if(n%i==0) { prime = false; break; }}if(prime==true) cout<<"\n the number is prime";else cout<<"\n the number is not prime";}getch();}
3/22/2013 CS&E Dept
#include<iostream.h> #include<conio.h> //Generating prime numbers below the given limit void main() { int n,i,prime,j;clrscr(); cout<<"enter limit";cin>>n; for(i=1;i<=n;i++) { for(j=2;j<=i/2;j++) { prime =1; if(i%j==0) { prime = 0; break; } } if((prime==1)||(i==2)||(i==3)) cout<<"\n"<<i; }getch(); }
3/22/2013 CS&E Dept
#include<iostream.h> #include<conio.h> #define true 1 #define false 0 // Generate prime Fibonacci numbers void main() { int j,prime,num,i,f1,f2,fib; clrscr(); cout<<"number of fib nos"; cin>>num; cout<<"\n sequence is "; for(i=1,f1=1,f2=1,fib=0;i<=num;i++) { if ((f1==2)||(f1==3)) cout<<"\n"<<f1; else { for(j=2;j<=f1/2;j++)
3/22/2013 CS&E Dept
{ prime = true; if(f1%j==0) { prime = false; break; } } if(prime==true) cout<<"\n"<<f1; } fib=f1+f2; f1=f2; f2=fib; } getch(); }
3/22/2013
CS&E Dept
#include<iostream.h> #include<conio.h> #define true 1 #define false 0 //Generating prime numbers below the given limit void main() { int n,i,prime,j; clrscr(); cout<<"enter limit"; cin>>n;
3/22/2013 CS&E Dept
for(i=1;i<=n;i++) { for(j=2;j<=i/2;j++) { prime = true; if(i%j==0) { prime = false; break; } } if((prime==true)||(i==2)||(i==3)) cout<<"\n"<<i; } getch(); }
3/22/2013
CS&E Dept
#include<iostream.h> #include<conio.h> //Find average of m numbers until negative number is typed using type casting void main() { int m; float sum,x,avg; clrscr(); cout<<"enter value"; sum=0; for(m=1;m<=100;m++) { cin>>x; if(x<0) break; sum+=x; } avg=sum/(float)(m-1);cout<<"\n avg = "<<avg; getch(); 3/22/2013 CS&E Dept }