Download presentation
Presentation is loading. Please wait.
Published byKristina Booth Modified over 9 years ago
1
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition * Nested Control Structure Dr. Ming Zhang
2
Condition Operator (?:) * Conditional Operator (?:) - Ternary Operator Operand1 ? Operand2 : Operand3 * First Operand Condition Expression * Second Operand the value for the entire conditional expression if the condition is true. * Third Operand the value for the entire conditional expression if the condition is false. Dr. Ming Zhang
3
Conditional Operator and if else * Conditional Operator cout =60 ? “Passed” : “Failed”); * if else if (grade >= 60) cout << “Passed”; else cout << “Failed” Dr. Ming Zhang
4
Examples of Conditional Operator * If grade is greater than or equal to 90, print on screen “A”, otherwise “B or less than B” cout =90 ? “A” : “B or less than B”); * If grade is greater than or equal to 80, print on screen “A or B”, otherwise “C or less than C” cout =80 ? “A or B” : “C or less than C”); Dr. Ming Zhang
5
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
6
Example of Pre/Postincrement int main( ) { int c; c = 5; cout << c << endl; // print 5 cout << c++ << endl;//print 5 then postincrement cout << c << endl << endl;// print 6 c = 5; cout << c << endl; // print 5 cout << ++c << endl;//preincrement then print 6 cout << c << endl << endl;// print 6 return (0); } Dr. Ming Zhang
7
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
8
Example of Pre/Postdecrement int main( ) { int c; c = 5; cout << c << endl; // print 5 cout << c-- << endl;//print 5 then postdecrement cout << c << endl << endl;// print 4 c = 5; cout << c << endl; // print 5 cout << --c << endl;//predecrement then print 4 cout << c << endl << endl;// print 4 return (0); } Dr. Ming Zhang
9
No increment in while Loop- Continuation Condition #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) { cout << counter << “ “; counter= counter + 1; } return (0); } /* print on screen 1 2 3 4 5 6 7 8 9 10 */ Dr. Ming Zhang
10
Preincrement in while Loop- Continuation Condition #include using std::cout; int main ( ) { int counter = 1; while (++counter <= 10) { cout << counter << “ “;} return (0); } /* print on screen 2 3 4 5 6 7 8 9 10 */ /* 1 would not be printed, because ++counter) */ Dr. Ming Zhang
11
Posincrement in while Loop- Continuation Condition #include using std::cout; int mail ( ) { int counter = 1; while (counter ++ <= 10) { cout << counter << “ “; } return (0); } /* print on screen (1?) 2 3 4 5 6 7 8 9 10 (11?)*/ Dr. Ming Zhang
12
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
13
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
14
Nested Control Structures (3) Top: Analyze exam results and decide if tuition should be raised. First Refinement: Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised. Dr. Ming Zhang
15
Nested Control Structures (4) Fig. 2.11....... while (studentCounter <= 10) { cout << “Enter result (1=pass, 2=fail):”; cin >> result; if (result == 1) passes = passes +1; else failures = failures + 1; studentCounter = studentCounter + 1; } Dr. Ming Zhang
16
Common Programming Error (1) #include using std::cout; int main( ) { int class = 1; while (class <= 10) { cout << class << “ “; class= class + 1; } return (0);} ERROR--------------------------------------------- Dr. Ming Zhang
17
Common Programming Error (2) #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) cout << counter << “ “; counter= counter + 1; } return (0);} ERROR----------------------------------------------- Dr. Ming Zhang
18
Common Programming Error (3) #include using std::cout; using std::cin int main ( ) { int grade; cin >> grade; if (grade >= 60); cout << “Passed”; else cout << “Failed”;return (0); } ERROR----------------------------------------------- Dr. Ming Zhang
19
Common Programming Error (4) #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) { cout << counter << “ “; } return (0);} ERROR----------------------------------------------- Dr. Ming Zhang
20
Common Programming Error (5) #include using std::cout; int main ( ) { int counter = 1; While (counter <= 10) { cout << counter << “ “; counter= counter + 1; } return (0);} ERROR----------------------------------------------- Dr. Ming Zhang
21
Common Programming Error (6) #include using std::cout; int main ( ) { while (counter <= 10) { cout << counter << “ “; counter= counter + 1; } return (0);} ERROR----------------------------------------------- Dr. Ming Zhang
22
Common Programming Error (7) #include using std::cout; int main ( ) { int counter = 1; while (counter <= 10) { cout << counter << “ “; ++(counter + 1); } return (0);} ERROR----------------------------------------------- Dr. Ming Zhang
23
Common Programming Error (8) #include using std::cout; int main ( ) { float counter = 1.0; while (counter <= 10.0 ) { cout << counter << “ “; counter = counter + 1.0; } return (0);} ERROR----------------------------------------------- Dr. Ming Zhang
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.