Lecture 8 - Lecture 8 Sept 25, 2002 Till now Introduction...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 8 Sept 25, 2002 Till now Introduction to computers Simple Programs using basic concepts like variables and data types, operators. Selection structures if, if/else, nested if /else and switch Repetition structures while contd today will be....for, do while for loop for (initialise count; test count; increment count) { do this; and this; } Example 1 #include<iostream.h> int main() { int p, n, count; float r, si; for (count=1; count<=3 ; count=count+1) { cout<<"Enter values of p ,n , r"<<endl; cin>>p>>n>>r; si = p*n*r/100; cout<<"Simple Interest = "<<si<<endl; } return 0; } // Calculation of Simple interest for 3 sets of p , n, r using for loop Explanation Setting a loop counter to an initial value. Testing the loop counter to determine whether its value has reached the number of repetitions desired. increasing the value of loop counter each time the program segment within the loop has been executed. Other ways of for loop Problem definition : Printing numbers from 1 to 10 in different ways 1st way int main() { int i; for ( i=1; i<=10; i=i+1) cout<<i<<endl; } 2nd way int main() { int i; for (i=1;i<=10;) { cout<<i<<endl; i=i+1; } return 0; } 3rd way int main() { int i=1; for (; i<=10; i+=1) cout<<i<<endl; } 4th way int main() { int i=1; for (; i<=10 ;) { cout<<i<<endl; i=i+1; } return 0; } Which way is best? Of all the 4 ways discussed, the 1st way is the best : It avoids confusion. It is a mark of good programming. Nesting of loops Just as if statements can be nested even while and for can be nested int main() output: { 1 1 2 int r, c, sum; 1 2 3 for (r=1; r<=3; r++) // outer 2 1 3 { 2 2 4 for (c=1;c<=2; c++) //inner 3 1 4 { 3 2 5 sum=r+c; cout<<r<<" "<<c<<" "<<sum<<endl; } } return 0; } Imp note about for loop Multiple initialization in for loop for ( i=1, j=2; j<=10; j++) Multiple initializations can be made in assignment and (incrementation, decementation) expressions of for loop but always separated by commas but only one condition can be checked always. The odd loop ( use of sentinal ) int main() { char another =`y'; // y is the sentinal used to stop loop operation int num; while (another==` y ') // use of equality operator { cout<<"Enter a number"<<endl; cin>>num; cout<<num*num<<endl; cout<<"Want to enter another number y/n"<<endl; cin>>another; } return 0; } The do while loop do { this; and this; and this; } while ( this condition is true ) Diff b/w while and do while In while condition is tested at the beginning In do while condition is tested at the end The loop will always be executed once in the case of a do while loop while illustration while ( 4<1) { cout<<"Hello there"<<endl; } cout not executed do while illustration do { cout<<"Hello there"<<endl; } while (4<1) break keyword int main() { int i, j; for (i=1;i<=100;i++) { for (j=1;j<=200;j++) { if (j==150) break; // break keyword else cout<<i<<j<<endl; } } return 0; } // when break is encountered, control is passed to first statement after the loop. The control breaks away from the loop. Continue keyword for ( i=1; i<=2; i++) output: { 1 2 for (j=1; j<=2; j++) 2 1 { if ( i==j) continue; else cout<<i<<j<<endl; } } // when continue is encountered inside a loop, control automatically passes to beginning of loop. ...
View Full Document

Ask a homework question - tutors are online