nested control structures * conditional operator (?:) * preincrement and postincrement *...

Download Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition

If you can't read please download the document

Upload: kristina-booth

Post on 20-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

  • Nested Control Structures

    * Conditional Operator (?:)* Preincrement and Postincrement* Predecrement and Postdecrement* Counter-Controlled Repetition* Nested Control Structure Dr. Ming Zhang

  • Condition Operator (?:)* Conditional Operator (?:) - Ternary Operator Operand1 ? Operand2 : Operand3* First Operand Condition Expression* Second Operandthe value for the entire conditional expression if the condition is true.* Third Operandthe value for the entire conditional expression if the condition is false. Dr. Ming Zhang

  • Conditional Operator and if else* Conditional Operator cout = 60) cout
  • Examples of Conditional Operator* If grade is greater than or equal to 90, print on screen A, otherwise B or less than Bcout
  • Preincrement and Postincrement* Preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides.

    * Postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1 Dr. Ming Zhang

  • Example of Pre/Postincrementint main( ){ int c; c = 5;cout
  • Predecrement and Postdecrement* Predecrement --a Decrement a by 1, then use the new value of a in the expression in which a resides.

    * Postdecrement a-- Use the current value of a in the expression in which a resides, then decrement a by 1 Dr. Ming Zhang

  • Example of Pre/Postdecrementint main( ){ int c; c = 5;cout
  • No increment in while Loop-Continuation Condition#include using std::cout;int main ( ){ int counter = 1;while (counter
  • Preincrement in while Loop-Continuation Condition#include using std::cout;int main ( ){ int counter = 1;while (++counter
  • Posincrement in while Loop-Continuation Condition#include using std::cout;int mail ( ){ int counter = 1;while (counter ++
  • Nested Control Structures (1)A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, several of the students who completed this course took the licensing examination. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam and a 2 it the student failed. Dr. Ming Zhang

  • Nested Control Structures (2)Your program should analyze the results of the exam as follow:1. Input each test result. Display the message Enter result on the screen each time the program requests another test result.2. Count the number of test results of each type.3. Display a summary of the test results indicating the number of students who passed and the number of students who failed.4. If more than 8 students passed the exam, print the message Raise tuition. Dr. Ming Zhang

  • Nested Control Structures (3)Top:Analyze exam results and decide if tuition should be raised.

    First Refinement:Initialize variablesInput the ten quiz grades and count passes and failuresPrint a summary of the exam results and decide if tuition should be raised. Dr. Ming Zhang

  • Nested Control Structures (4)Fig. 2.11.......while (studentCounter result; if (result == 1)passes = passes +1;elsefailures = failures + 1;studentCounter = studentCounter + 1; } Dr. Ming Zhang

  • Common Programming Error (1)#include using std::cout;int main( ){ int class = 1; while (class
  • Common Programming Error (2)#include using std::cout;int main ( ){ int counter = 1;while (counter
  • Common Programming Error (3)#include using std::cout; using std::cinint main ( ){ int grade; cin >> grade; if (grade >= 60); cout
  • Common Programming Error (4)#include using std::cout;int main ( ){ int counter = 1;while (counter
  • Common Programming Error (5)#include using std::cout;int main ( ){ int counter = 1;While (counter
  • Common Programming Error (6)#include using std::cout;int main ( ){ while (counter
  • Common Programming Error (7)#include using std::cout;int main ( ){ int counter = 1;while (counter
  • Common Programming Error (8)#include using std::cout;int main ( ){ float counter = 1.0; while (counter