180 likes | 358 Views
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++. Prof. Amr Goneid AUC Part 3. Selection Constructs. Selection Constructs. Selection Constructs. Basic C++ Constructs Sequential Constructs Selection with if Statement The Ternary Operator The switch Construct. 1. Basic C++ Constructs.
E N D
CSCE 110PROGRAMMING FUNDAMENTALSWITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs Prof. amr Goneid, AUC
Selection Constructs Prof. amr Goneid, AUC
Selection Constructs • Basic C++ Constructs • Sequential Constructs • Selection with if Statement • The Ternary Operator • The switch Construct Prof. amr Goneid, AUC
1.Basic C++Constructs • Sequential construct : statements performed in succession, single or as a block • Selection construct : implemented with if- else and switch statements • Repetition construct : implemented with for , while , and do- while loops • Functional construct : a program is decomposed into functions (Modules) • Object-oriented : a program is decomposed into objects. Prof. amr Goneid, AUC
2. Sequential Constructs • Simple Statement: c = a + b; • Block statement: { temp = a; a = b; b = temp; } Prof. amr Goneid, AUC
3. Selection with if Statement C = condition (logical: false/true, zero/non-zero) e.g. a > b S = simple or compound statement • Syntax (1): if (c) < s >; e.g. if (b > a) x = abs(a-b); T C F S Prof. amr Goneid, AUC
if Statement • Syntax (2): if (c) s1; else s2; e.g. if (n == 0) sum = 0; else { sum += x; y = sum / n; cout << n << y; } C T F S2 S1 Prof. amr Goneid, AUC
Nested if Statements • Example: if (m >= 90) g = ‘A’; else if (m >= 80) g = ‘B’; else if (m >= 70) g = ‘C’; else if (m >= 60) g = ‘D’; else g = ‘F’; Prof. amr Goneid, AUC
Conditions & Short Circuit Evaluation • A condition is built up using logical and relational operators. It evaluates to true/false , (non-zero / zero) e.g. • (a < b) || (c > d) if a is less than b then the whole condition is true and (c > d) will not be evaluated. • (c >= 65) && (c <= 90) if c is less than 65 then the whole condition is false and (c <= 90) will not be evaluated. (a < b) || (c > d) (c >= 65) && (c <= 90) Prof. amr Goneid, AUC
4. The Ternary Operator • The ternary operator will select to evaluate one of two expressions e1 , e2 based on a condition c • Syntax: c ? e1 : e2 e.g. (k == 2) ? (x + 2) : (x + 5) if k equals 2 the expression is evaluated as x + 2 otherwise it will be evaluated as x + 5. • Example: int x = 2; int k = 2; y = ((k == 2) ? (x + 2) : (x + 5)) + 3; cout << y; k++; y = ((k == 2) ? (x + 2) : (x + 5)) + 3; cout << y; Prof. amr Goneid, AUC
5. The switch Construct Prof. amr Goneid, AUC
The switch Construct • Syntax: e = ordinal Expression(int , char, bool) switch (e) { case value1 : s1; break; case value2 : s2; break; … default : s; //Optional } Prof. amr Goneid, AUC
switch Construct Example char choice ; cin >> choice; switch (choice) { case ‘R’ : cout << “Red” ; break; case ‘G’ : cout << “Green” ; break; case ‘B’ : cout << “Blue” ; break; default : cout << “Error”; } Prof. amr Goneid, AUC
Ommiting break switch (choice) { case ‘R’ : cout << “Red” ; case ‘G’ : cout << “Green” ; case ‘B’ : cout << “Blue” ; default : cout << “Error”; } Input : R output: RedGreenBlueError Input : G output: GreenBlueError Input : B output: BlueError Input : Y output: Error Prof. amr Goneid, AUC
More than one Label switch (choice) { case ‘R’ : case ‘r’ : cout << “Red” ; break; case ‘G’ : case ‘g’ : cout << “Green” ; break; case ‘B’ : case ‘b’ : cout << “Blue” ; break; default : cout << “Error”; } Prof. amr Goneid, AUC
Example: void main() { int choice; cout << ”Assignment 1:\n”; cout << ”Choose 1 to see the due date\n”; cout << ”Choose 2 to see the maximum ” << ”mark\n”; cout << ”Choose 3 to exit\n”; do { cout << ”Enter a choice and press ” << ”return\n”; cin >> choice; Prof. amr Goneid, AUC
switch (choice) { case 1: cout << ”The due date is 13/4/01\n”; break; case 2: cout << ”The maximum mark is 20\n”; break; case 3: cout << ”End of program\n”; break; default: cout << ”Not a valid choice.\n” << ”Choose again, please.\n”; } } while (choice != 3); } // End of program Prof. amr Goneid, AUC
Sample Dialogue: Assignment 1: Choose 1 to see the due date Choose 2 to see the maximum mark Choose 3 to exit Enter a choice and press return 1 The due date is 23/4/01 Enter a choice and press return 2 The maximum mark is 20 Enter a choice and press return 4 Not a valid choice. Choose again, please. 3 End of program Prof. amr Goneid, AUC