Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)

Similar presentations


Presentation on theme: "SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)"— Presentation transcript:

1 SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)

2 Lecture Slides are unsuitable for learning how to program!
You just get an illusion of learning! Programming is a craft. You have to do it to learn it.

3 'switch'-'case' statements
Conditionals 'if' statements 'switch'-'case' statements 3

4 if ( (x > 5) && (y < 7) ) { // do stuff }
We Need 3 Things: 2. Logical Operators 1. Comparison Operators 3. 'if' statement structure if ( (x > 5) && (y < 7) ) { // do stuff } 4

5 Not used for Conditionals (still, let’s have a quick look)
Operators Operators come in 4 different 'flavours': arithmetic * / % ( ) Not used for Conditionals (still, let’s have a quick look) incremental comparison < > == != >= <= logical && || ! 5

6 Using Arithmetic Operators
incremental comparison logical Using Arithmetic Operators Arithmetic operators are used as we know it from maths at school. Only difference: there may be variables or constants in the equation, too: What gets printed ? int a=5, b=6, c; const int TAX = 20; c = a * b TAX; cout << " c is: " << c; 20 6

7 arithmetic incremental comparison logical Operator Precedence The usual rules for "what gets calculated first" apply: 1. first everything in braces ( ) 2. then * and / and % 3. finally + and - int a=5, b=6, c; const int TAX = 20; 20 Therefore: c = a * b TAX; is different from: c = a * (b TAX); -20 7

8 Quick Reminder When you see an equation sign in a line:
arithmetic incremental comparison logical Quick Reminder When you see an equation sign in a line: ….things on the left are calculated first c = a * b TAX; … and then copied into the variable to the left. 1 destination source 2 This is called an ASSIGNMENT 8

9 Incremental Operators
arithmetic incremental comparison logical Incremental Operators Programmers are lazy. In order to add 1 to an int you could write: int myIntegerVariable = 100; // declare and initialise myIntegerVariable = myIntegerVariable + 1; // add one The second line can be written in "shorthand": ++ myIntegerVariable; 9

10 Incremental Operators (cont.)
arithmetic incremental comparison logical Incremental Operators (cont.) Similarly in order to subtract 1 you could write -- myIntegerVariable; NOTE: ++ and – – work with integer type variables (char, short, int and long) and on float types (double, float). (But don't even try that on strings) Could you subtract 2 by writing -- -- myIntegerVariable ? 10

11 Example int x = 5; // declare and initialise
arithmetic incremental comparison logical Example int x = 5; // declare and initialise cout << "x is " << x; // prints: x is 5 -- x; // decrement cout << " x is now " << x; // prints: x is now 4 ++ x; // increment cout << "x back to " << x; // x back to 5 11

12 Check your knowledge now:
arithmetic incremental comparison logical Check your knowledge now: Spot the mistakes in the code: const float TAX = 0.78; float income = £50.00; float netIncome; netIncome = income * TAX; ++netIncome; TAX should be uppercase should be just 50.0 correct correct 12

13  Operators arithmetic + - * / % ( ) incremental ++ --
comparison logical Operators arithmetic * / % ( ) incremental comparison < > == != >= <= logical && || ! 13

14 if ( (x > 5) && (y < 7) ) { // do stuff }
We Need 3 Things: 2. Logical Operators 1. Comparison Operators 3. 'if' statement structure if ( (x > 5) && (y < 7) ) { // do stuff } 14

15 Comparison Operators Example (using two ints x and y):
arithmetic incremental comparison logical Comparison Operators Used in loops and conditionals Compares two expressions Outcome is either true or false Example (using two ints x and y): if ( x > y ) { // yes, x is greater than y, do stuff here } else // no, it isn't, do things here 15

16 Comparison Operators (cont.)
arithmetic incremental comparison logical Comparison Operators (cont.) An expression with these operators is called a boolean expression List of 6 operators: > greater than < smaller than >= greater or equal than <= smaller or equal than == equal to != not equal to George Boole, 16

17 That's an assignment, remember ?
arithmetic incremental comparison logical a boolean expression Don't confuse with single '=' e.g. x=10; That's an assignment, remember ? Notes on '==' It is a double '==' e.g. if (x == 10) Never use '==' on floats or doubles: The float x=3.0 may internally be stored as or , therefore the outcome of if( x == 3.0 ) { could be false! 17

18 Comparison Operators (cont.)
arithmetic incremental comparison logical Examples: int a = 5, b = 10, c = 5; Code English a > b a greater than b a < b a less than b a >= b a greater or equal to b a <= b a less or equal to b a == c a equal to c a != b a not equal to b False True False True True True 18

19 Three Steps to Conditional Heaven
arithmetic incremental comparison logical Three Steps to Conditional Heaven Step 1: Comparison Operators Step 2: Logical Operators ( &&, ||, ! ) Step 3: 'if' statement structure 19

20 if ( (x > 5) && (y < 7) ) { // do stuff }
We Need 3 Things: 2. Logical Operators 1. Comparison Operators 3. 'if' statement structure if ( (x > 5) && (y < 7) ) { // do stuff } 20

21 Logical Operators With comparison operators alone we can say:
arithmetic incremental comparison logical Logical Operators With comparison operators alone we can say: " if a is greater than b, then do …." But how can we say: " if a is greater than b AND c is greater than d, then do …. " Here is how : if ( ( a>b) && (c>d) ) { // then do stuff here } 21

22 Logical Operators (cont.)
arithmetic incremental comparison logical Logical Operators (cont.) && and || or ! not List of 3 operators: || is two of these 22

23 Logical Operators (cont.)
arithmetic incremental comparison logical Logical Operators (cont.) && is evaluated before || Example: if ( (a>b) || (c>d) && (x==y) ) { // then do stuff here } 1 tab indentation 23

24 Logical Operators (cont.)
arithmetic incremental comparison logical Logical Operators (cont.) Another Example: if ( !(a>b) && !(x==y) ) { // then do stuff here } This is equivalent to: if ( (a<=b) && (x!=y) ) { // then do stuff here } 24

25 Logical Operators (cont.)
arithmetic incremental comparison logical Logical Operators (cont.) Logical Truth Tables: True && True = True False && True = False True && False = False False && False = False AND True || True = True False II True = True True II False = True False || False = False OR ! Adding a NOT will invert the result. XOR study this in your own time 25

26 if ( (x > 5) && (y < 7) ) { // do stuff }
We Need 3 Things: 2. Logical Operators 1. Comparison Operators 3. 'if' statement structure if ( (x > 5) && (y < 7) ) { // do stuff } 26

27 Conditions Happen in our day to day lives They are logical arguments
Have true or false answers If your age is 17 or older then You can drive a car. If your licence points are more than 12 then You cannot drive a car. If thirsty then Take a drink

28 Conditions If age 18 or older then Allow nightclub entry.

29 Conditions If age 18 or older then { Allow nightclub entry. If thirsty
Get a drink } Allow nightclub entry thirsty? Get a drink

30 'if' Statements, version 1 of 4
simple ‘if’ 'if' statements can come in different forms The simplest form is this: condition go here only if true continue here // program comes from here if (condition true ) { // go here if only true // execute lines coded here } // continue here 30

31 'if' Statements, version 2 of 4
‘if’ ’else’ The second version looks like this: // program comes from here if (condition true ) { // go here if true // execute code written here } else // go here if false // continue here condition go here if false go here if true continue here 31

32 'if' Statements, version 3 of 4
‘else if’ ’else’ The third version looks like this: condition1 condition2 go here if cond1 true else go here if cond2 true else go here // program comes from here if (condition true ) { // go here if true } else if ( another condition true ) // go here if 1st condition false and 2nd true else // go here if none of the above expressions true // continue here 32

33 'if' Statements, version 4 of 4
‘else if’ : ’else’ 'if' Statements, version 4 of 4 1st Condition 2nd Condition 3rd Condition If 1st cond true do this If 2nd cond true do this If 3rd cond true do this else to this if (condition here ) { // Go here if first condition is true } else if (2nd condition here ) { // only if 1st condition is false // and the 2nd condition one is true else if (3rd condition here ) { // only if 1st and 2nd conditions are false // and the 3rd one true else { // if all the above conditions are false do this. //Continue here 33

34 For Conditionals We Need
Logical Operators Comparison Operators 'if' statement structure if ( (x > 5) && (y < 7) ) { // do stuff } Done ! 34

35 Check your knowledge now:
What is wrong here ? float premium, carValue=500.0; int driverAge=30; if ( (driverAge>=18) & (driverAge <30 ) ) { premium = 0.20; } else if (driverAge>30) && (driverAge <40) ) premium = 0.10; else if premium = 0.05; cout << "Premium is " << premium*carValue; correct & should be && '(' missing. Also: what if driver is precisely 30? 'else if' but no boolean expr. 35

36 Revision This was tricky ! You need to: revise the material
study more examples practice Let’s do a few quick ones now...

37 Examples.. if (7 == 5) { cout << “true”; } else
cout << “false“;

38 Examples.. if (5 > 4 ) { cout << “true”; } else
cout << “false“;

39 Examples.. if ( 3 != 2) { cout << “true”; } else
cout << “false“;

40 Examples.. if (6 >= 9) { cout << “true”; } else
cout << “false“;

41 Examples.. if ( 5 < 12 ) { cout << “true”; } else
cout << “false“;

42 A bit more.. Instead of using only numeric constants, we can use any valid expression, including variables. Suppose that a=2, b=3 and c=6, if ( a == 5) { cout << “true”; } else cout << “false“;

43 Examples.. if ( a * b >= c) { cout << “true”; } else
cout << “false“;

44 Examples.. if ( b + 4 >= a * c) { cout << “true”; } else
cout << “false“;

45 Examples.. if ( a * b >= c) { cout << “true”; } else
cout << “false“;

46 Examples.. if ((b=2) == a ) { cout << “true”; } else
cout << “false“;

47 Logical operators (!, && ||)
! Not operator && And operator || Or operator

48 Not operator ! The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. Lets look at some examples:

49 Examples.. if ( ! (5 ==5) ) { cout << “true”; } else
cout << “false“; OUTPUT ….. false

50 Examples.. if ( ! (6<=4) ) { cout << “true”; } else
cout << “false“; OUTPUT ….. true

51 Examples.. if ( ! true ) { cout << “true”; } else
cout << “false“; OUTPUT ….. false

52 Examples.. if ( ! false ) { cout << “true”; } else
cout << “false“; OUTPUT ….. true

53 && || operators The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression “a” && “b”:

54 && operator The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression “a” && “b”:

55 && operator if ((6 > 2) && (7 > 3)) { cout << “true”; }
else cout << “false“;

56 && operator int c = 7; int d = 12 if ((d==12) && (c < 3)) {
cout << “true”; } else cout << “false“;

57 || operator The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b:

58 || operator if ((6==(2+4)) || (7 > 3)) { cout << “true”; }
else cout << “false“;

59 Demo Session Testing In-Class Test #1 Several ‘if’ statements
Simple if if - else if - else if – else Testing Exploring boundary conditions In-Class Test #1 Resit next week if needed

60 Summary Today we have learned how to...
use arithmetic operators (+, -, %, *, / ) use incremental operators (++, --) build boolean expressions in conditional statements ('if') by using comparison operators (<,>, ==,…) using logical operators (&&, ||, ! ) apply the structure of the 'if' statement

61 END OF LECTURE 4


Download ppt "SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)"

Similar presentations


Ads by Google