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

Control Structures – Sequence

 A sequence structure contains steps


CS1010E Lecture 2 that are performed one after another
 Example: To pass this course, you Read module
Control Structures: Selection
have to read the module, and then
take the exam
Take exam
Henry Chia (hchia@comp.nus.edu.sg)

Semester 1 2017 / 2018

1 / 24 3 / 24

Lecture Outline Control Structures – Selection

 Algorithmic problem solving  A selection structure contains one set


 Control structures of steps that is performed if a
Not
 Boolean values, variables and expressions condition is true, and possibly another exempted?
n

 Relational and Logical Operators set of steps that is performed if a y


 Selection statements condition is false
Read module
 Nested selection statements  Example: If you are not exempted from
the module, then you need to read the
module and take the exam Take exam

 There is just one other repetition


structure (to be discussed later) very
similar to selection, except that it
allows control to flow back to a
previous step
2 / 24 4 / 24
Declaring Boolean Variables Boolean Expression – Condition
declaration
expr
var-id
type var-id ;
value
= value
expr op expr
,
( expr )
 type: bool
 value: true, false  A condition is an expression that evaluates to true/false
 To specify bool/true/false within the program, use  Two types of operations that evaluates to true/false
#include <stdbool.h>
– Relational operations that operates on two arithmetic
 Boolean variable identifiers should suggest a true/false expressions
outcome, e.g. overWeight, underWeight, but not weight – Logical (Boolean) operations that operates on two
 Example: bool overWeight = true; conditions

5 / 24 7 / 24

Boolean Assignment, Input and Output Relational Operators


assignmentStatement
 Relational operators compare two arithmetic expressions:
var-id = expr ; Relational Op Interpretation
< less than
 Example:
<= less than or equal to
overWeight = false;
> greater than
 C associates true with integer 1, and false with integer 0 >= greater than or equal to
 Use %d to output a boolean expression; 0 or 1 is displayed == equal to
printf("Is overweight? %d\n", overWeight); != not equal to
 Boolean value input requires first reading an integer 0 or 1  E.g. conditions using relational operators:
scanf("%d", &temp); // temp declared as int variable
overWeight = temp; // safe-type-conversion from int to bool – x == y
– (mass/(ht*ht)) > 24.9

6 / 24 8 / 24
Statement and Statement Block Example: Maximum of two numbers
 Using one if..else construct
statement  Control Statements  Easier to understand
/*
assignmentStatement – Selection This program determines the maximum of two input numbers.
*/
controlStatement
⊲ if..else #include <stdio.h>
int main(void) {
– Repetition int x=0, y=0; x>y n
stmtBlock
⊲ do..while printf("Enter two numbers: "); y
statement scanf("%d%d", &x, &y);
⊲ while
if (x > y) { Output x Output y
{ statement } ⊲ for printf("Maximum is %d\n", x);
} else {
 Statement Block – printf("Maximum is %d\n", y);
}
one statement or
return 0;
group of statements }

9 / 24 11 / 24

Selection: if..else Statement Example: Maximum of two numbers


ifElseStatement
 Using two if constructs
if ( condition ) stmtBlock  Requires two conditions
/*
else stmtBlock This program determines the maximum of two input numbers.
*/
if (condition) { #include <stdio.h>
statement; x>y n
... int main(void) {
} else { int x=0, y=0; y
statement; printf("Enter two numbers: ");
... Output x
} scanf("%d%d", &x, &y);
if (x > y) {
printf("Maximum is %d\n", x);
 Executes the if statement block when the condition is true; } y >= x n
if (y >= x) { /* if (y > x) ??? */
otherwise the else statement block is executed printf("Maximum is %d\n", y); y
 Else statement block is optional }
return 0; Output y
 Curly braces may be omitted (but encouraged) for statement }
block consisting of one statement
10 / 24 12 / 24
Exercise: Maximum of two numbers Logical Operators: Examples
 Using only one if construct
/*  A BMI(body-mass-index) of between 18.5 and 24.9 (both
This program determines the maximum of two input numbers.
*/ inclusive) indicates a normal weight.
#include <stdio.h>
int main(void) {
normal = (bmi >= 18.5) && (bmi <= 24.9);
int x=0, y=0;
abnormal = (bmi < 18.5) || (bmi > 24.9);
printf("Enter two numbers: ");
scanf("%d%d", &x, &y); /* or simply... abnormal = !normal */
 What happens in the following case?
normal = (18.5 <= bmi <= 24.9);
 A year is a leap year if it is divisible by 400, or when the year
is divisible by 4 and not divisible by 100.
leap = (year%400 == 0) ||
((year%4 == 0) && (year%100 != 0));

13 / 24 15 / 24

Logical Operators Logical Operators: Short-Circuit

 Logical (Boolean) operators compare conditions  When expressions with logical operators are executed, C will
 Three logical operators: and (&&), or (||), not (!) only evaluate as much of the expression as necessary to
evaluate it
A B A && B A || B !A !B
– If A is false, then the expression A && B is also false, and
false false false false true true
there is no need to evaluate B
false true false true true false
– If A is true, then the expression A || B is also true, and
true false false true false true
there is no need to evaluate B
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

14 / 24 16 / 24
Example: BMI Nesting if..else Statements
 Using the logical && operator in the condition  Nested ifs represent &&
/*
/* This program classifies a BMI input as Normal/Abnormal.
This program classifies a BMI input as Normal/Abnormal. */
*/ #include <stdio.h>
#include <stdio.h>
int main(void) {
int main(void) { bmi ≥ 18.5 double bmi=0.0; bmi ≥ 18.5 n
double bmi=0.0; and n
bmi ≤ 24.9 printf("Enter bmi: ");
printf("Enter bmi: "); scanf("%lf", &bmi); y
scanf("%lf", &bmi);
y if (bmi >= 18.5) { bmi ≤ 24.9 n
if (bmi >= 18.5 && bmi <= 24.9) { if (bmi <= 24.9) {
printf("Normal\n"); printf("Normal\n"); y
} else { } else {
printf("Abnormal\n"); Normal Abnormal
printf("Abnormal\n");
} Normal Abnormal Abnormal
}
return 0; } else {
} printf("Abnormal\n");
}
return 0;
}

17 / 24 19 / 24

Example: BMI Nesting if..else Statements


 Using the logical || operator in the condition  Resolving case-by-case starting with the lowest BMI values
/*
 Condition is expressed in the opposite sense This program classifies a BMI input as Normal/Underweight/Overweight.
/* */
This program classifies a BMI input as Normal/Abnormal. #include <stdio.h>
*/ int main(void) {
#include <stdio.h> double bmi=0.0; bmi < 18.5
n

int main(void) { bmi < 18.5 printf("Enter bmi: ");


double bmi=0.0; n y
or scanf("%lf", &bmi);
printf("Enter bmi: "); bmi > 24.9
if (bmi < 18.5) { bmi ≤ 24.9 n
scanf("%lf", &bmi); printf("Underweight\n");
y
if (bmi < 18.5 || bmi > 24.9) { } else { y
printf("Abnormal\n"); if (bmi <= 24.9) {
} else { printf("Normal\n"); Underweight Overweight
Normal
printf("Normal\n"); Abnormal Normal } else {
} printf("Overweight\n");
}
return 0; }
}
return 0;
}

18 / 24 20 / 24
Nesting if..else Statements Exercise: Leap Year
 Resolving case-by-case starting with the highest BMI values
/*  Write a program that accepts a year as user input and
This program classifies a BMI input as Normal/Underweight/Overweight.
*/ determines whether it is a leap year or otherwise
#include <stdio.h>  Problem analysis:
int main(void) {
double bmi=0.0; bmi > 24.9
n – 2000, 2004, 2008, ... are leap years; however
printf("Enter bmi: ");
y – 2100, 2200, 2300, 2500, 2600, 2700, ... are not; but
scanf("%lf", &bmi);
– 2000, 2400, 2800, ... are.
if (bmi > 24.9) { bmi ≥ 18.5 n
printf("Overweight\n");
} else { y
 Use nested if..else statements without logical operations
if (bmi >= 18.5) {  Design Strategy: resolve specifics first
printf("Normal\n"); Overweight Underweight
Normal
} else { – Which of the above three cases is the most specific?
printf("Underweight\n");
}
}  Do not just stop at the first correct answer; try out different
return 0; ways in which nested if..else statements can be composed
}

21 / 24 23 / 24

Nesting if..else Statements Lecture Summary


 Still correct, but difficult to understand
/*  Characteristics of an algorithm
This program classifies a BMI input as Normal/Underweight/Overweight.
*/  Control structures: sequence, selection and repetition
#include <stdio.h>  Conditions involving relational and logical operators
int main(void) {
double bmi=0.0; bmi ≤ 24.9 n
 Selection
printf("Enter bmi: ");
scanf("%lf", &bmi); y – if..else statement
if (bmi <= 24.9) { n – Nested if..else statements
bmi < 18.5
if (bmi < 18.5) {
printf("Underweight\n"); y
 Lay out nested if..else constructs in an easy to
} else {
printf("Normal\n");
understand fashion, typically resolving case-by-case starting
Underweight Normal Overweight
} from one end of the range of possible values, and working
} else {
printf("Overweight\n"); towards the other end
}
return 0;
}

22 / 24 24 / 24

You might also like