selection. structured components there are three basic constructs that can be used to build programs...

37
Selection

Post on 22-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Selection

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

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.

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

Selection In C

If statement Many forms

Switch statement Implements a case structure

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

Compound Statement

Groups a set of statements together under one scope.

Used in selections.

{

statement1;

statement2;

statement3;

}

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

Function Body

Uses a compound statement.

int MyFunction(int a, int b, int c)

{

int ReturnValue;

ReturnValue = a + b + c;

return ReturnValue;

}

Compound Statement

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

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}

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

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

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

Relational Operators

< less than <= less than or equal to > greater than >= greater than or equal to

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

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.

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

Logical Expression rel_exress logical_operator

rel_express Logical Operators

&& And || Or ! Not

If ( a < b && c > d ) … If a < b and c > d do true compound

statement. If either is false, do false compound statement.

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

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.

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

Watch comparing float to zero

NEVER NEVER NEVER NEVER NEVER NEVER if ( float_variable ==

0.0 ) ..

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

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

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

Precedence?

Study the book carefully. Or, just use parentheses.

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

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.

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

Example if (a<b && c>d) …

If a<b is false, c>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)

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

Character Relational

Lexicographic (alphabetical) order is used.

‘a’ > ‘c’ is false ‘d’ < ‘f’ is true

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

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) …

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

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<b && c>d) is the same as !(a<b) || !(c>d)

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

Don’t Use Flow Charts

They are cumbersome. They are old fashioned. They limit your thinking. Use pseudocode.

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

IF ( ) THEN { } ELSE { }

if (a < b)

{ compound1 } <- If a<b is true execute this.

else

{ compound2 } <- If a<b is false execute this.

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

One Statement ? No {}

if (d<e)

statement1;

else

statement2;

If (f<=g)

statement1;

Null else branch!

If (h==i)

else

statement2;

Seldom used!

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

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.

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

Nested If Statements

Obviously, a compound statement may contain more if statements.

Logic can be a bit tricky if they get too deep!

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

Switch Statement

A range of choices.switch( size ) {

case 2 :

case 3 :

printf(“small\n”);

break;

case 4 :

printf(“medium”\n);

break;

}

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d &&

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2

&&

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

False

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4False

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

True

Output:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

so longOutput:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

so longOutput:

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

Hand Traceint main ( void ){ int a=3, b=4, c=5, d=6,total=2; if( a<b && c>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?

a b c d total a<b c>d

3 4 5 6 2 T F

&&

F

4

so long

main exits

Output: