Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CS1010E Lecture 2

Control Structures: Selection

Henry Chia (hchia@comp.nus.edu.sg)

Semester 1 2014 / 2015

1 / 24
Lecture Outline

 Algorithmic problem solving


 Control structures
 Boolean values, variables and expressions
 Relational and Logical Operators
 Selection statements
 Nested selection statements

2 / 24
Algorithmic Problem Solving

 Algorithm – set of instructions that manipulates data to


solve an algorithmic problem.
 Control structures (sequence, selection, and repetition)
provide the flow of control in an algorithm
 Characteristics of an algorithm:
– Each step of an algorithm is exact
– An algorithm must terminate
– An algorithm must be effective
– An algorithm must be general

3 / 24
Control Structures – Sequence

 A sequence structure contains steps


that are performed one after another
 Example: To pass this course, you Read module

have to read the module, and then


take the exam
Take exam

4 / 24
Control Structures – Selection

 A selection structure contains one set


of steps that is performed if a
No
condition is true, and possibly another exemption?
n

set of steps that is performed if a y


condition is false
Read module
 Example: If you do not have exemption
from the module, then you need to
read the module and take the exam Take exam

5 / 24
Control Structures – Repetition

 A repetition structure contains a set


of steps that is repeated as long as a
No
condition is true exemption?
n

 Example: One who reads the module, y


takes the exam but fail will need to
Read module
repeat again

Take exam

Fail? y

6 / 24
Boolean Values and Variables

 Boolean value – true and false


 To use boolean values and variables in a C program include
the preprocessor directive
#include <stdbool.h>
 Example boolean variable declaration:
bool overWeight=true;
 Example boolean value assignment:
overWeight = false;
 Boolean variables names should suggest a true/false (yes/no)
outcome, e.g. overWeight, underWeight, but not weight

7 / 24
Conditions – Boolean Expression

booleanAssignment

boolVar = boolExpr ;
boolExpr

true
false

boolVar
arithmeticExpr relationalOp arithmeticExpr
boolExpr logicalOp boolExpr

( boolExpr )

8 / 24
Relational Operators

 Relational operators compare two arithmetic expressions:


Relational Op Interpretation
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
 Example of assignment statements with conditions:
– same = x == y;
– overWeight = (mass/(ht*ht)) > 24.9;

9 / 24
Logical Operators
 Logical operators compare conditions
 Three logical operators: and (&&), or (||), not (!)
A B A && B A || B !A !B
false false false false true true
false true false true true false
true false false true false true
true true true true false false

– A && B is true only if both A and B are true


– A || B is false only if both A and B are false
– !A is true only if A is false
 Example:
– normal = (bmi >= 18.5) && (bmi <= 24.9);
– normal = (18.5 <= bmi <= 24.9); /*???*/
10 / 24
Logical Operators: Short-Circuit

 When expressions with logical operators are executed, C will


only evaluate as much of the expression as necessary to
evaluate it
– If A is false, then the expression A && B is also false, and
there is no need to evaluate B
– If A is true, then the expression A || B is also true, and
there is no need to evaluate A

11 / 24
Statement and Statement Block

statement  Control Statements


assignmentStatement – Selection
controlStatement
⊲ if..else
functionCallStatement
– Repetition
⊲ do..while
returnStatement
⊲ while
stmtBlock ⊲ for
statement  Statement Block –
one statement or
{ statement }
group of statements

12 / 24
Selection: if..else Statement
ifElseStatement

if ( condition ) stmtBlock

else stmtBlock
if (condition)
{
statements(s);
}
else
{
statements(s);
}

 Executes the if statement block when the condition is true;


otherwise the else statement block is executed
 Else statement block is optional
 Curly braces may be omitted if single-statement block
13 / 24
Example: Maximum of two numbers

#include <stdio.h>
int main(void)
{ x>y n
int x, y, max;
printf("Enter two numbers: "); y
scanf("%d%d", &x, &y);
max = x
if (x > y)
max = x;
if (y >= x) /* if (y > x) ??? */
max = y;
y >= x n
printf("Maximum is %d\n", max);
return 0; y
}
max = y

14 / 24
Example: Maximum of two numbers

#include <stdio.h>
int main(void)
{
int x, y, max;
x>y n
printf("Enter two numbers: ");
scanf("%d%d", &x, &y); y
if (x > y)
max = x; max = x max = y
else
max = y;
printf("Maximum is %d\n", max);
return 0;
}

15 / 24
Example: BMI

 Using a boolean variable to store the boolean expression


normal←
#include <stdio.h> bmi ≥ 18.5
#include <stdbool.h> and
int main(void) bmi ≤ 24.9
{
double bmi; n
bool normal; normal

printf("Enter bmi: "); y


scanf("%lf", &bmi);
normal = (bmi >= 18.5 && bmi <= 24.9); Normal Abnormal

if (normal) /* or (normal == true) */


printf("Normal\n");
else
printf("Abnormal\n");
return 0;
}

16 / 24
Example: BMI

 Using the boolean expression as part of the if condition


#include <stdio.h>
int main(void)
{ bmi ≥ 18.5
and n
double bmi;
bmi ≤ 24.9
printf("Enter bmi: ");
scanf("%lf", &bmi); y
if (bmi >= 18.5 && bmi <= 24.9)
printf("Normal\n");
else Normal Abnormal
printf("Abnormal\n");
return 0;
}

17 / 24
Example: BMI

 Expressing the condition in the opposite sense


#include <stdio.h>
int main(void)
{ bmi < 18.5
or n
double bmi;
bmi > 24.9
printf("Enter bmi: ");
scanf("%lf", &bmi); y
if (bmi < 18.5 || bmi > 24.9)
printf("Abnormal\n");
else Abnormal Normal
printf("Normal\n");
return 0;
}

18 / 24
Nesting if..else Statements

#include <stdio.h>
bmi ≥ 18.5 n
int main(void)
{ y
double bmi;
printf("Enter bmi: "); bmi ≤ 24.9 n
scanf("%lf", &bmi);
y
if (bmi >= 18.5)
if (bmi <= 24.9)
printf("Normal\n"); Normal Abnormal Abnormal
else
printf("Abnormal\n");
else
printf("Abnormal\n");
return 0;
}

19 / 24
Nesting if..else Statements

#include <stdio.h> normal← n


#include <stdbool.h>
int main(void)
{ bmi ≥ 18.5 n
double bmi;
bool normal = false; y
printf("Enter bmi: "); bmi ≤ 24.9 n
scanf("%lf", &bmi);
y
if (bmi >= 18.5)
if (bmi <= 24.9)
normal = true; normal← y

if (normal) /* or (normal == true) */


printf("Normal\n");
else normal n
printf("Abnormal\n");
y
return 0;
} Normal Abnormal

20 / 24
Nesting if..else Statements

#include <stdio.h>
int main(void)
{
double bmi;
n
bmi < 18.5
printf("Enter bmi: ");
scanf("%lf", &bmi); y
if (bmi < 18.5)
printf("Underweight\n"); bmi ≤ 24.9 n
else
if (bmi <= 24.9) y
printf("Normal\n");
else Underweight Normal Overweight
printf("Overweight\n");
return 0;
}

 Every else binds to the nearest available if

21 / 24
Nesting if..else Statements
#include <stdio.h>
int main(void)
{
double bmi; bmi ≤ 24.9 n
printf("Enter bmi: ");
scanf("%lf", &bmi); y

if (bmi <= 24.9) n


bmi < 18.5
{
if (bmi < 18.5) y
printf("Underweight\n");
else
printf("Normal\n"); Underweight Normal Overweight
}
else
printf("Overweight\n");
return 0;
}

 Use braces to define blocks of statements that go together


22 / 24
Exercise: Leap Year

 Write a program that accepts a year as user input and


determines whether it is a leap year or otherwise.
 Problem analysis:
– 2000, 2004, 2008, ... are leap years; however
– 2100, 2200, 2300, 2500, 2600, 2700, ... are not; but
– 2000, 2400, 2800, ... are.
 Design Strategy: resolve specifics first.
 Which of the above three cases is the most specific?

23 / 24
Lecture Summary

 Characteristics of an algorithm
 Control structures: sequence, selection and repetition
 Conditions involving relational and logical operators
 Selection
– if..else statement
– Nested if..else statements

24 / 24

You might also like