Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing a program with conditionals

Similar presentations


Presentation on theme: "Writing a program with conditionals"— Presentation transcript:

1 Writing a program with conditionals
Example: print the percentage tax based on income if income < 15000 0% if  income < 30000 18% if  income < 50000 22% if  income < 28% if  income 31% Simple solution if (income<15000) printf(“No tax\n”); if (income>=15000 && income<30000) printf(“18%% tax\n”); if (income>=30000 && income<50000) printf(“22%% tax\n”); if (income>=50000 && income<100000) printf(“28%% tax\n”); if (income>=100000) printf(“31%% tax\n”); Mutually exclusive: only one is true 142 I -1

2 Using nested ifs if (income < 15000) else if (income < 30000)
Cascaded ifs if (income < 15000) printf(“No tax\n”); else if (income < 30000) printf(“18%% tax\n”); else if (income < 50000) printf(“22%% tax\n”); else if (income < ) printf(“28%% tax\n”); else printf(“31%% tax\n”); which can be written order of the conditions is important. Conditions are successively inclusive. if (income < 15000) printf(“No tax\n”); else if (income < 30000) printf(“18%% tax\n”); else if (income < 50000) printf(“22%% tax\n”); else if (income < ) printf(“28%% tax\n”); else printf(“31%% tax\n”); 142 I -2

3 Other Examples 1) Read 3 characters
Print the first one in alphabetical order e.g. p, g and m, print g Reminder: character: individual character keyboard (or an escape sequence ‘\n’) in C, char However, for the computer, characters are just integers: different character sets ASCII, EBCDIC... (see text appendix) …< & < … < 5 < … < A < B < … < a < … < z < … < } < … 38 53 65 66 97 122 125 The computer can compare characters (in a program, use <, >, <=, >=, !=, ==) 142 I -3

4 char first(char c1, char c2, char c3) { char first; first = c1;
as a function: char first(char c1, char c2, char c3) { char first; first = c1; if (c2<first) first = c2; if (c3<first) first = c3; return first; } 2) Input 2 characters Rearrange them in sorted order Output them in sorted order e.g. input ra, output ar char c1, c2, temp; printf(“Enter 2 characters: ”); scanf(“%c%c”,&c1,&c2); if (c2<c1) { temp = c1; c1 = c2; c2 = temp; } printf("In alphabetical order: %c %c",c1,c2); why not c1 = c2; c2 = c1; ? 142 I -4

5 De Morgan’s law !(P && Q) is equivalent to !P || !Q
e.g. if ( !(age > 50 && smoker == ‘Y’) ) printf(“Cheaper health insurance”); is equivalent to if ( age <= 50 || smoker != ‘Y’ ) printf(“Cheaper health insurance”); Check with a logical table P, Q are conditions T and F stand for True and False T F F T F T F T F T 142 I -5

6 Operator Precedence Check inside cover of the text == ! - + !=
-(unary) - + == != * / % < > <= >= && || lower precedence (evaluate last) higher (evaluate first) Consider a = 2; b = 4; z = (a+3 >=5 && !(b<5)) || a*b+b != 7; What is the value of z (1 or 0)? (Recall that True is 1 and False is 0) z is 1 142 I -6

7 Pitfalls of if (1) What is wrong in each of these code snippets?
printf is not part of the if statement if (x>10); printf(“x>10”); obscure: better x!=0 if (x) printf(“x is non zero”); if (x=10) printf(“x is 10”); = is not a relational operator! Use == Whatever the value of x before, after executing this statement x is 10 142 I -7

8 Pitfalls of if (2) if (0 <= x <= 10)
printf(“x is between 0 and 10”); What happens if x = 11? x is between 0 and 10 is printed! (0<=x is true thus 1 and 1<=10) Write: if (0<=x && x<=10) printf(“x is between 0 and 10”); Also: In C, & and | are operators for binary numbers NOT the same as && and || 142 I -8

9 Avoid: Using == and != with doubles: Because of round off errors,
2 equivalent mathematical computations can lead to different results double x; x=(sqrt(2)+sqrt(2))*(sqrt(2)+sqrt(2)); if (x == 8.0) printf(“x is 8.0”); printf is not executed with some compilers long winded ifs (difficult to understand): /* how many days in a month */ if (month == 1) days = 31; else if (month == 2) days = 28; ... 12 of these (without including the leap year case!) Can do better! 142 I -9

10 Better Best: using switch if (month == 4 || month == 6 ||
days = 30; /* April, June, Sept, Nov */ else if (month == 2) days = 28; /* February */ else days = 31; /* all the rest */ Best: using switch switch(month) { case 2: days = 28; break; case 4: case 6: case 9: case 11: days = 30; default: days = 31; } printf(“There are %i days in that month”,days); if month is 6, execution within switch starts here and ends here 142 I -10

11 Syntax of switch switch(control expression) { case constant1:
... statement1; statement2; break; case constant3: case constant4: statement3; statement4; default: statement5; } an int or a char must be a constant e.g. 1, 2 , -3 ‘A’, ‘c’, do not forget fall through (everything between the case line and the next break is executed) optional but might prevent a fall through when adding cases after the default (e.g. in a subsequent update) 142 I -11

12 another switch example
char order; printf("Please, type b or l to order\n"); printf("Breakfast (b) or lunch (l): "); scanf("%c",&order); switch(order) { case ‘b’: case ‘B’: printf(“Breakfast\n”); break; case ‘l’: case ‘L’: printf(“Lunch\n”); default: printf(“Sorry, I don’t understand”); } 142 I -12


Download ppt "Writing a program with conditionals"

Similar presentations


Ads by Google