1 / 18

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

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.

melosa
Download Presentation

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Selection Constructs Prof. amr Goneid, AUC

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

  4. 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

  5. 2. Sequential Constructs • Simple Statement: c = a + b; • Block statement: { temp = a; a = b; b = temp; } Prof. amr Goneid, AUC

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

More Related