Download presentation
Presentation is loading. Please wait.
1
Thursday, December 21, 2006 The Briggs/Chase Law of Program Development: To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.
2
§Friday classes §Assignment 1 on website §Late Assignment Policy §Quiz 1 has been marked
3
Fibonacci numbers F1=1 F2=2 Fn=Fn-1 + Fn-2 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 …
4
int i; double Fn, Fnminus2=1, Fnminus1=2; for (i=0; i<100; i++){ Fn=Fnminus1 + Fnminus2; cout<<Fn<<" "; //updating values below Fnminus2=Fnminus1; Fnminus1=Fn; }
5
The for loop int main() { int x; for(x=0; x != 123; ) { cout << "Enter a number: "; cin >> x; } return 0; }
6
The for loop int x = 0; for( ; x<10; ) { cout << x << " " ; ++x; } int x; for (x=0; x<10; x++) { cout << x << " " ; }
7
for loop Self test exercise: Write a function to calculate the factorial of a number. 4! = 24 5! = 120 6! = 720 7! =5040 8!=40320
8
Print 30 stars on the screen ******************************
9
Print 30 stars on the screen int i; for (i=0; i<30; i++) { cout<<"*"; } cout<<endl;
10
Print 20 lines of 30 stars on screen ******************************
11
Print 20 lines of 30 stars on screen int i,j; for (j=0; j<20; j++) { for(i=0; i<30; i++) { cout<<"*"; } cout<<endl; } cout<<endl;
12
Nested Loops int main() { int i, j; for(i=0; i<3; i++) { for(j=0; j <= 4; j++) cout<<“The value of i=” <<i<<“ and j= ”<<j<<“\n”; cout<<"Out of the inner for-loop\n"; } return 0; }
13
Nested Loops The value of i=0 and j=0 The value of i=0 and j=1 The value of i=0 and j=2 The value of i=0 and j=3 The value of i=0 and j=4 Out of the inner for-loop The value of i=1 and j=0 The value of i=1 and j=1 The value of i=1 and j=2 The value of i=1 and j=3 The value of i=1 and j=4 Out of the inner for-loop The value of i=2 and j=0 The value of i=2 and j=1 The value of i=2 and j=2 The value of i=2 and j=3 The value of i=2 and j=4 Out of the inner for-loop
14
Print this pattern on screen * ** *** **** ***** ****** ******* ******** ********* ********** *********** ************ ************* ************** *************** **************** ***************** ****************** ******************* ********************
15
Print this pattern on screen int i,j; for (j=0; j<20; j++) { for(i=0; i<(j+1); i++) { cout<<"*"; } cout<<endl; } cout<<endl;
16
SELF TEST: Print this pattern on screen ******************** ******************* ****************** ***************** **************** *************** ************** ************* ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** *
17
Do-while loops Do-while loops are very much like while loops, except that the loop body is executed once before the loop condition is ever evaluated. After that, they are exactly the same.
18
Do-while loops Example: do { cout << "This is a test"; } while (0); –Compare this to: while (0) { cout << "This is a test"; }
19
char ans; do { cout << "Hello\n"; cout << "Do you want another greeting?\n" << "Press y for yes, n for no,\n" << "and then press return: "; cin >> ans; } while (ans == 'y' || ans == 'Y'); cout << "Good-Bye\n";
20
int secret=3, guess; char answer; do { cout<<"Guess a number between 0 and 10 "; cin>>guess; if (guess < secret) cout<<"Sorry, Too Small!"<<endl; else if (guess > secret) cout<<"Sorry, Too Big!"<<endl; else cout<<"Congratulations! Your guess is right!"<<endl; cout<<"Do you want to play again?\n"; cin>>answer; }while(answer=='y' || answer=='Y');
21
int secret=3, guess; char answer; do { cout<<"Guess a number between 0 and 10 "; cin>>guess; while((guess 10)) { cout<<"Out of range. Enter number between 0 and 10"; cin>>guess; } if (guess < 3) cout<<"Sorry, Too Small!"<<endl; else if (guess > 3) cout<<"Sorry, Too Big!"<<endl; else cout<<"Congratulations! Your guess is right!"<<endl; cout<<"Do you want to play again?\n"; cin>>answer; }while(answer=='y' || answer=='Y');
22
continue statement int i; for (i=0; i<50; i++){ if ((i==4)||(i==42)) continue; cout<<i<<"\n"; } In for-loop a continue statement will cause the increment part to be performed and then conditional expression to be executed.
23
break statement int i, j=15; for (i=0; i<50; i++){ if((i*2)==(j+3)) break; else { cout<<i<<" "; }
24
break statement int i, j=15; for (i=0; i<50; i++){ if((i*2)==(j+3)) break; else { cout<<i<<" "; } //prints 0 1 2 3 4 5 6 7 8
25
continue int main() { int x; for(x=0; x<=10; x++) { if(x%2) continue; cout << x << " "; } return 0; } //output?
26
continue int main() { int x; for(x=0; x<=10; x++) { if(x%2) continue; cout << x << " " ; } return 0; } //output? 0 2 4 6 8 10
27
Self Test: continue int loop ; for( loop=2; loop<10; ++loop ) {if (loop%3==0) continue; cout << loop << " "; } cout<<loop<<endl; int loop ; for( loop=2; loop<10; ++loop ) {if (loop%3) continue; cout << loop << " "; } cout<<loop<<endl;
28
Self Test: continue What happens here? int i, s=0 ; for( i=1; i<5; ) { continue; cout<<i<<endl; }
29
Self Test: continue int i=0; while (i<10){ i++; if (i==5) continue; cout<<i; }
30
Self Test: break break forces immediate exit from loop int main() { int t; // what is the output? for (t=0; t<100; t++) { if(t==10) break; cout << t << “ “; } return 0; }
31
break int i; for( i=1; ; ++i ) {if (i==5) continue; if (i%9 ==0) break; cout<<i; } cout<<"\n"<<i;
32
break int i; for( i=1; ; ++i ) {if (i==5) continue; if (i%9 ==0) break; cout<<i; } cout<<"\n"<<i; //prints 9 1234678 9
33
§continue and break inside loops only.
34
char a,b; cin>>a>>b; cout<<a<<b<<endl; cout<<a<<" "<<b<<endl;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.