1 / 30

Introduction to Programming

Introduction to Programming. Lecture No. 8. In the last lecture. Loops While Do while For Operators Increment / Decrement Compound Assignment Operators. Example: Program to calculate the average marks of class. int sum; int students ; int average ; sum = 0 ; students = 0 ;

wendler
Download Presentation

Introduction to Programming

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. Introduction to Programming Lecture No. 8

  2. In the last lecture • Loops • While • Do while • For • Operators • Increment / Decrement • Compound Assignment Operators

  3. Example: Program to calculate the average marks of class int sum; int students ; int average ; sum = 0 ; students = 0 ; do { cin >> grade ; sum += grade ; students ++ ; } while (grade >= 0) ; average = sum / students ; cout << average ; A Flaw in the code

  4. Multi-way decision

  5. if Statements if ( grade ==‘A’ ) cout << “ Excellent ” ; if ( grade ==‘B’ ) cout << “ Very Good ” ; if ( grade ==‘C’ ) cout << “ Good ” ; if ( grade ==‘D’ ) cout << “ Poor ” ; if ( grade ==‘F’ ) cout << “ Fail ” ;

  6. if else if ( grade ==‘A’ ) cout << “ Excellent ” ; else if ( grade ==‘B’ ) cout << “ Very Good ” ; else if ( grade ==‘C’ ) cout << “ Good ” ; else if ( grade ==‘D’ ) cout << “ Poor ” ;

  7. if else if ( grade == ‘A’ ) cout << “ Excellent ” ; else if ( grade == ‘B’ ) … else if … … else …

  8. switch statement

  9. switch statements switch ( variable name ){ case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; …}

  10. switch statements switch ( grade){ case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … …}

  11. switch statements case ‘A’ : cout << “ Excellent ” ; … …

  12. Example switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : cout << “Good ” ; case ‘D’ : cout << “ Poor ” ; case ‘F’ : cout << “ Fail ” ; }

  13. break;

  14. Example switch ( grade ) { case ‘A’ : cout << “ Excellent ” ; break ; case ‘B’ : cout << “ Very Good ” ; break ; case ‘C’ : cout << “Good ” ; break ; case ‘D’ : cout << “ Poor ” ; break ; case ‘F’ : cout << “ Fail ” ; break ; }

  15. default : default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ;

  16. Flow Chart of switch statement switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..”

  17. if ( amount > 2335.09 ) statements ;

  18. Whole Number • short • int • long

  19. case ‘A’ : case ‘ 300 ‘ : case ‘ f ‘ :

  20. break ; if (c == ‘z’ ) { cout << “ Great ! You have made the correct guess “ ;break ; }

  21. continue ;

  22. continue while trynum <= 5 ; { …. …. continue ; }

  23. continue in ‘for’ loop for ( counter = 0 ;counter <= 10 ; counter ++ ) { ……. continue ; }

  24. What have we done till now … • Sequential Statements • Decisions • if , if else , switch • Loops • while , do while , for

  25. goto Unconditional Branch of Execution

  26. Structured Programming • Sequences • Decisions • Loops

  27. Minimize the use of break • Minimize the use of continue • Never use goto

  28. Guide lines for structured programming • Modular • Single entry - single exit

  29. Rules for Structured Flowchart Rule 1 : Use the simplest flowchart Rule 2 : Any rectangle can be replaced by two rectangles. Rule 3 : Any rectangle can be replaced with structured flowcharting constructs. Rule 4 : It says, rule 2 and rule 3 can be repeated as many times as needed

  30. Next Milestones • Data Structures • Arrays • Character Strings • Pointers

More Related