Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5.

Similar presentations


Presentation on theme: "1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5."— Presentation transcript:

1 1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5

2 2 TDBA66, VT-03, Lecture - Ch. 4 Operators (other than aritmetic) Relational operators > = <= (greater than, less than etc.) Equality operators == != (equal, not equal) Logical operators ! && || (not, and, or (inclusive or)) Note –Order of priority (Appendix C) E.g. Relational higher than logical –Symbols: <= must be written like this (no space between < and =)

3 3 TDBA66, VT-03, Lecture - Ch. 4 Relational operators binary and results in 0 or 1. Integer representation of false and true respectively Ex.1 (a != b) Ex. 2 (a >=0) && (a<100) Note : 3 < j < 5 is not true if j equals 7 (logically), but in C the evaluation of 3 < j < 5 is like (3 < j) < 5 i.e. (3 < j) which is true and the result is 1, in the next step the expression 1 < 5 is evaluated which is true and the result is 1 Correct condition in C is (3<j) && (j<5) but 3<j && j<5 is also correct in C due to priority rules

4 4 TDBA66, VT-03, Lecture - Ch. 4 Equality operators == and != (equal to and not equal to) Binary and results in the integer 0 or 1 Ex.1 (expr1 == expr2) (Normally expr1 and expr2 of same data type) Important to realize the difference between = and == Syntactically correct if (a = 0) if (a == 0) …but (a = 0) is an assignment with the value 0, (false), while (a == 0) is a comparison that is true if a equals 0.

5 5 TDBA66, VT-03, Lecture - Ch. 4 Logical operators ! not (almost) && and || or Note : not not s ->  not (not s)  -> s but !!5  !(!5)  -> !(0)  1 Everything ≠ 0 is true

6 6 TDBA66, VT-03, Lecture - Ch. 4 Short-circut evaluation (lazy evaluation) A condition is evaluated until its result is known Ex. 1 expr1 && expr2 If value of expr1 is false the value of expr2 is not evaluated Ex. 2 expr1 || expr2 If value of expr1 is true the value of expr2 does not matter

7 7 TDBA66, VT-03, Lecture - Ch. 4 The compound statement Many statements surrounded by { } No semicolon after } Can contain declarations and statements If declarations are present it is called a block The compound statement can be placed anywhere a statement is legal. Normally it is used where C allows one statement but you would like to execute more than one statement Often in if-statements and loops

8 8 TDBA66, VT-03, Lecture - Ch. 4 if -statements expr is very often a logical expression Figure 4.5 if Statement to Order x and y such that x <= y if (x > y) { /* Switch x and y */ temp = x; /* Store old x in temp */ x = y; /* Store old y in x */ y = temp; } /* Note indentation */ if (expr) statement; if (expr) statement1; else statement2;

9 9 TDBA66, VT-03, Lecture - Ch. 4 The statements in if-statements might be if-statements (nested if- statements) Ex.1 see page 133 in text book if (x > 0) num_pos = num_pos + 1; else if (x < 0) num_neg++; else /* x equals 0 */ num_zero += 1;

10 10 TDBA66, VT-03, Lecture - Ch. 4 The dangling-else-problem else is always associated to the nearest previous if-statement that misses an else Ex.1 if (a <= b) if (a == b) printf(”a is equal to b\n”); else /* bad indentation does not fool the compiler */ printf(”a is less than b\n”); Note: no printing if a > b See also page 140 in the text book

11 11 TDBA66, VT-03, Lecture - Ch. 4 Figure 4.6 Multiple-Alternative if to Calculate Tax /* * Computes the tax due based on a tax table. * Returns the tax due for 0.0 <= salary <= 150,000.00; * returns -1.0 if salary is outside the table range. */ if (salary < 0.0) tax = -1.0; else if (salary < 15000.00)/* first range */ tax = 0.15 * salary; else if (salary < 30000.00)/* second range*/ tax = (salary - 15000.00) * 0.18 + 2250.00; else if (salary < 50000.00)/* third range*/ tax = (salary - 30000.00) * 0.22 + 5400.00; else if (salary < 80000.00)/* fourth range*/ tax = (salary - 50000.00) * 0.27 + 11000.00; else if (salary <= 150000.00)/* fifth range*/ tax = (salary - 80000.00) * 0.33 + 21600.00; else tax = -1.0;

12 12 TDBA66, VT-03, Lecture - Ch. 4 switch -statement Better than nested if-statements in certain cases Controlling expr must be of ”integral-type” (int or char) The values must be unique The statements might be empty switch (controlling expr) { case value1 : statements; case value2 : statements; case value3 : statements; …… default : statements; /* can be excluded */ }

13 13 TDBA66, VT-03, Lecture - Ch. 4...switch The controlling expr is evaluated If the value of the controlling expr is equal to one of the values in the case labels the execution continues sequentially from there If the controlling value is not found among the case labels execution is continued in the default-alternative (if that is present) Normally the last statement in all case alternatives is break

14 14 TDBA66, VT-03, Lecture - Ch. 4 Figure 4.10 Example of a Switch-statement with Type char Case Labels switch (sclass) { case ’B’: case ’b’:printf("Battleship\n"); break; case ’C’: case ’c’:printf("Cruiser\n"); break; Case ’D’: case ’d’: printf("Destroyer\n"); break; case ’F’: case ’f’: printf("Frigate\n"); break; default: printf("Unknown ship class %c\n", sclass); }

15 15 TDBA66, VT-03, Lecture - Ch. 4 Solve programming project 1 in Ch. 4 (page 158) In short: write a program that reads one character and prints different messages depending on the read charcter Read characterInterpretationPrint NNegativeMessage 1 PPositiveMessage 2 BBothMessages 1 and 2 ZNeitherMessage 3 Use simple functions: one to write Message1 and the other one to write Message 2

16 16 TDBA66, VT-03, Lecture - Ch. 4 peppar:~/c/Ckod>./proj4_1 Type one of the characters P, N, B and Z: P My analysis of sample with code P is Gram-postive: Perform standard tests 1 and 5. Record results in notebook #2 peppar:~/c/Ckod> !!./proj4_1 Type one of the characters P, N, B and Z: n My analysis of sample with code n is Gram-negative: Perform standard tests 2, 3 and 4. Record results in notebook #3 Example runs of proj4_1.c


Download ppt "1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5."

Similar presentations


Ads by Google