Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 5.

Similar presentations


Presentation on theme: "Lec 5."— Presentation transcript:

1 Lec 5

2 Lecture Outline Multiple Alternative if Counters Nested If statement
Switch Statement

3 Review of Syntax Simple if Statement if (condition) statement;
if (condition) statement; else statement; Compound if Statement if (condition) {statements} if (condition) {statements} else {statements}

4 Exercise Write an interactive program that contains an if statement that may be used to compute the area of a square (area = side²) or a triangle (area = 1/2 * base * height) after prompting the user to type the first character of the figure name (S or T).

5 Multiple Alternative Decision
Syntax if (condition) statement; else if (condition) statement; : else statement;

6 alternative if statement
Comparing if Statements if (score>=90) printf("A"); if (score>=80 && score<90) printf("B"); if (score>=70 && score<80) printf("C"); if (score>=60 && score<70) printf("D"); if (score<60) printf("F"); 5 simple if statements if (score>=90) printf("A"); else if (score>=80) printf("B"); else if (score>=70) printf("C"); else if (score>=60) printf("D"); else printf("F"); 1 multiple alternative if statement

7 Exercise 1 Write a program that accepts a score from the user and displays the grade for that score. Example: if the user enters a 75, the program will display "C".

8 Exercise 1 #include<stdio.h> int main(void) {float score;
printf("Please enter a score: "); scanf("%f", &score); if (score>=90) printf("A\n"); else if (score>=80) printf("B\n"); else if (score>=70) printf("C\n"); else if (score>=60) printf ("D\n"); else printf("F\n"); return(0);}

9 Counters A counter is a variable whose value is either added to or
subtracted from to achieve a result. An increment counter is a variable whose value is added to. A decrement counter is a variable whose value is subtracted from. All counters MUST be initialized before using. All counters MUST be of type int. Examples: x=x+1; stock=stock-1; hot=hot+1; x=x-1; stock=stock-100; cold=cold+1;

10 Nested if statement A nested if is an if statement with another if statement as its true task. A real nested if is found in the true task of an if statement and not the false task. If an if statement is found in the false task then it’s a multiple alternative if. if (condition) if (condition) statement; else statement; else statement; Syntax

11 Example true true Wet roads Message Status='S' temp>0 false false
Icy roads Message Drive carefully Message

12 Example #include<stdio.h> /* a program to determine road conditions */ int main(void) {char status; float temp; printf("Please enter the road status: "); scanf("%c",&status); if (status=='S' || status=='s') {printf("Please enter the temperature: "); scanf("%f", &temp); if (temp>0) printf("Wet Roads!\n"); else printf ("Icy Roads!\n");} else printf ("Drive carefully\n"); return (0);}

13 Example 2 true true Wet roads Message Status='S' temp>0 false false
Drive carefully Message

14 Example 2 if (status=='S' || status=='s')
if (temp>0) printf("Wet Roads!\n"); else printf ("Drive carefully\n"); if (status=='S' || status=='s') {if (temp>0) printf("Wet Roads!\n");} else printf ("Drive carefully\n");

15 Switch Statement switch (controlling expression) { label_set_1:
Syntax switch (controlling expression) { label_set_1: statements_1 break; label_set_2: statements_2 . Label_set_n: statements_n default: statements_d } Selection Structures 2

16 Example 1 printf("Please enter a color code (R, B, or Y): ");
scanf("%c",&color); switch (color) { case 'R': printf("Red\n"); break; case 'B': printf("Blue\n"); case 'Y': printf("Yellow\n"); } Selection Structures 3

17 Example 2 printf("Enter watts (25, 40, 60, 75, or 100): ");
scanf("%i",&watts); switch (watts) { case 25: life=2500; break; case 40: case 60: life=1000; case 75: case 100: life=750; default: life=0; } printf("The life of your watts is %i\n",life); Selection Structures 4

18 Example 3 printf("Please enter your grade: "); scanf("%c",&grade);
switch (grade) { case 'A': case 'a': printf("Excellent!\n"); break; case 'B': case 'b': printf("Very Good!\n"); case 'C': case 'c': printf("Fair.\n"); default: printf("Poor\n");} Selection Structures 5

19 Example 4 Write a program that reads a number from the user and determines if it's a positive or negative number. Can this problem be implemented using a switch statement? #include<stdio.h> int main(void) {int number; printf("Please enter a number: "); scanf("%i", &number); if (number>=0) printf("Positive\n"); else printf("Negative\n"); return(0);} Selection Structures 11

20 Comparison of if Statements and The Switch Statement
if statements are more general than the switch statement. Case labels that contain type float values or strings are not permeated in switch statements. The switch statement is more readable in many contexts. Thus, it should be used whenever practical. Selection Structures 12

21 Switching the values of two variables
Exercise 1 Write a program that switches the values of two variables. printf ("The value of x is %i", y); printf ("The value of y is %i", x); temp = x; x = y; y = temp;

22 Solution #include<stdio.h> int main(void)
{int first, second, temp; printf("Please enter two numbers: "); scanf("%i%i", &first, &second); printf("The value of first number is %i\n", first); printf("The value of second number is %i\n", second); temp=first; /* Switch them */ first=second; second=temp; printf("The value of first number after switching is %i\n", first); printf("The value of second number after switching is %i\n", second); return(0);}

23 Exercise 2 Write a program that reads an integers from the user, then finds & prints: The value of the (units) digit. The value of the (tens) digit. The value of the (hundreds) digit.

24 Solution #include <stdio.h> int main (void)
{int number, units, tens, hundreds; printf("Please enter a number: "); scanf("%i", &number); units=number%10; tens=(number%100)/10; hundreds=(number%1000)/100; printf("Units = %i\n", units); printf("Tens = %i\n", tens); printf("Hundreds = %i\n", hundreds); return(0);}


Download ppt "Lec 5."

Similar presentations


Ads by Google