selection. structured components there are three basic constructs that can be used to build programs...
Post on 22-Dec-2015
213 views
TRANSCRIPT
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.
Selection In C
If statement Many forms
Switch statement Implements a case structure
Compound Statement
Groups a set of statements together under one scope.
Used in selections.
{
statement1;
statement2;
statement3;
}
Function Body
Uses a compound statement.
int MyFunction(int a, int b, int c)
{
int ReturnValue;
ReturnValue = a + b + c;
return ReturnValue;
}
Compound Statement
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}
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
Relational Operators
< less than <= less than or equal to > greater than >= greater than or equal to
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.
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.
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.
Watch comparing float to zero
NEVER NEVER NEVER NEVER NEVER NEVER if ( float_variable ==
0.0 ) ..
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
Precedence?
Study the book carefully. Or, just use parentheses.
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.
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)
Character Relational
Lexicographic (alphabetical) order is used.
‘a’ > ‘c’ is false ‘d’ < ‘f’ is true
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) …
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)
Don’t Use Flow Charts
They are cumbersome. They are old fashioned. They limit your thinking. Use pseudocode.
IF ( ) THEN { } ELSE { }
if (a < b)
{ compound1 } <- If a<b is true execute this.
else
{ compound2 } <- If a<b is false execute this.
One Statement ? No {}
if (d<e)
statement1;
else
statement2;
If (f<=g)
statement1;
Null else branch!
If (h==i)
else
statement2;
Seldom used!
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.
Nested If Statements
Obviously, a compound statement may contain more if statements.
Logic can be a bit tricky if they get too deep!
Switch Statement
A range of choices.switch( size ) {
case 2 :
case 3 :
printf(“small\n”);
break;
case 4 :
printf(“medium”\n);
break;
}
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?
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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: