Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection.

Similar presentations


Presentation on theme: "Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection."— Presentation transcript:

1 Selection

2 Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection Choose what statements to execute. Repetition Repeat statements.

3 Selection In C If statement Many forms Switch statement Implements a case structure

4 Compound Statement Groups a set of statements together under one scope. Used in selections. { statement1; statement2; statement3; }

5 Function Body Uses a compound statement. int MyFunction(int a, int b, int c) { int ReturnValue; ReturnValue = a + b + c; return ReturnValue; } Compound Statement

6 The if statement Condition is an expression that evaluates to false (0) or true (≠0). If it evaluates to true, the first compound statement is executed. If it evaluates to false, the second compound statement is executed. if Condition {do if true} else {do if false}

7 So, What Is A Condition? variable relational-operator variable a < b variable relational-operator constant a > 3 variable equality-operator variable d == e variable equality-operator constant fire != 12

8 Relational Operators < less than <= less than or equal to > greater than >= greater than or equal to

9 Equality Operators == Equal to. != Not equal to. Be sure to remember both = signs on the equal to operator. Be sure to review Table 4.2 This author calls 1 true. Since 1 is ≠ 0, he’s right. But 2 is true, 3 is true, 129 is true. Anything other than 0.

10 Logical Expression rel_exress logical_operator rel_express Logical Operators && And || Or ! Not If ( a d ) … If a d do true compound statement. If either is false, do false compound statement.

11 Logical Expression (cont.) if (e >= f || gift == snow) … If either is true, execute true compound statement. If both are false, execute false compound statement if ! (cost < 12.0) … If cost is less than 12.0, execute false. If cost is greater than or equal to 12.0 execute true.

12 Watch comparing float to zero NEVER NEVER if ( float_variable == 0.0 )..

13 Did I say NEVER!! The reason is that internal representations for float or double variables can have a bit set that will fail a comparison to zero when the value is basically zero. Better to do the following: if ( fabsf(float_variable) < some_epsilon ) … Trust Me! (or trust someone else!) Just Trust! www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

14 Precedence? Study the book carefully. Or, just use parentheses.

15 Short Circuit Evaluation If the first relational expression in an AND logical expression is false, the rest of the condition is not executed. The result is false. If the first relational expression in an OR logical expression is true, the rest of the condition is not executed. The result is true.

16 Example if (a d) … If a d is not evaluated. If (e != f || g<h) … If e != f is true, g<h is not evaluated. With relational, this isn’t to significant. But if the second relational also changes a value, this can be very significant. (More Later)

17 Character Relational Lexicographic (alphabetical) order is used. ‘a’ > ‘c’ is false ‘d’ < ‘f’ is true

18 Logical Assignment The book shows using an int. Most people define a separate variable type bool to make programs more readable. result = a < b; The comparison is made and the true or false is stored in result. if (result) …

19 DeMorgan’s Theorem To complement an && expression, not both the relationals and change the && to ||. To complement an || expression, not both the relationals and change the || to &&. !(a d) is the same as !(a d)

20 Don’t Use Flow Charts They are cumbersome. They are old fashioned. They limit your thinking. Use pseudocode.

21 IF ( ) THEN { } ELSE { } if (a < b) { compound1 } <- If a<b is true execute this. else { compound2 } <- If a<b is false execute this.

22 One Statement ? No {} if (d<e) statement1; else statement2; If (f<=g) statement1; Null else branch! If (h==i) else statement2; Seldom used!

23 Book Case Studies They have some nice case study examples. Don’t lose the benefits by skipping over them too lightly. If you see something funny, bring it up in class.

24 Nested If Statements Obviously, a compound statement may contain more if statements. Logic can be a bit tricky if they get too deep!

25 Switch Statement A range of choices. switch( size ) { case 2 : case 3 : printf(“small\n”); break; case 4 : printf(“medium”\n); break; }

26 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints?

27 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d&& Output:

28 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562 && Output:

29 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && Output:

30 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 Output:

31 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 Output:

32 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 False Output:

33 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 False Output:

34 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 True Output:

35 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 so long Output:

36 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 so long Output:

37 Hand Trace int main ( void ) { int a=3, b=4, c=5, d=6,total=2; if( a d ) total = 3; else total = 4; switch (total) { case 2: printf(“hello\n”); break; case 3: printf(“good-bye\n”); break; default: printf(“so long\n”); } Given this code, what prints? abcdtotala<bc>d 34562TF && F 4 so long main exits Output:


Download ppt "Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection."

Similar presentations


Ads by Google