Docsity

Programming Fundamentals: Loops, Operators, Decision Statements, and Switch Statements, Slides of Computer Programming

Partial preview of the text

Download Programming Fundamentals: Loops, Operators, Decision Statements, and Switch Statements and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture No. 8

๏ฎ Loops

  • While
  • Do while
  • For

๏ฎ Operators

  • Increment / Decrement
  • Compound Assignment Operators

In the last lecture

Multi-way decision

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 โ€ ;

if Statements

if ( grade == โ€˜Aโ€™ )

cout << โ€œ Excellent โ€ ;

else if ( grade == โ€˜Bโ€™ )

โ€ฆ

else if โ€ฆ

โ€ฆ

else โ€ฆ

if else

switch statement

switch ( grade) { case โ€˜Aโ€™ : cout << โ€œ Excellent โ€ ; case โ€˜Bโ€™ : cout << โ€œ Very Good โ€ ; case โ€˜Cโ€™ : โ€ฆ โ€ฆ }

switch statements

case โ€˜Aโ€™ :

cout << โ€œ Excellent โ€ ;

switch statements

break;

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 ; }

switch (grade)

Display โ€œExcellentโ€ case โ€˜Bโ€™ :

case โ€˜Aโ€™ :

Display โ€œVery Goodโ€

Default :

โ€œโ€ฆโ€ฆ..โ€

Flow Chart of switch statement

โ€ฆ

if ( amount > 2335.09 )

statements ;

case โ€˜Aโ€™ :

case โ€˜ 300 โ€˜ :

case โ€˜ f โ€˜ :

if (c == โ€˜zโ€™ )

{ cout << โ€œ Great! You have made the correct guess โ€œ ; break ; }

break ;