CMPT 201 if-else statement
Syntax for if-else if (condition) Yes_Statement else No_Statement Chooses between two alternative actions
Boolean Expression The type bool Two possible values: true or false bool flag; Two possible values: true or false 1 < 2 2 > 3 number > 10 num1 < num2
Boolean Expression if (number > 10) //when it is true cout<<“The number is greater than 10\n”; else //when it is false cout<<“The number is less than 10\n”;
Comparison Operators > greater than < less than >= greater than or equal to <= less than or equal to == equal to != not equal to a = 2; if (a == 2)
Exercises Read an integer from the user input, print a message showing if the number is even/odd.
Omitting the else part .... if (score < 60) score = 60; cout<<“Your final score is ”<<score;
Exercise: Guess a number
The And Operator If score is greater than 0 and less than 100... (score > 0) && (score < 100) is true only if both are true
The Or Operator (score1 >= 60) || (score2 >= 60) is true if one or both are true
The Not Operator !(score > 60) is equivalent to score <= 60
What is the result of the following expression !( (y < 3) || (y > 7) ) when y is 8?
Precedence Rules
if ((score > 0) && (score < 100)) even though the inner parentheses are not required x + 1 > 2 || x + 1 < -3 ((x + 1 ) > 2) || ((x + 1) < -3) Arithmetic operators (highest) Comparison operators Logical operators
Note if (x < z < y ) // Wrong!! cout<<“z is between x and y”; if ((z > x) && (z < y))
Short-Circuit Evaluation exp1 && exp2 if (((slices/students)>=2) && (students! =0)) cout<<“Each student may have two slices of pizza!”; if ((students!=0) && ((slices/students)>=2)) cout<<“Each student may have two slices of pizza!”;
Nested Statements
Guess a Number (Validate the User Input)
Guess a Number
Find the right match! if (fuel < 0.75) if (fuel < 0.25) cout<<“Fuel very low. Caution!”; else cout<<“Fuel over 3/4. Don’t stop now.”;
if (fuel < 0.75) { if (fuel < 0.25) cout<<“Fuel very low. Caution!”; } else cout<<“Fuel over 3/4. Don’t stop now.”;
Multiway if-else Sometimes things can not be solved in two ways. Driver license example: age<16: no way.. 16<=age<18: youth license 18<=age<70: standard license age>=70: special license
Multiway if-else syntax if (condition_1) Action_1; else if (condition_2) Action_2; ... else if (condition_N) Action_N; else Action_For_All_Other_Cases;
Under 16: You have to wait Between 16 and 18: You may have a youth license Between 18 and 70: You may have a standard license Over 70: You need a special license
Exercise Write a program to print the grade result based on the user input (score). Be sure to validate the user input. A 90-100 B 80-90 C 70-80 D 60-70 F <60
The Conditional Operator Can be used to create short if/else statements Format: expr ? expr : expr; x<0 ? y=10 : z=20; First Expression: Expression to be tested 2nd Expression: Executes if first expression is true 3rd Expression: Executes if the first expression is false
x > 100? a = 0 : a = 1 a = x > 100 ? 0 : 1
Blocks and Scope Scope of a variable is the block in which it is defined.
Type Casting static_cast<Type>(Value) double -> int static_cast<int>(9.2) static_cast<int>(number)
Type Casting (cont.) double m; m = static_cast<double>(y2-y1) Useful for floating point division using ints: double m; m = static_cast<double>(y2-y1) /(x2-x1); Useful to see int value of a char variable: char ch = 'C'; cout << ch << " is " << static_cast<int>(ch);
Comparing Characters Characters are compared using their ASCII values. 'A' < 'B' The ASCII value of 'A' (65) is less than the ASCII value of 'B'(66) How could you determine if a character is a capital letter? Lowercase letters have higher ASCII codes than uppercase letters, so 'a' > 'Z'
Homework 2 Due on Tuesday, October 4