Beruflich Dokumente
Kultur Dokumente
SELECTION STRUCTURE
Use selection control structure to represent decision in algorithm and use pseudo code
to write them in algorithm
Use expression whose values indicate whether certain condition are true or false
use logical operator to handle complex logical expression
4.1
INTRODUCTION
Conditions or expression make a program choose alternative statements by testing value. Condition
specifies decision. It results in either a true or false answer only. A statement specifies an action to be
performed. Statements are executed in sequence. A selection statement selects among a set of
statements depending on the value of controlling expression. Types of selection statement: if, if
else, nested if else or else if and switch.
An if selection statement either performs (selects) an action if a condition is true or skips the action if the
condition is false. If else selection statement performs an action if a condition is true and performs a
different action if the condition is false. Nested if statement is used to code decisions with multiple
alternatives include one or more if statements inside an existing if statement. A switch statement used to
select one of several alternatives.
4.2
GENERAL INFORMATION
4.2.1 Syntax
1) if
if(expression)
statement
E.g.
if ( grade >= 60 ) {
printf( "Passed\n" );
}
2) if-else
if(expression)
statement1
else
statement2
E.g.
if ( grade >= 60 ) {
printf( "Passed\n" );
}
else {
printf( "Failed\n" );
}
E.g.
if ( grade >= 90 )
printf( "A\n" );
else if ( grade >= 80 )
printf( "B\n" );
else if ( grade >= 70 )
printf( "C\n" );
else if ( grade >= 60 )
printf( "D\n" );
else
printf( "F\n" );
or rearrange
as
if(expression1)
statement1
else
if(expression2)
statement2
else
if(expression3)
statement3
else
statement4
E.g.
if ( grade >= 90 )
printf( "A\n" );
else
if ( grade >= 80 )
printf("B\n");
else
if ( grade >= 70 )
printf("C\n");
else
if ( grade >= 60 )
printf( "D\n" );
else
printf( "F\n" );
E.g.
switch (number)
{
case 1:
printf("ONE");
break;
case 2:
4) switch
switch(expression) {
case const-expr1:
statement1
break;
case const-expr2:
statement2
break;
default:
statement
break;
}
4.3
EXERCISES
1. Write an expression to test each of the following relationship:
a. age is from 18 to 21 inclusive
b. water is less than 1.5 and also greater than 0.1
c. speed is not greater than 55
d. y is greater than x and less than z
e. w is either equal to 6 or not greater than 3.
2. What do this statement display:
if (12<12)
printf(less)
else
printf(not less)
3. What value is assign to x when y is 10?
if(y<15.0&&y>=0.0)
x=5*y;
else
x=2*y;
4. What value is assign to x when y is 15?
if(y<15.0)
if(y>=0.0)
x=5*y;
else
x=2*y;
else
x=3*y;
5. Insert braces where they are needed so the meaning matches the indentation:
if (x>y)
x=x+10.0;
printf(x Bigger\n);
else
printf(x Smaller\n);
printf(y is %.2f\n,y);
20 3
4 + 2 > 3
10. The valid grades for a class are A, B, C, D and F corresponding to the scores 4, 3, 2, 1, and 0
respectively. Write a program to print out the score on when user enters a grade. Use:
a. nested if else selection structure
b. switch selection structure
11. Implement the following decision table using a nested if statement . Assume the wind is an
integer value
Wind speed(mph)
Below 25
25-38
39-54
55-72
Above 72
Category
Not a strong wind
Strong wind
Gale
Whole gale
hurricane
Reference:
1. Deitel, Paul and Deitel, Harvey. 2010. C How To Program, Sixth Edition. Perason, 2010.
2. Hanly, Jeri R. and Koffman, Elliot B. 2010. Problem Solving and Program Design in C, Sixth
Edition. Pearson, 2010.
3. Cheng, H. Harry. 2010. C for engineers and scientists: An Interpretive Approach. McGrawHill, 2010.