control structures 4 control structures control the flow of execution of a program 4 the categories...
Post on 15-Jan-2016
232 views
TRANSCRIPT
Control Structures
Control structures control the flow of execution of a program
The categories of control structures are:– Sequence– Selection– Repetition
A compound statement is used to specify sequential flow
Control Structures
In sequential flow, control flows from one statement to the next in the compound statement
This is the control structure we have been using up until now
In this lesson, we will introduce the selection control structures which let us choose between alternative statements
Conditions
A program chooses among alternative statements by evaluating program variables– age > 65 /* age an int variable */
– This expression is an example of a condition– A condition establishes a criterion for executing
or skipping a group of statements– A condition evaluates to either true
(represented by 1) or false (represented by 0)
Relational & Equality Operators
The most common conditions will have one of the following forms:– variable relational-operator variable– variable relational-operator constant– variable equality-operator variable– variable equality-operator constant– The constant may also be the first operand
Relational & Equality Operators
The relational and equality operators in C are the following:– < less than– > greater than– <= less than or equal to– >= greater than or equal to– == equal to – != not equal to
Logical Operators
We can form more complicated conditions using the logical operators && (and), || (or), ! (not)– salary < MIN_SALARY || dependents > 5– temperature > 90.0 && humidity > 0.90– n >= 0 && n <= 100– 0 <= n && n <= 100– !(0 <= n && n <= 100)
Logical expressions are evaluated as are arithmetic expressions
The && Operator (and)
operand1 operand2 operand1 &&operand 2
nonzero (true) nonzero (true) 1 (true)
nonzero (true) 0 (false) 0 (false)
0 (false) nonzero (true) 0 (false)
0 (false) 0 (false) 0 (false)
The || Operator (or)
operand1 operand2 operand1 ||operand2
nonzero (true) nonzero (true) 1 (true)
nonzero (true) 0 (false) 1 (true)
0 (false) nonzero (true) 1 (true)
0 (false) 0 (false) 0 (false)
The ! Operator (not)
operand1 !operand1
nonzero (true) 0 (false)
0 (false) 1 (true)
Operator Precedence
We can mix arithmetic and logical operators (x+5 == y) thus we must know the precedence of both types of operators– ! + - & (unary operators)– * / %– + -– < <= >= >– == !=– &&– ||– =
Operator Precedence
(x < y || x < z) && x > 0.0 x + y < min + max (x + y) < (min + max)
Operator Precedence
Let us assume that the following variables have the following values: x 3.0, y 4.0, z 2.0, flag 0.– !flag– x + y /z <= 3.5– !flag || (y + z >= x - z)– !(flag || (y + z >= x - z))
Short-Circuit Evaluation
If we have a logical expression with an ‘or’ operator and the first operand evaluates to ‘true’, C doesn’t evaluate the second operand (Why?). This is known as short-circuit evaluation
This also works for the ‘and’ operator where the first operand is ‘false’
Why is it important to know this feature of C?
Comparing Characters
We can also compare characters using the relational and equality operators– ‘9’ >= ‘0’– ‘a’ < ‘e’– ‘Z’ == ‘z’– ‘a’ <= ‘A’
Logical Assignment
The simplest form of a logical expression in C is a single int value or variable– 0 represents false– A non-zero value represents true
We can use the assignment statement to give a logical value to a variable:– senior_cititzen = 1;– senior_citizen = (age >= 65);
Logical Assignment
We can now use the variable in a logical expression– senior_citizen && gender == ‘M’– Evaluates to 1 (true) if senior_citizen is
1 (true) and the character in gender is M
Complementing a Condition
A logical expression may be complemented by preceding it with the ! Operator
A simple condition can be complemented by changing the condition
DeMorgan’s theorem tells us that the complement of expr1 && expr2 is comp1 || comp2, the complement of expr1 || expr2 is comp1 && comp2
Complementing a Condition
We can write the complement of – age > 25 && (status == ‘S’ || status == ‘D’)
– age <= 25 || (status != ‘S’ && status != ‘D’)
The if Statement
Now that we are able to write conditions, we will use these conditions to choose among alternative groups of statements
The C statement which accomplishes this is the if statement
An example of an if statement isif (rest_heart_rate> 56)
printf(‘Keep up your exercise program!\n’);
The if Statement
if (rest_heart_rate> 56)
printf(‘Keep up your exercise program!\n’);
else
printf(‘Your heart is in great health!\n’);
This is an example of an if statement with two alternatives– Here, each alternative is a single statement, but
in general each alternative can be a compound statement
The if Statement
We can also write an if statement with a single alternativeif (x != 0.0)
product = product * x;
In this case, if the condition is false, we simply go on to the statement following the if
The if Statement
If we want to have more than one statement in an alternative, we must use the compound statement constructif (x != 0.0)
result = y / x;
else {
printf(‘You can’t do this!\n’);
result = 0.0;
}
The if Statement
if (x != 0.0)
result = y / x;
else
printf(‘You can’t do this!\n’);
result = 0.0;
What happens in this case?– Indentation has no effect on program
execution!
The if Statement
The general form of the if statement with two alternatives where both alternatives have multiple statements is:if (condition){
true task
}
else {
false task
}
The if Statement
Imagine that we have two variables in a program. One, sex, is a char variable which will have the value ‘m’ or ‘f’. The second, age, is an int variable which holds a person’s age. Write an if statement which examines the variables and prints out ‘Eligible’ for females 63 and older and males 65 and older, and ‘Ineligible’ for everyone else
The if Statement
We may also need to choose among multiple alternatives (e.g. different prices for men, women, and children). For these cases, we use the nested form of the if statementif (type == ‘m’) price = 25;
else if (type == ‘f’) price = 20;
else /* type is ‘c’ */
price = 15;
The if Statement
The general form for the nested if statement is (each statement may be compound):
if (condition1)
statement1
else if (condition2)
statement2
. . .
else if (conditionn)
statementn
else
statemente