Title: if Statements
1Programming
2Review Rules for Division
- C treats integers different than doubles.
- 100 is an int.
- 100.0 , 100.0000, and 100. are doubles.
- The general rule for division of int and double
types is - double/double -gt double (normal)
- double/int -gt double (normal)
- int/double -gt double (normal)
- int/int -gt int (special case any decimal
places discarded)
3Rules for Division
- Example
- 220. / 100.0 double/double -gt double result is
2.2 - 220. / 100 double/int -gt double result is 2.2
- 220 / 100.0 int/double -gt double result is 2.2
- 220 / 100 int/int -gt 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).
4Forcing a Type Change
- You can change the type of an expression with a
cast operation - Syntax
- variable1 type(variable2)
- variable1 type(expression)
- Example
- int x1, y2
- double result1 x/y // 1/2 0 -gt 0.0
- double result2 double(x)/y // 1.0/2 0.5 -gt
0.5 - double result3 x/double(y) // 1/2.0 0.5 -gt
0.5 - double result4 double(x)/double(y) // 1.0/2.0
-gt 0.5 - double result5 double(x/y)
- // double(1/2) double(0) -gt 0.0
- int cents int(result4100) // cents is 50
5Three Program Structures
- Sequence - executable statements which the
computer processes in the given order - Choice - sequence(s) selected depending on some
condition - if ltcondition existsgt
- ltdo Pgt
-
- Iteration - repetitively executed sequences
- while ltcondition existsgt
- ltdo Pgt
-
6Sequence
- It is natural to write a program as a sequence of
program structures such as sequences, choices,
and iterations
7Choice Constructs
- Provide
- Ability to control whether a statement list is
executed - Two constructs
- if statement
- if
- if-else
- if-else-if
- switch statement
8The Basic if Statement
- Syntax
- if(Expression)
- Action
- If the Expression is true then execute Action
- Action is either a single statement or a group of
statements within braces - Example absolute value
- if(value lt 0)
- value -value
Expression
true
false
Action
9Absolute Value
- // program to read number print its absolute
value - include ltiostreamgt
- using namespace std
- int main()
- int value
- cout ltlt "Enter integer "
- cin gtgt value
- if(value lt 0)
- value -value
- cout ltlt "The absolute value is " ltlt value ltlt
endl - return 0
10Choice (if)
- Put multiple action statements within braces
-
- if ltit's raininggt
- lttake umbrellagt
- ltwear raincoatgt
11Sorting Two Numbers
- int value1
- int value2
- int temp
- cout ltlt "Enter two integers "
- cin gtgt value1 gtgt value2
- if(value1 gt value2)
- temp value1
- value1 value2
- value2 temp
-
- cout ltlt "The input in sorted order "
- ltlt value1 ltlt " " ltlt value2 ltlt endl
12Relational Operators
- Relational operators are used to construct a
logical expression - Math C Plain English
- equals example if(ab)
- (ab) means put the value of b into a
- lt lt less than
- ? lt less than or equal to
- gt gt greater than
- ? gt greater than or equal to
- ? ! not equal to
13Relational Expressions
- Examples
- numberOfStudents lt 200
- 10 gt 20
- 20 j 10 i
14Operator Precedence
- Which comes first?
- /
- -
- lt lt gt gt
- !
-
Answer
15The if-else Statement
- Syntax
- if(Expression)
- Action1 else Action2
- If Expression is true thenexecute Action1
otherwiseexecute Action2 - Example
- if(v 0)
- cout ltlt "v is 0"
- else
- cout ltlt "v is not 0"
Expression
false
true
Action1
Action2
16Choice (if and else)
- if ltit's sunnygt
- ltgo to beachgt
-
- else
- lttake umbrellagt
-
17Finding the Big One
- int value1
- int value2
- int larger
- cout ltlt "Enter two integers "
- cin gtgt value1 gtgt value2
- if(value1 gt value2)
- larger value1
- else
- larger value2
- cout ltlt "Larger of inputs is " ltlt larger ltlt endl
18Selection
- Often we want to perform a particular action
depending on the value of an expression - Two ways to do this
- if-else-if statement
- if-else statements glued together
- switch statement
- An advanced construct
19if-else-if Statements
- if ltcondition 1 existsgt
- ltdo Qgt
-
- else if ltcondition 2 existsgt
- ltdo Rgt
-
- else if ltcondition 3 existsgt
- ltdo Sgt
-
- else
- ltdo Tgt
Q
R
T
S
20if-else-if Statements
- if ltMon, Wed, or Fri AMgt
- ltgoto COMP 104gt
-
- else if ltTues, Thurs AMgt
- ltgoto MATH 113gt
-
- else if lt1PM or 7PMgt
- lteatgt
-
- else
- ltsleepgt
21if-else-if Statement
- Example
- if(score gt 90)
- cout ltlt "Grade A" ltlt endl
- else if(score gt 80)
- cout ltlt "Grade B" ltlt endl
- else if(score gt 70)
- cout ltlt "Grade C" ltlt endl
- else if(score gt 60)
- cout ltlt "Grade D" ltlt endl
- else
- cout ltlt "Grade F" ltlt endl
-
22switch Statement
-
- switch(int(score)/10)
- case 10
- case 9 cout ltlt "Grade A" ltlt endl
- break
- case 8 cout ltlt "Grade B" ltlt endl
- break
- case 7 cout ltlt "Grade C" ltlt endl
- break
- case 6 cout ltlt "Grade D" ltlt endl
- break
- default cout ltlt "Grade F" ltlt endl
-
23- int left
- int right
- char oper
- cout ltlt "Enter simple expression "
- cin gtgt left gtgt oper gtgt right
- cout ltlt left ltlt " " ltlt oper ltlt " " ltlt right
- ltlt " "
- switch (oper)
- case '' cout ltlt left right ltlt endl break
- case '-' cout ltlt left - right ltlt endl break
- case '' cout ltlt left right ltlt endl break
- case '/' cout ltlt left / right ltlt endl break
- default cout ltlt "Illegal operation" ltlt endl
24Boolean 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)
- Boolean operators can be used to form more
complex conditional expressions - The and operator is
- The or operator is
- The not operator is !
- Warning
- and are bit-wise operators
- please do NOT confuse them with boolean operators
25A Boolean Type
- Example logical expressions
- bool P truebool Q falsebool R
truebool S P Qbool T !Q Rbool U
!(R !Q)
26More Operator Precedence
- Precedence of operators (from highest to lowest)
- Parentheses ( )
- Unary operators !
- Multiplicative operators /
- Additive operators -
- Relational ordering lt lt gt gt
- Relational equality !
- Logical and
- Logical or
- Assignment
27More Operator Precedence
- Parentheses ( )
- Unary operators !
- Multiplicative operators /
- Additive operators -
- Relational ordering lt lt gt gt
- Relational equality !
- Logical and
- Logical or
- Assignment
- Examples
- 5 ! 6 7 lt 3
- (5 !6) (7 lt 3)
- 5 15 4 13 12 lt 19 !false 5 lt 24
- 5 15 4 13 12 lt 19 (!false) 5 lt
24 - (5 15) 4 13 12 lt 19 true 5 lt 24
- ((5 15) 4) 13 12 lt 19 true 5 lt
24 - ((5 15) 4) 13 (12 lt 19) true (5
lt 24) - (((5 15) 4) 13) true (true true)
- false true true
- (false true) true
- true
28Finding the largest number
- Consider three numbers, A, B and C How many ways
can they be ordered? - A gt B gt C
- A gt C gt B
- B gt A gt C
- B gt C gt A
- C gt A gt B
- C gt B gt A
29Whats Wrong?
int a3, b2, c1 if ((a gt b gt c) (a gt c gt
b)) cout ltlt a ltlt endl else if ((b gt a gt c)
(b gt c gt a)) cout ltlt b ltlt endl else if ((c gt a
gt b) (c gt b gt a)) cout ltlt c ltlt endl
- a gt b gt c
- (a gt b) gt c
- (3 gt 2) gt 1
- true gt 1
- 1 gt 1
- false
- a gt c gt b
- (a gt c) gt b
- (3 gt 1) gt 2
- true gt 2
- 1 gt 2
- false
30Correct program
int a2, b2, c1 if ((a gt b) (b gt c) (a gt
c) (c gt b)) cout ltlt a ltlt endl else if ((b gt
a) (a gt c) (b gt c) (c gt a)) cout ltlt b
ltlt endl else if ((c gt a) (a gt b) (c gt b)
(b gt a)) cout ltlt c ltlt endl
31Nested if Statements
- Nested means that one complete statement is
inside another (nested scopes) - if (condition_1)
- if (condition_2)
- if (condition_3)
- cout ltlt 123
-
- cout ltlt 12
-
- cout ltlt 1
-
Scope of if-statement
32Dangling 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, b1, c1
- if(agt0)
- if(bgt0)
- c 2
- else
- c 3
33Dangling Else Problem
- C groups a dangling else with the most recent
if. - The following indentation shows how C would
group this example (answer c1). - int a-1, b1, c1
- if(agt0)
- if(bgt0)
- c 2
- else // dangling else grouped to nearest if
- c 3
34Dangling Else Problem
- Use extra brackets to clarify the intended
meaning, even if not necessary. - int a-1, b1, c1
- if(agt0)
- if(bgt0)
- c 2
- else // use to avoid dangling else
- c 3
-
-