1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

40
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

description

3 Conditional Statement: if used to execute conditionally a statement (or block of code) Syntax: if (expression) statement If expression is true, statement is executed (what is the meaning of ‘true’?) statement can be replaced by a block of statements, enclosed in curly braces

Transcript of 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Page 1: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

1

Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Page 2: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

2

Flow Control Usually a program is executed line after

line in order It is reasonable to expect different

execution orders on different inputs Computer games Illegal input

Conditional statements if switch

Page 3: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

3

Conditional Statement: if used to execute conditionally a

statement (or block of code) Syntax: if (expression) statement If expression is true, statement is

executed (what is the meaning of ‘true’?)

statement can be replaced by a block of statements, enclosed in curly braces

Page 4: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

4

Example/* This program displays the absolute value of a number given by the

user */#include <stdio.h>

int main() {double num;

printf("Please enter a real number: ");scanf("%lf", &num);if (num<0) {num = -num;}

printf("The absolute value is %g\n", num);

return 0;}

Page 5: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

5

If-Else Statementif (expression)

statement1 else

statement2

if expression is true, statement1 is executed. if expression is false, statement2 is executed both statements can be (and very often are)

replaced by blocks of statements (“compound statements”)

Page 6: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

6

Exampleint first, second, min;if (first < second) { min = first; printf ("The first number is smaller than the second\n");} else { min = second; printf ("The second number is smaller than the first\n");}

printf("The smaller number is equal to %d\n", min);

Page 7: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

7

Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Page 8: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

8

True and False In C, every expression has a numeric value An expression is ‘true’ when it is non-zero If it is zero, it is false Therefore, in the following –

if (expression) statementstatement is executed if expression is non zero

Page 9: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

9

More About Operators In C, every expression has a

numeric value When using arithmetical operators

(+, -, *, /) this is straight forward The value of A+B is the sum of A and

B And so on…

Page 10: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

10

More About Operators Expressions with relational operators

(<, <=, etc’) have values as well (intuitively, we are used to thinking about them as ‘true’ or ‘false’)

A < B equals zero if A is larger than or equal to B (false), and some non-zero value if A is smaller than B (true)

The exact non-zero value varies (and is not important for that matter)

Page 11: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

11

Relational Operators They are –

A == B (Note the difference from A = B) A != B (inequality) A < B A > B A <= B A >= B

The value of the expression is non-zero if it’s true, zero if it’s false

Page 12: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

12

Exampleint a, b;

printf("Enter two Numbers\n");scanf("%d%d", &a, &b);

if (a == b){

printf("The numbers equal %d\n", a);printf("The expression a == b is %d\n", a == b);

}else{

printf("The numbers are not equal\n");printf("The expression a == b is %d\n", a == b);

}

Page 13: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

13

The Assignment Operator = The assignment operator is also an

operator. Hence, expressions involving it have a numeric value

This value equals to whatever appears on the right of the assignment operator

For example – (x = 4) equals 4 (y = 0) equals 0

Page 14: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

14

A Very Common Mistake Very often a programmer might

confuse between the equality operator and the assignment operator - if (x==4) … if (x=4) …

The second is usually a mistake, but legal in C so the compiler doesn’t call it!

Page 15: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

15

More Examples if (0) if (1) if (3>4) if (x) equivalent to if (x == 0) if (a = 5)

Page 16: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

16

Code Examples val.c, eqn_sign.c

Page 17: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

17

Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Page 18: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

18

Logical Operators Allows to evaluate two or more

expressions - !A – ‘not’ - True when A is not, and

vice versa. A && B – ‘and’ - True when both A

and B are true A || B – ‘or’ (inclusive or) - True when

either A or B (or both) are true

Page 19: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

19

&&, ||, !=

0 10 0 01 0 1

0 10 0 11 1 1

0 11 0

&&

||!

Page 20: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

20

Exampleint grade;printf("Please enter your grade: ");scanf("%d", &grade);

if (grade < 0 || grade > 100)printf("This is not a valid grade!\n");

elseprintf("This is a valid grade\n");

Page 21: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

21

Exampleif (price > 100)

if (price < 200) printf(“reasonable price”);

if (price > 100 && price < 200) printf(“reasonable price”);

Page 22: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

22

Operation Execution Order ! %, /, * -, + <, >, ==, … &&, ||

Page 23: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

23

Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Page 24: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

24

Else-If if statements distinguish between

exactly 2 cases and execute different code in each case

The else-if construction allows for a multi-way decision

Page 25: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

25

Else-Ifif (expression)

statementelse if (expression)

statementelse if (expression)

statementelse

statement

Page 26: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

26

Exampleif (grade >= 90)

printf ("A\n"); else if (grade >= 80)

printf ("B\n"); else if (grade >= 70)

printf ("C\n"); else if (grade >= 60)

printf ("D\n"); else

printf ("F\n");

Page 27: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

27

Switchswitch (expression)

{case const-exp1:statementsbreak;case const-exp2:statementsbreak;...default:statements

}

variable – discrete const-exp – equality is

tested It is ordered break – stop default – optional, when

no prior condition held

Page 28: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

28

That grade example againswitch (grade/10) {

case 10: case 9:

printf ("A\n"); break;

case 8: printf ("B\n"); break;

case 7: printf ("C\n"); break;

case 6: printf ("D\n"); break;

default: printf ("F\n");

}

Page 29: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

29

Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Page 30: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

30

Exercise Input –

An English letter Output –

If input is a lowercase letter – the corresponding uppercase letter

If input is an uppercase letter - corresponding lowercase letter

Note – Remember to check for input validity!

Page 31: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

31

Solution switch_case.c

Page 32: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

32

Exercise (difficult!) Write a program such that –

Input – a 3-digit number Output – the same number with digits

sorted Example – if input is 132, output

should be 123 Note – if input is not a 3-digit number,

display an error message and exit!

Page 33: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

33

Solution Sort_digits.c

Page 34: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

34

The ?: operator expr1 ? expr2 : expr3 If expr1 is true (non-zero), expr2 is

evaluated. Otherwise, expr3 is evaluated

Page 35: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

35

The ?: operatorint i, j, min;

printf("Please enter two numbers: ");scanf("%d%d", &i, &j);

min = (i < j) ? i : j;printf("The minimum between %d and %d is

%d\n", i, j, min);

Page 36: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

36

Example – mass conversion Write a program such that –

Input – A positive number indicating mass One of the following characters – o, c, k,

p, indicating measurement unit (ounce, carat, kilogram, or pound

Output – The same mass expressed in grams

convert_gr.c

Page 37: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

37

Exercise Write a program that accepts a

number between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin

1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar

Remember to check for input validity!

Page 38: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

38

Solution Coins.c

Page 39: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

39

Exercise Write a program that accepts an real

number, followed by an arithmetical operator (+, -, *, /) and then a second real number

The program will calculate the result of the expression and present it on the screen

Example – for the input 10-8, the output will be – 10-8 = 2

Page 40: 1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

40

Solution Operation.c