Wednesday, 15 July 2015

C++ Control Structures, Statements and Operators

Today, we will look at some more C++ statements and the do/while loop.

"do/while" Repetition Structure
•Similar to while structure
–Makes loop continuation test at end, not beginning
–Loop body executes at least once
•Format
do {
   statement

} while ( condition );








"break" and "continue" Statements
•          break statement
–        Immediate exit from while, for, do/while, switch
–        Program continues with first statement after structure
•          Common uses
–        Escape early from a loop
–        Skip the remainder of switch




•          continue statement
–        Used in while, for, do/while
–        Skips remainder of loop body

–        Proceeds with next iteration of loop





Logical Operators
•          Used as conditions in loops, if statements
•          && (logical AND)
–        true if both conditions are true
                               if ( gender == 1 && age >= 65 )
                             ++seniorFemales;
•          || (logical OR)
–        true if either of condition is true
                               if ( semesterAverage >= 90 || finalExam >= 90 )
                              cout << "Student grade is A" << endl;
•          ! (logical NOT, logical negation)
–        Returns true when its condition is false, & vice versa
                               if ( !( grade == sentinelValue ) )
                             cout << "The next grade is " << grade << endl;
Alternative:
                if ( grade != sentinelValue )
                cout << "The next grade is " << grade << endl;




Structured Programming Summary
•          Structured programming
–        Programs easier to understand, test, debug and modify
•          Rules for structured programming
–        Only use single-entry/single-exit control structures
–        Rules
                              1)  Begin with the “simplest flowchart”
                              2)  Any rectangle (action) can be replaced by two rectangles (actions) in sequence
                              3)  Any rectangle (action) can be replaced by any control structure (sequence,                                                              if,if/else, switch, do/while or for)
                               4)  Rules 2 and 3 can be applied in any order and multiple times



•          All programs broken down into
–        Sequence
–        Selection
•          if, if/else, or switch
•          Any selection can be rewritten as an if statement
–        Repetition
•          while, do/while or for
•          Any repetition structure can be rewritten as a while statement




About the Author

Unknown

Editorial Team

Post a Comment

 
Codexify © 2015 - Designed by Templateism.com