Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1 If statement (preview version) Syntax form: if ( condition) Statements else Statements.

Similar presentations


Presentation on theme: "CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1 If statement (preview version) Syntax form: if ( condition) Statements else Statements."— Presentation transcript:

1 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 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"); }

2 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 2 Flowchart for if Statements Two AlternativesOne Alternative a == b a and b equal a and b not equal exit a == b A and b are equal F T T F

3 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, c 2.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, c 2.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, c 2.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

4 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 4 Example 2: find if a year is a leap year Analysis input: a positive integer year output: Yes if it is a leap year, and No otherwise relation: 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 1 1.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

5 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 5 Implementation /* leap year tester implementation for algorithm 1 */ #include 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

6 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

7 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 7 Implementation Using Nested if #include 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 == 0 leap = 1 leap = 0leap = 1 FT FT FT Input year

8 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 8 Case Study: Program for Water Bill Problem

9 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 9 Program for Water Bill Problem (cont’d)

10 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 10 Program for Water Bill Problem (cont’d)

11 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 11 Program for Water Bill Problem (cont’d)

12 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: Analysis input: an integer between 0 and 100 output: a letter grade Algorithm 1.get a number grade 2.If 100>=grade >=90 then assign letter_grade A 3.else if 89>=grade >=80 then assign letter_grade B 4.else if 79>=grade >=70 then assign letter_grade C 5.else if 69>=grade >=50 then assign letter_grade D 6.else 49>=grade >=0 then assign letter_grade F 7.Print the letter_grade 100~9089~8079~7069-5049~0 ABCDF

13 CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 13 Implementation for Letter Grade Convert with Nested if Statements #include 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;


Download ppt "CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1 If statement (preview version) Syntax form: if ( condition) Statements else Statements."

Similar presentations


Ads by Google