CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1
If statement (preview version)
Syntax form:
if ( condition)
Statements
else
Statements
/* example: compare if two given integers are equal */
#include <stdio.h>
main()
{
int a, b;
printf("Input two integers>");
scanf("%d%d", &a, &b);
/* comparison of the two integers */
if (b == a)
{
printf("The two numbers are equal. \n");
} /* { } can be omitted if there is only one statement */
else
printf("The two numbers are not equal. \n");
}
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 2
Flowchart for if Statements
Two Alternatives One Alternative
a == b
a and bequal
a and b not equal
exit
a == b
A and b are equal
FT
TF
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 3
Example 1: Find Roots of a Quadratic Equation (Refined)
Algorithm 3 1. Get a, b, c2. Calculate the
discriminant: disc = pow(b,2) – 4*a*c;
3. Square root of discriminant: sqrt_disc = sqrt(labs(disc));
4. e = -b / (2*a), f = sqrt_disc /(2*a)
5. If disc >= 0 then root_1 = e + f, root_2 = e – f
6. else root_1 = e + f i, root_2 = e – f i
Algorithm 2
1. Get a, b, c2. Calculate the
discriminant: disc = pow(b,2) – 4*a*c;
3. Calculate the square root of discriminant: sqrt_disc = sqrt(disc);
4. Calculate root_1 = (-b +sqrt_disc)/(2*a)
5. Calculate root_2 = (-b –sqrt_disc)/(2*a)
6. Display root_1, root_2
Algorithm 4 1. Get a, b, c2. If a == 0, print
the first coefficient must not be 0
3. else 4. disc = pow(b,2) –
4*a*c;5. sqrt_disc =
sqrt(labs(disc));6. e = -b / (2*a)7. f = sqrt_disc /(2*a)8. If disc >= 0 then
root_1 = e + f,root_2 = e – f
9. else root_1 = e + f i, root_2 = e – f I
See implementation
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 4
Example 2: find if a year is a leap year
• Analysisinput: a positive integer yearoutput: Yes if it is a leap year, and No otherwiserelation:a year is a leap year if and only if it is divided by 4 and not by 100 except it is divided by 400. 1900? 2000? 2004?
• Algorithm 11. get a positive integer, assign it to year
2. If year % 4 ==0 and year % 100 == 0 and year % 400 == 0, then leap = 1
3. If year % 4 ==0 and year % 100 == 0 and year % 400 != 0, then leap = 0
4. If year % 4 ==0 and year % 100 != 0, then leap = 1
5. If year % 4 !=0, then leap = 0
6. Output
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 5
Implementation
/* leap year tester implementation for algorithm 1 */
#include <stdio.h>
main(void){ int year, leap;
/* input a year */ printf("Input a year>\n"); scanf("%d", &year);
/* test if the year is leap */
if (year%4 == 0 && year%100 == 0 && year%400 == 0) leap = 1;
if (year%4 == 0 && year%100 == 0 && year%400 != 0) leap = 0;
if (year%4 == 0 && year%100 != 0) leap = 1;
if (year%4 != 0) leap = 0;
if (leap) printf("%d is a leap year\n", year); else printf("%d is not a leap year", year);
}
• In this algorithm, more time is spent on condition expression evaluation
• Time can be saved by using the simple condition and expression and nested if statement
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 6
Refined Algorithm Using Nested if
• Algorithm 2
1. Get a positive integer year
2. If year % 4 ==0
3. if year % 100 == 0
4. if year % 400 == 0
5. leap = 1
6. else leap = 0
7. else leap = 1
8. else leap = 0
9. Output
• Nested if statements
if ( condition){ if (condition) Statements else Statements}else statements
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 7
Implementation Using Nested if
#include <stdio.h>
main(void)
{
int year, leap;
printf("Input a year>\n");
scanf("%d", &year);
if (year%4 == 0)
{
if (year%100 == 0)
{
if (year%400 == 0) leap = 1;
else leap = 0;
}
else leap = 1;
}
else leap = 0;
if (leap)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year", year);
}
year%4 == 0
leap = 0
Output leap
year%100 == 0
year%400 == 0leap = 1
leap = 0 leap = 1
F T
F T
F T
Input year
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 8
Case Study: Program for Water Bill Problem
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 9
Program for Water Bill Problem (cont’d)
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 10
Program for Water Bill Problem (cont’d)
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 11
Program for Water Bill Problem (cont’d)
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 12
Example 3: Percentage Grade to Letter Grade Convert
• Problem: covert a percentage grade to a letter grade according the table:
• Analysisinput: an integer between 0 and 100output: a letter grade
• Algorithm
1. get a number grade2. If 100>=grade >=90 then assign letter_grade A3. else if 89>=grade >=80 then assign letter_grade B4. else if 79>=grade >=70 then assign letter_grade C5. else if 69>=grade >=50 then assign letter_grade D6. else 49>=grade >=0 then assign letter_grade F7. Print the letter_grade
100~90 89~80 79~70 69-50 49~0
A B C D F
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 13
Implementation for Letter Grade Convert with Nested if Statements
#include <stdio.h>
main(void){ int grade;
char letterGrade;
/* input a percentage grade */
scanf(“Input a percentage grade: %d", &grade); /* find the corresonding letter grade */
if (100 >=grade && grade >=90) letterGrade = ‘A’; else if (89 >=grade && grade >=80) letterGrade = ‘B’; else if (79 >=grade && grade >=70) letterGrade = ‘C’; else if (69 >=grade && grade >=50) letterGrade = ‘D’; else
letterGrade = ‘F’;
/* output the result */ printf(“The corresponding letter grade is %c.”,
letterGrade); }
Syntax form for nested if
if ( condition)
Compound Statements;
else if
Compound statements;
else if
Compound statements;
…
else
Compound statements;