18
If Statements & Relational Operators, Part 2 Programming

If Statements & Relational Operators, Part 2 Programming

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Page 1: If Statements & Relational Operators, Part 2 Programming

If Statements & Relational Operators,

Part 2

Programming

Page 2: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 2

The if-else Statement

Syntax

if (condition) Action_Aelse Action_B

if the condition is true then execute Action_A elseexecute Action_B

Example:if(value == 0)

cout << "value is 0";else

cout << "value is not 0";

condition

Action_A Action_B

true false

Page 3: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 3

Choice (if and else)

if <it's sunny>{

<go to beach>

}

else{

<take umbrella>

}

Page 4: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 4

Finding the Big One

int value1;

int value2;

int larger;

cout << "Enter two integers: ";

cin >> value1 >> value2;

if(value1 > value2)

larger = value1;

else

larger = value2;

cout << "Larger of inputs is: " << larger << endl;

Page 5: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 5

Finding the Big One

const double PI = 3.1415926;

int radius;

double area;

cout << "Enter the radius of the circle: ";

cin >> radius;

if(radius > 0){

area = radius * radius * PI;

cout << "The area of the circle is: " << area;

}

else

cout << "The radius has to be positive " << endl;

Page 6: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 6

Even or Odd

int value1;

bool even;

cout << "Enter a integer : ";

cin >> value;

if(value%2 == 0)

even = true;

else

even = false;

// even = !( value%2);

Page 7: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 7

if-else-if Statements

if <condition 1 exists>{

<do Q>

}

else if <condition 2 exists>{

<do R>

}

else if <condition 3 exists>{

<do S>

}

else{

<do T>

}

Q

R

TS

Page 8: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 8

if-else-if Statements

if <1PM or 7PM>{

<eat>

}

else if <Mon, Wed or Fri>{

<goto COMP 102>

}

else if <Tues or Thurs AM>{

<goto MATH 113>

}

else{

<sleep>

}

Page 9: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 9

if-else-if Statement int people, apples, difference;

cout << "How many people do you have?\n";cin >> people;cout << "How many apples do you have?\n";cin >> apples;

if(apples == people) cout << "Everybody gets one apple.\n"; else if(apples > people){ difference = apples - people; cout << "Everybody gets one apple, & there are " << difference << " extra apples.\n";}

else{ difference = people - apples;

cout << "Buy " << difference << " more apples so that everyone gets one apple.\n";}

Page 10: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 10

if-else-if Example int score; cout << "Please enter a score: "; cin >> score;

if (score >= 90) cout << "Grade = A" << endl;

else if (score >= 80) cout << "Grade = B" << endl; else if (score >= 70) cout << "Grade = C" << endl; else if (score >= 60) cout << "Grade = D" << endl; else // totalscore < 59 cout << "Grade = F" << endl;

Page 11: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 11

Nested if Statements Nested means that one complete statement is inside

another

if <condition 1 exists>{

if <condition 2 exists>{

if <condition 3 exists>{

<do A>

}

<do B>

}

<do C: sleep>

}

Page 12: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 12

Nested if Statements Example:

if <it's Monday>{

<go to HKUST>

if <it's time for class>{

if <it's raining>{

<bring umbrella>

}

<go to COMP 102>

}

}

Page 13: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 13

Nested if Statements

Consider the following example: if the customer is a member, then { If the customer is under 18, then the entrance fee is half the full fee. If the customer is 18 or older, then the entrance fee is 80% of the full fee. }

The if statements deciding whether to charge half fee to someone under 18 or whether to charge 80% to someone over 18 are only executed if the outer if statement is true, i.e. the customer is a member. Non-members, no matter what their age, are charged full fee.

Page 14: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 14

Nested if Statements

Consider a variant of the previous example: if the customer is a member, then { If the customer is under 18, then the entrance fee is half the full fee. } If the customer is 18 or older, then the entrance fee is 80% of the full fee.

Here, member customers under 18 will be charged half fee and all other customers over 18 will be charged 80% of the full fee.

Page 15: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 15

Nested if Statements

If (member)

{

if (age < 18)

{

fee = fee * 0.5;

}

if (age >=18)

fee = fee * 0.8;

}

If (member)

{

if (age < 18)

{

fee = fee * 0.5;

}

}

if (age >=18)

fee = fee * 0.8;

Page 16: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 16

“Dangling Else” Problem

Always pair an else with the most recent unpaired if in the current block. Use extra brackets { } to clarify the intended meaning, even if not necessary. For example, what is the value of c in the following code?

int a = -1, b = 1, c = 1;if( a > 0 ) if( b > 0 ) c = 2;else c = 3;

Page 17: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 17

“Dangling Else” Problem(A) int a = -1, b = 1, c = 1; if (a > 0) { if (b > 0) c = 2; else c = 3; }(B) int a = -1, b = 1, c = 1; if (a > 0) { if (b > 0) c = 2; } else c = 3;

(A) is the correct interpretation. To enforce (B), braces have to be explicitly used, as above.

Page 18: If Statements & Relational Operators, Part 2 Programming

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 18

Short-circuit Evaluation If the first operand of a logical and expression is false,

the second operand is not evaluated because the result must be false.

If the first operand of a logical or expression is true, the second operand is not evaluated because the result must be true.