1 / 28

Programming Fundamentals

Programming Fundamentals. Lecture 6. while loop. while (condition) { statements; : } statements;. While loop executes zero or more times. What if we want the loop to execute at least one time?. do-while. Do while loop execute one or more times. Syntax of do-while loop.

mili
Download Presentation

Programming Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Fundamentals Lecture 6

  2. while loop while (condition) { statements; : } statements;

  3. While loop executes zero or more times. What if we want the loop to execute at least one time?

  4. do-while

  5. Do while loop execute one or more times

  6. Syntax of do-while loop do { statements ; } while ( condition ) ;

  7. { int counter; counter=1; do { cout<<counter; cout<<endl; counter++; } } while(counter<=10)

  8. intn,table; cout<<"enter value for table"; cin>>table; n=1; do { cout<<n<<"x"<<table<<"="<<table*n<<endl; n++; • }while(n<=10); }

  9. All the programs that we did from while loop do it using do while

  10. Example-Guessing game char c ; inttryNum = 1 ; do { cout << "Please enter your guess by pressing a character key from a to z “ ; cin >> c ; if ( c == 'z‘ ) { cout << "Congratulations! you guessed the right answer“ ; tryNum = 6 ; } else tryNum = tryNum + 1 ; } while ( tryNum <= 5 ) ;

  11. Flow chart for do-while loop

  12. Relational Operators char c ; inttryNum , maxTries ; tryNum = 1 ; maxTries = 5 ; cout << "Guess the alphabet between a to z “ ; cin >> c ; while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) { cout << "Guess the alphabet between a to z “ ; cin >> c ; tryNum = tryNum + 1 ; }

  13. for Loop

  14. For loop for ( initialization condition ; termination condition ; increment condition ) { statement ( s ) ; }

  15. Example int counter ; for( counter = 0 ; counter < 10 ; counter = counter + 1 ) cout << counter; Output 0123456789

  16. Program #include <iostream.h> void main(void) { int num; cout << “Number Number Squared\n"; cout << "-------------------------\n"; for (num = 1; num <= 10; num++) cout << num << "\t\t" << (num * num) << endl; }

  17. Table Int tab, c, r; Cout<<“enter any value”; Cin>>tab; For(c=1; c<=10; c++) { R=tab*c; Cout<<tab<<“x”<<c<<“=”<<r<<endl; }

  18. Sum of first 10 odd num Int n, sum; Sum=0; For(n=1; n<=10; n+=2) { Sum=sum+n; Cout<<n<<endl; } Cout<<“sum=”<<sum<<endl;

  19. Factorial { inta,fac,num; cout<<”Enter any Number: ”; cin>>num; fac=1; for(a=num;a>=1;a--) { fac=fac*a; } cout<<”Factorial=”<<fac<<endl; }

  20. { intu,i; for(u=1;u<=5;u++) { for(i=1;i<=u;i++) cout<<i<<”\t”; cout<<endl; } }

  21. Nested Loops • A loop that is inside another loop is called a nested loop.

  22. { inti,j; for(i=1;i<=4;i++) { for(j=1;j<=10;j++) cout<<'*'; cout<<endl; } }

  23. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

  24. intu,i; for(u=1;u<=5;u++) { for(i=1; i<=u ; i++) { cout<<i<<“\t”; } cout<<endl; }

More Related