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
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
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;}
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”)
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);
7
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
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…
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)
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
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);
}
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
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!
15
More Examples if (0) if (1) if (3>4) if (x) equivalent to if (x == 0) if (a = 5)
16
Code Examples val.c, eqn_sign.c
17
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
19
&&, ||, !=
0 10 0 01 0 1
0 10 0 11 1 1
0 11 0
&&
||!
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");
21
Exampleif (price > 100)
if (price < 200) printf(“reasonable price”);
if (price > 100 && price < 200) printf(“reasonable price”);
22
Operation Execution Order ! %, /, * -, + <, >, ==, … &&, ||
23
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
25
Else-Ifif (expression)
statementelse if (expression)
statementelse if (expression)
statementelse
statement
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");
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
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");
}
29
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!
31
Solution switch_case.c
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!
33
Solution Sort_digits.c
34
The ?: operator expr1 ? expr2 : expr3 If expr1 is true (non-zero), expr2 is
evaluated. Otherwise, expr3 is evaluated
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);
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
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!
38
Solution Coins.c
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
40
Solution Operation.c