1 / 16

Control Structures: Selection Statement

Control Structures: Selection Statement. Chapter 3 B. Ramamurthy. Introduction. “ Two roads divulged in a yellow wood, And sorry I could not travel both….” Robert Frost From “The Road Not Taken”. no. yes. Structured Programming. Sequence Selection Repetition. no.

lulu
Télécharger la présentation

Control Structures: Selection Statement

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. Control Structures: Selection Statement Chapter 3 B. Ramamurthy B.Ramamurthy

  2. Introduction “Two roads divulged in a yellow wood, And sorry I could not travel both….” Robert Frost From “The Road Not Taken” B.Ramamurthy

  3. no yes Structured Programming • Sequence • Selection • Repetition no yes B.Ramamurthy

  4. Decision Statements (selection) • How to compare data values? Relational operators • How to alter the sequence of program execution based on the result? if.. else statements • How to deal with multiple choices? Switch statement B.Ramamurthy

  5. Example • We will use “assigning letter grade based on percentage points” as an example to illustrate selection statement. B.Ramamurthy

  6. Relational Operators Operator Meaning < Less than ? > Greater than ? >= Greater than or equal to? <= Less than or equal to? == Equal to? != Not Equal to ?

  7. Logical Operators • ! not • && and • || or • These operators are used to combine more than one condition forming a complex condition. B.Ramamurthy

  8. if Statement • An if statement allows a program to choose whether or not to execute a following statement. • Syntax: if (condition) statement; • Semantics: condition is a Boolean expression: Something that evaluates to True or False. If condition is true then execute the statement is executed. B.Ramamurthy

  9. The if statement : syntax if(expression) statement; //single statement executed //if expression is true if(expression) { //statements inside {} are //executed if expression is true statement1; statement2; … statement n; } B.Ramamurthy

  10. If -else Statement • An if-else statement allows a program to do one thing if a condition is true and a different thing if the condition is false. • Syntax: if ( condition ) statement1 else statement2 • Statements to be executed for if and else can be a single statement or multiple statements enclosed in { }. B.Ramamurthy

  11. The if - else statement: syntax if(expression) statement; else statement; if(expression) { statement block } else { statement block } B.Ramamurthy

  12. The switch statement • switch(expression) • { • caseconstant: • statement(s); • break; • caseconstant: • statement(s); • break; • /* default is optional*/ • default: • statement(s); • } B.Ramamurthy

  13. The switch statement • Expression must be of type integer or character • The keyword case must be followed by a constant • break statement is required unless you want all subsequent statements to be executed. B.Ramamurthy

  14. Practice! • Convert these nested if/else statements to a switch statement: • if (rank==1 || rank==2) • cout << "Lower division \n"; • else • { if (rank==3 || rank==4) • cout << "Upper division \n"; • else •    { if (rank==5) • cout << "Graduate student \n"; • else • cout << "Invalid rank \n"; • } • } B.Ramamurthy

  15. Practice Solution! • switch(rank) • { • case 1: case 2: • cout << "Lower division \n"; • break; • case 3: case 4: • cout << "Upper division \n"; • break; • case 5: • cout << "Graduate student \n"; • break; • default: • cout << "Invalid rank \n"; • }//end switch B.Ramamurthy

  16. Summary • In many applications, choices are to be made depending on some conditions related to the problem. Selection or decision structures are used to model such situations. • C++ supports the implementation of “selection” through the “if” and “switch” statements. In this discussion we looked at various forms of selection statements. B.Ramamurthy

More Related