CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

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

description

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. - PowerPoint PPT Presentation

Transcript of CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++

Page 1: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 1

CSCE 110CSCE 110PROGRAMMING FUNDAMENTALSPROGRAMMING FUNDAMENTALS

WITH WITH C++C++

Prof. Amr Goneid

AUC

Part 3. Selection Constructs

Page 2: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 2

Selection ConstructsSelection Constructs

Page 3: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 3

Selection ConstructsSelection Constructs

Basic C++ Constructs Sequential Constructs Selection with if Statement The Ternary Operator The switch Construct

Page 4: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 4

1.1. Basic C++Basic C++ ConstructsConstructs

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.

Page 5: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 5

2. Sequential Constructs2. 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;

}

Page 6: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 6

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);

3. 3. Selection with if StatementSelection with if Statement

C

SF

T

Page 7: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 7

Syntax (2):if (c) s1; else s2;e.g.if (n == 0) sum = 0;

else {

sum += x;y = sum / n;cout << n << y;

}

if Statementif Statement

C

S1S2

F

T

Page 8: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 8

Nested if StatementsNested 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’;

Page 9: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 9

Conditions & Short Circuit Conditions & Short Circuit EvaluationEvaluation 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)

Page 10: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 10

4. The Ternary Operator4. 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;

Page 11: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 11

5. The5. The switch Construct switch Construct

Page 12: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 12

TheThe switch Construct switch Construct

Syntax: e = ordinal Expression(int , char, bool)

switch (e)

{

case value1 : s1; break;

case value2 : s2; break;

default : s; //Optional

}

Page 13: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 13

switch Construct Exampleswitch 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”;

}

Page 14: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 14

Ommiting breakOmmiting break

switch (choice){

case ‘R’ : cout << “Red” ;case ‘G’ : cout << “Green” ; case ‘B’ : cout << “Blue” ; default : cout << “Error”;

}

Input : R output: RedGreenBlueErrorInput : G output: GreenBlueErrorInput : B output: BlueErrorInput : Y output: Error

Page 15: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 15

More than one LabelMore 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”;

}

Page 16: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

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;

Page 17: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

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

Page 18: CSCE 110 PROGRAMMING FUNDAMENTALS WITH  C++

Prof. amr Goneid, AUC 18

Sample Dialogue:

Assignment 1:Choose 1 to see the due dateChoose 2 to see the maximum markChoose 3 to exitEnter a choice and press return1The due date is 23/4/01Enter a choice and press return2The maximum mark is 20Enter a choice and press return4Not a valid choice. Choose again, please.3End of program