Computer Programming I: Spring 2015

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

CSC 1401

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

• A program may choose among alternative


statements by testing the value of key
variables.
if (grade > 60)
printf(“Passed!”)
• Condition is an expression that is either false
(represented by 0) or true (represented by 1).
grade > 60
• Conditions may contain relational or equality
operators.
5
Relational and Equality Operators

6
Sample Conditions

7
Logical Operators

• To form more complicated conditions by the


three logical operators
&& (and)
|| (or)
! (not)
• Logical Expressions is an expression which
uses one or more logical operators, e.g.,
salary < MIN_SALARY || dependents > 5
temperature > 90.0 && humidity > 0.9
!(0 <= n && n<= 100)

8
&& (and), || (or), ! (not)

9
Operator Precedence
• An operator’s precedence determines
its order of evaluation.

- x – y * z

x + y < min + max

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

age > 25 && (status == ‘S’|| status == ‘D’)

age <= 25 ||(status != ‘S’&& status!= ‘D’)

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

• An if statement with another if statement as


its true task or its false task

22
Nested if Example: Road Sign
Decision Process

23
Road Sign Decision Process

24
Dangling Else

• Which if is associated with the 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.

For example, if the user enters the following


list of integers: 84 3 55
Your program should display the following:
The ordered list is: 3 55 84

30
The switch statement

When the selection


is based on the
value of a single
variable or of a
simple
expression
(called the
controlling
expression).

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: + - * /.

You program should start by asking


the user to select an arithmetic
operation and the two double
operands, calculates and then displays
the result.
33
Summary

• Control Structure
• Conditions
• The if Statements
• The switch Statements

34

You might also like