Presentation is loading. Please wait.

Presentation is loading. Please wait.

If Statements & Relational Operators, Part 2 Programming.

Similar presentations


Presentation on theme: "If Statements & Relational Operators, Part 2 Programming."— Presentation transcript:

1

2 If Statements & Relational Operators, Part 2 Programming

3 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 2 The if-else Statement l Syntax if (condition) Action_A else Action_B l if the condition is true then execute Action_A else execute Action_B l Example: if(value == 0) cout << "value is 0"; else cout << "value is not 0"; condition Action_A Action_B true false

4 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 3 Choice ( if and else ) if { } else{ }

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

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

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

8 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 7 if-else-if Statements if { } else if { } else if { } else{ } Q R TS

9 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 8 if-else-if Statements if { } else if { } else if { } else{ }

10 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";}

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

12 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 11 Nested if Statements n Nested means that one complete statement is inside another if { } } }

13 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 12 Nested if Statements n Example: if { if { } } }

14 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 13 Nested if Statements l 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.

15 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 14 Nested if Statements l 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.

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

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

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

19 COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 18 Short-circuit Evaluation l If the first operand of a logical and expression is false, the second operand is not evaluated because the result must be false. l If the first operand of a logical or expression is true, the second operand is not evaluated because the result must be true.


Download ppt "If Statements & Relational Operators, Part 2 Programming."

Similar presentations


Ads by Google