Semester 1 2017 / 2018: Read Module
Semester 1 2017 / 2018: Read Module
1 / 24 3 / 24
5 / 24 7 / 24
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
13 / 24 15 / 24
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
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
22 / 24 24 / 24