1 / 24

C++ Programming: From Problem Analysis to Program Design

Explore switch structures and efficiency through control structures for problem analysis and program design. Learn how repetition helps in programming with examples of while loops and their variations.

tjudge
Download Presentation

C++ Programming: From Problem Analysis to Program Design

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. C++ Programming: From Problem Analysis to Program Design Chapter 4: Control Structures I (Selection)

  2. switch Structures • switch structure: alternate to if-else • switch expression is evaluated first • Value of the expression determines which corresponding action is taken • Expression is sometimes called the selector

  3. switch Structures (continued) • Expression value can be only integral • Its value determines which statement is selected for execution • A particular case value should appear only once

  4. switch Structures (continued) • One or more statements may follow a case label • Braces are not needed to turn multiple statements into a single compound statement • The break statement may or may not appear after each statement • switch, case, break, and default are reserved words

  5. #include <iostream> using namespace std; int main() { char grade;cout << "Enter your grade: ";cin >> grade;cout << endl; switch (grade) { case 'A': cout << "Your grade is A." << endl; break; case 'B': cout << "Your grade is B." << endl; break; case 'C': cout << "Your grade is C." << endl; break; case 'F': cout << "Your grade is F." << endl; break; default: cout<<" The grade is invalid."<<endl; } return 0; } C++ Programming: From Problem Analysis to Program Design, Third Edition

  6. Chapter 5: Control Structures II (Repetition)

  7. Why Is Repetition Needed? • Repetition allows you to efficiently use variables • Can input, add, and average multiple numbers using a limited number of variables • For example, to add five numbers: • Declare a variable for each number, input the numbers and add the variables together • Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers

  8. The while Loop • The general form of the while statement is: while (expression) statement while is a reserved word • Statement can be simple or compound • Expression acts as a decision maker and is usually a logical expression • Statement is called the body of the loop • The parentheses are part of the syntax

  9. The while Loop (continued) • Expression provides an entry condition • Statement executes if the expression initially evaluates to true • Loop condition is then reevaluated • Statement continues to execute until the expression is no longer true

  10. The while Loop (continued) • Infinite loop: continues to execute endlessly • Can be avoided by including statements in the loop body that assure exit condition will eventually be false

  11. Counter-Controlled while Loops • If you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled loop

  12. Sentinel-Controlled while Loops • Sentinel variable is tested in the condition and loop ends when sentinel is encountered

  13. Flag-Controlled while Loops • A flag-controlled while loop uses a bool variable to control the loop • The flag-controlled while loop takes the form:

More Related