Professional Documents
Culture Documents
Computer Programming I: Spring 2015
Computer Programming I: Spring 2015
Computer Programming I: Spring 2015
Computer Programming I
Hamid Harroud
School of Science and Engineering,
Al Akhawayn University in Ifrane
h.harroud@aui.ma
Spring 2015
Lecture 4
Selection Structures:
if and switch Statements
Objectives
• Control Structure
• Conditions
• The if Statement
• The switch Statement
3
Control Structures
• Control Structures control the flow of execution
• Three kinds of execution flow:
– Sequence
• The execution of the program is sequential
– Selection
• A control structure which chooses alternative to execute
– Repetition
• A control structure which repeats a group of statements.
• We will focus on the selection control structure
in this lecture.
4
Conditions
6
Sample Conditions
7
Logical Operators
8
&& (and), || (or), ! (not)
9
Operator Precedence
• An operator’s precedence determines
its order of evaluation.
- x – y * z
10
Evaluation Tree & Step-by-Step
Evaluation
11
Short Circuit Evaluation
• Stopping evaluation of a logical
expression as soon as its value can be
determined.
e.g. a && b must be false if a is 0.
12
Writing English Conditions in C
13
Comparing Characters
14
Complementing a condition
• Complement a logical expression
– with the symbol !
– just change its operator
item == SENT
!(item == SENT)
item != SENT
!(a <= b)
a>b
15
DeMorgan’s Theorem
• A way to simplify the logical expression
• If comp_1 is the complement of expr_1
– The complement of expr_1 && expr_2 is
comp_1 || comp_2
– The complement of expr_1 || expr_2 is
comp_1 && comp_2.
e.g. the complement of
16
The if statement with 1 or 2 alternatives
:: Flowchart
17
The if statement with 1 or 2
alternatives
• Syntax
if (condition)
statement ;
else
statement ;
Ex-1.
if (rest_heart_rate > 56 )
printf(“keep up your exercise program!\n”);
else
printf(“Your heart rate is in excellent health\
n”);
Ex-2.
if (x != 0.0)
product = product * x;
18
Example
• Write a C program that reads in a
number N from the user and then
displays its parity (odd or even)
19
If statement with compound
statements
if (condition)
{
true task
}
else
{
false task
}
20
Example
• Write a C program that determines the
maximum value in a list of three
numbers entered by the user.
21
Nested if Statements
22
Nested if Example: Road Sign
Decision Process
23
Road Sign Decision Process
24
Dangling Else
if (x > 0)
if (y > 0)
a = a + 1;
else b = b + 1;
25
Multiple-Alternative Decision
26
Order of Conditions in a
Multiple-Alternative Decision
• When more than one condition in a
multiple-alternative decision is true, only the
first task following the first true condition
executes.
• Textbook examples
27
Example
• Write a C program that reads in a
grade on a scale of 100 and displays
the equivalent letter grade (A, B, C, D
or F)
28
Example
• Write a C program that asks a user
about his or her gender and height and
then decides if the user is Short,
Medium or Tall based on the decision
tree shown below:
29
Example
• Write a C Program that reads in three
integer numbers; Num1, Num2 and
Num3 and displays the list of entered
numbers in ascending order.
30
The switch statement
31
Example of a switch Statement
32
Example
• Write a program using the switch
statement to simulate a calculator that
supports the following arithmetic
operations: + - * /.
• Control Structure
• Conditions
• The if Statements
• The switch Statements
34