Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 Sept 25, 2002. Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types,

Similar presentations


Presentation on theme: "Lecture 8 Sept 25, 2002. Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types,"— Presentation transcript:

1 Lecture 8 Sept 25, 2002

2 Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types, operators. ► S► S► S► Selection structures- if, if/else, nested if /else and switch ► R► R► R► Repetition structures – while contd today will be….for, do while

3 for loop ► for (initialise count; test count; increment count) { do this; do this; and this; and this; }

4 Example 1 ► // Calculation of Simple interest for 3 sets of p, n, r using for loop #include<iostream.h> int main() { int p, n, count; int p, n, count; float r, si; float r, si; for (count=1; count<=3 ; count=count+1) for (count=1; count<=3 ; count=count+1) { cout<<“Enter values of p,n, r”<<endl; cout<<“Enter values of p,n, r”<<endl; cin>>p>>n>>r; cin>>p>>n>>r; si = p*n*r/100; si = p*n*r/100; cout<<“Simple Interest = “<<si<<endl; cout<<“Simple Interest = “<<si<<endl; } return 0; return 0; }

5 Explanation ► S► S► S► Setting a loop counter to an initial value. ► T► T► T► Testing the loop counter to determine whether its value has reached the number of repetitions desired. ► i► i► i► increasing the value of loop counter each time the program segment within the loop has been executed.

6 Other ways of for loop ► Problem definition : Printing numbers from 1 to 10 in different ways ► 1st way int main() int main() { int i; int i; for ( i=1; i<=10; i=i+1) for ( i=1; i<=10; i=i+1) cout<<i<<endl; cout<<i<<endl; }

7 2 nd way ► int main() { int i; int i; for (i=1;i<=10;) for (i=1;i<=10;) { cout<<i<<endl; cout<<i<<endl; i=i+1; i=i+1; } return 0; return 0; }

8 3 rd way ► int main() { int i=1; int i=1; for (; i<=10; i+=1) for (; i<=10; i+=1) cout<<i<<endl; cout<<i<<endl; }

9 4 th way ► int main() { int i=1; int i=1; for (; i<=10 ;) for (; i<=10 ;) { cout<<i<<endl; cout<<i<<endl; i=i+1; i=i+1; } return 0; return 0; }

10 Which way is best? ► Of all the 4 ways discussed, the 1 st way is the best : ► It avoids confusion. ► It is a mark of good programming.

11 Nesting of loops ► Just as if statements can be nested even while and for can be nested int main()output: int main()output: {1 1 2 {1 1 2 int r, c, sum;1 2 3 int r, c, sum;1 2 3 for (r=1; r<=3; r++) // outer2 1 3 for (r=1; r<=3; r++) // outer2 1 3 {2 2 4 {2 2 4 for (c=1;c<=2; c++) //inner3 1 4 for (c=1;c<=2; c++) //inner3 1 4 {3 2 5 {3 2 5 sum=r+c; sum=r+c; cout<<r<<“ “<<c<<“ “<<sum<<endl; cout<<r<<“ “<<c<<“ “<<sum<<endl; } } return 0; return 0; }

12 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.

13 The odd loop ( use of sentinal ) ► int main() { char another =‘y’; // y is the sentinal used to stop loop operation char another =‘y’; // y is the sentinal used to stop loop operation int num; int num; while (another==‘ y ’) // use of equality operator while (another==‘ y ’) // use of equality operator { cout<<“Enter a number”<<endl; cout<<“Enter a number”<<endl; cin>>num; cin>>num; cout<<num*num<<endl; cout<<num*num<<endl; cout<<“Want to enter another number y/n”<<endl; cout<<“Want to enter another number y/n”<<endl; cin>>another; cin>>another; } return 0; return 0; }

14 The do – while loop ► do { this; this; and this; and this; } while ( this condition is true ) } while ( this condition is true )

15 Diff b/w while and do while ► I► I► I► In while condition is tested at the beginning ► I► I► I► In do while condition is tested at the end ► T► T► T► The loop will always be executed once in the case of a do while loop

16 ► while illustration while ( 4<1) while ( 4<1) { cout<<“Hello there”<<endl; cout<<“Hello there”<<endl; } cout not executed cout not executed

17 ► do while illustration do do { cout<<“Hello there”<<endl; cout<<“Hello there”<<endl; } while (4<1) } while (4<1)

18 break keyword ► int main() { int i, j; int i, j; for (i=1;i<=100;i++) for (i=1;i<=100;i++) { for (j=1;j<=200;j++) for (j=1;j<=200;j++) { if (j==150) if (j==150) break; // break keyword break; // break keyword else else cout<<i<<j<<endl; cout<<i<<j<<endl; } } return 0; return 0; } // when break is encountered, control is passed to first statement after the loop. The control breaks away from the loop. } // when break is encountered, control is passed to first statement after the loop. The control breaks away from the loop.

19 Continue keyword ► for ( i=1; i<=2; i++) output: {1 2 {1 2 for (j=1; j<=2; j++)2 1 for (j=1; j<=2; j++)2 1 { if ( i==j) if ( i==j) continue; continue; else else cout<<i<<j<<endl; cout<<i<<j<<endl; } } // when continue is encountered inside a loop, control automatically passes to beginning of loop. } // when continue is encountered inside a loop, control automatically passes to beginning of loop.


Download ppt "Lecture 8 Sept 25, 2002. Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types,"

Similar presentations


Ads by Google