Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.

Similar presentations


Presentation on theme: "CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel."— Presentation transcript:

1 CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel

2 Example 1 “ x is not equal to either 2 or 3” if (x != 2 || x != 3) No, it should be if (!(x == 2 || x == 3)) or if (x != 2 && x != 3)

3 Example 2 “x is in the range from 0 to 10 exclusive” if (0 < x < 10) No, it should be if (0 < x && x < 10)

4 Example Leap year every fourth year, except centuries, then just every fourth century –year is divisible by 4 but not by 100, or –year is divisible by 400 Try ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)

5 Bad Example if (x < 5) cout << ”A”; if (x > 5) cout << ”B”; else cout << ”C”;

6 If Blocking Rule For any if statement that requires (1) more than a single line, or (2) an else clause, always use braces to enclose in a separate block the statements under the control of the if statement.

7 Single-line if statement if ( condition ) statement where: condition is the Boolean value to test statement is a single statement to be executed if condition is true

8 Multiline if statement if ( condition ) { statements; } where condition is the Boolean value to test statements is a block of statements to be executed if condition is true

9 if-else statements if ( condition ) { statements T ; } else { statements F ; }

10 Cascading if statement if ( condition 1 ) { statements 1 ; } else if ( condition 2 ) { statements 2 ;... } else { statements none }

11 Crafting a Program Use comments to tell readers what they need to know, including difficult code Use indentation to show bodies of loops Use meaningful names Use convention for names - lastNumber Use standard idioms when appropriate Avoid unnecessary complexity

12 Designing for Change #define construct - symbolic constants #define symbol value symbol - name for symbolic constant value - replaces symbol in precompilation Why? Easier to modify program

13 Named Constants A variable whose content cannot be changed while program is running const double PI = 3.14159; Variable still has data type and memory location, but is read-only

14 Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls cout << ”Hello.\n”; Useless statements n1 + n2;

15 Embedded Assignments Assignment expression can be used as part of a larger expression in a statement Its value is the value assigned z = (x = 6) + y; x is assigned value 6, then z assigned 6 + y Difficult to read Used rarely and only when makes sense

16 Multiple Assignments Embedded assignments useful to set several variables to the same value n1 = n2 = n3 = 0; Assignment operator evaluated right to left Avoid mixed types; e.g. double d and int i d = i = 1.5; Assigns i value 1, thus 1 assigned to d

17 Math Library Functions for performing math operations abs(x) absolute value of argument sqrt(x) square root of argument pow(y, x) y x sin(x) sine (argument in radians) cos(x) cosine (argument in radians) tan(x) tangent (argument in radians) log(x) natural logarithm exp(x) e x


Download ppt "CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel."

Similar presentations


Ads by Google