Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs.

Similar presentations


Presentation on theme: "Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs."— Presentation transcript:

1 Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs

2 Prof. amr Goneid, AUC2 Selection Constructs

3 Prof. amr Goneid, AUC3 Selection Constructs Basic C++ Constructs Sequential Constructs Selection with if Statement The Ternary Operator The switch Construct

4 Prof. amr Goneid, AUC4 1.Basic C++Constructs 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.

5 Prof. amr Goneid, AUC5 2. Sequential Constructs Simple Statement: c = a + b; // Add a to b, store in c Block statement: {// Swap a with b temp = a; a = b; b = temp; }

6 Prof. amr Goneid, AUC6 C = condition (logical: false/true, zero/non-zero) e.g. a > b S = simple or compound statement Syntax (1): if (c) ; e.g. if (b > a) x = abs(a-b); 3. Selection with if Statement C S F T

7 Prof. amr Goneid, AUC7 Syntax (2):if (c) s1; else s2; e.g. if (n == 0) sum = 0; else { sum += x; y = sum / n; cout << n << y; } if Statement C S1S2 F T

8 Prof. amr Goneid, AUC8 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’;

9 Prof. amr Goneid, AUC9 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 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 d) (c >= 65) && (c <= 90)

10 Prof. amr Goneid, AUC10 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 : e2e.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;

11 Prof. amr Goneid, AUC11 5. The switch Construct

12 Prof. amr Goneid, AUC12 The switch Construct Syntax: e = ordinal Expression(int, char, bool) switch (e) { case value1 : s1; break; case value2 : s2; break; … default : s;//Optional }

13 Prof. amr Goneid, AUC13 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”; }

14 Prof. amr Goneid, AUC14 Ommiting break switch (choice) { case ‘R’ : cout << “Red” ; case ‘G’ : cout << “Green” ; case ‘B’ : cout << “Blue” ; default : cout << “Error”; } Input : Routput: RedGreenBlueError Input : Goutput: GreenBlueError Input : Boutput: BlueError Input : Youtput: Error

15 Prof. amr Goneid, AUC15 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”; }

16 Prof. amr Goneid, AUC16 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;

17 Prof. amr Goneid, AUC17 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

18 Prof. amr Goneid, AUC18 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


Download ppt "Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs."

Similar presentations


Ads by Google