Presentation is loading. Please wait.

Presentation is loading. Please wait.

If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,

Similar presentations


Presentation on theme: "If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,"— Presentation transcript:

1 if Statements Programming

2 COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0, 100.0000, and 100. are doubles. The general rule for division of int and double types is: double/double -> double (normal) double/int -> double (normal) int/double -> double (normal) int/int -> int (special case: any decimal places discarded)

3 COMP104 Lecture 7 / Slide 3 Rules for Division l Example : 220. / 100.0 double/double -> double result is 2.2 220. / 100 double/int -> double result is 2.2 220 / 100.0 int/double -> double result is 2.2 220 / 100 int/int -> int result is 2 Summary: division is normal unless both the numerator and denominator are int, then the result is an int (the decimal places are discarded).

4 COMP104 Lecture 7 / Slide 4 Forcing a Type Change l You can change the type of an expression with a cast operation l Syntax: variable1 = type(variable2); variable1 = type(expression); l Example: int x=1, y=2; double result1 = x/y;// 1/2 = 0 -> 0.0 double result2 = double(x)/y;// 1.0/2 = 0.5 -> 0.5 double result3 = x/double(y);// 1/2.0 = 0.5 -> 0.5 double result4 = double(x)/double(y); // 1.0/2.0 -> 0.5 double result5 = double(x/y); // double(1/2) = double(0) -> 0.0 int cents = int(result4*100); // cents is 50

5 COMP104 Lecture 7 / Slide 5 Three Program Structures l Sequence - executable statements which the computer processes in the given order l Choice - sequence(s) selected depending on some condition if { } l Iteration - repetitively executed sequences while { }

6 COMP104 Lecture 7 / Slide 6 Sequence l It is natural to write a program as a sequence of program structures such as sequences, choices, and iterations

7 COMP104 Lecture 7 / Slide 7 Choice Constructs l Provide n Ability to control whether a statement list is executed l Two constructs if statement –if –if-else –if-else-if switch statement

8 COMP104 Lecture 7 / Slide 8 The Basic if Statement l Syntax if(Expression) Action If the Expression is true then execute Action l Action is either a single statement or a group of statements within braces l Example: absolute value if(value < 0) value = -value; Expression Action truefalse

9 COMP104 Lecture 7 / Slide 9 Absolute Value // program to read number & print its absolute value #include using namespace std; int main(){ int value; cout << "Enter integer: "; cin >> value; if(value < 0) value = -value; cout << "The absolute value is " << value << endl; return 0; }

10 COMP104 Lecture 7 / Slide 10 Choice ( if ) l Put multiple action statements within braces if { }

11 COMP104 Lecture 7 / Slide 11 Sorting Two Numbers int value1; int value2; int temp; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2){ temp = value1; value1 = value2; value2 = temp; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;

12 COMP104 Lecture 7 / Slide 12 Relational Operators Relational operators are used to construct a logical expression MathC++Plain English === equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] << less than  <= less than or equal to >> greater than  >= greater than or equal to  != not equal to

13 COMP104 Lecture 7 / Slide 13 Relational Expressions Examples: numberOfStudents < 200 10 > 20 20 * j == 10 + i

14 COMP104 Lecture 7 / Slide 14 Operator Precedence Which comes first? * / % + - = > == != = Answer:

15 COMP104 Lecture 7 / Slide 15 The if-else Statement l Syntax if (Expression) Action 1 else Action 2 l If Expression is true then execute Action 1 otherwise execute Action 2 l Example if(v == 0) cout << "v is 0"; else cout << "v is not 0"; Expression Action 1 Action 2 true false

16 COMP104 Lecture 7 / Slide 16 Choice ( if and else ) if { } else{ }

17 COMP104 Lecture 7 / Slide 17 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;

18 COMP104 Lecture 7 / Slide 18 Selection l Often we want to perform a particular action depending on the value of an expression l Two ways to do this if-else-if statement –if-else statements “glued” together switch statement –An advanced construct

19 COMP104 Lecture 7 / Slide 19 if-else-if Statements if { } else if { } else if { } else{ } Q R TS

20 COMP104 Lecture 7 / Slide 20 if-else-if Statements if { } else if { } else if { } else{ }

21 COMP104 Lecture 7 / Slide 21 if-else-if Statement l Example 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 cout << "Grade = F" << endl;

22 COMP104 Lecture 7 / Slide 22 switch Statement switch(int(score)/10){ case 10: case 9: cout << "Grade = A" << endl; break; case 8: cout << "Grade = B" << endl; break; case 7: cout << "Grade = C" << endl; break; case 6: cout << "Grade = D" << endl; break; default: cout << "Grade = F" << endl; }

23 int left; int right; char oper; cout << "Enter simple expression: "; cin >> left >> oper >> right; cout << left << " " << oper << " " << right << " = "; switch (oper) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }

24 COMP104 Lecture 7 / Slide 24 Boolean Type C++ contains a type named bool which can have one of two values true (corresponds to non-zero value) false (corresponds to zero value) l Boolean operators can be used to form more complex conditional expressions The and operator is && The or operator is || The not operator is ! l Warning & and | are bit-wise operators n please do NOT confuse them with boolean operators

25 COMP104 Lecture 7 / Slide 25 A Boolean Type l Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || R; bool U = !(R && !Q);

26 COMP104 Lecture 7 / Slide 26 More Operator Precedence Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering = > Relational equality == != Logical and && Logical or || Assignment =

27 COMP104 Lecture 7 / Slide 27 More Operator Precedence l Examples 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24 5 * 15 + 4 == 13 && 12 < 19 || (!false) == 5 < 24 (5 * 15) + 4 == 13 && 12 < 19 || true == 5 < 24 ((5 * 15) + 4) == 13 && 12 < 19 || true == 5 < 24 ((5 * 15) + 4) == 13 && (12 < 19) || true == (5 < 24) (((5 * 15) + 4) == 13) && true || (true == true) false && true || true (false && true)|| true true Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering = > Relational equality == != Logical and && Logical or || Assignment =

28 COMP104 Lecture 7 / Slide 28 Finding the largest number Consider three numbers, A, B and C How many ways can they be ordered? l A > B > C l A > C > B l B > A > C l B > C > A l C > A > B l C > B > A

29 COMP104 Lecture 7 / Slide 29 int a=3, b=2, c=1; if ((a > b > c) || (a > c > b)) cout << a << endl; else if ((b > a > c) || (b > c > a)) cout << b << endl; else if ((c > a > b) || (c > b > a)) cout << c << endl; What’s Wrong? a > b > c (a > b) > c (3 > 2) > 1 true > 1 1 > 1 false a > c > b (a > c) > b (3 > 1) > 2 true > 2 1 > 2 false

30 COMP104 Lecture 7 / Slide 30 Correct program int a=2, b=2, c=1; if ((a > b) && (b > c) || (a > c) && (c > b)) cout << a << endl; else if ((b > a) && (a > c) || (b > c) && (c > a)) cout << b << endl; else if ((c > a) && (a > b) || (c > b) && (b > a)) cout << c << endl;

31 COMP104 Lecture 7 / Slide 31 Nested if Statements n Nested means that one complete statement is inside another (nested scopes) if (condition_1) { if (condition_2) { if (condition_3) { cout << “123”; } cout << “12”; } cout << “1”; } Scope of if-statement

32 COMP104 Lecture 7 / Slide 32 “Dangling Else” Problem Problem: Nested if statements can seem ambiguous in their meaning. What is the value of c after the following is executed? int a=-1, b=1, c=1; if(a>0) if(b>0) c = 2; else c = 3;

33 COMP104 Lecture 7 / Slide 33 “Dangling Else” Problem l C++ groups a dangling else with the most recent if. The following indentation shows how C++ would group this example (answer: c=1 ). int a=-1, b=1, c=1; if(a>0) if(b>0) c = 2; else // dangling else grouped to nearest if c = 3;

34 COMP104 Lecture 7 / Slide 34 “Dangling Else” Problem Use extra brackets { } to clarify the intended meaning, even if not necessary. int a=-1, b=1, c=1; if(a>0){ if(b>0){ c = 2; else // use { } to avoid dangling else c = 3; }


Download ppt "If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,"

Similar presentations


Ads by Google