Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition 4 A compound statement is used to specify sequential flow
Control Structures 4 In sequential flow, control flows from one statement to the next in the compound statement 4 This is the control structure we have been using up until now 4 In this lesson, we will introduce the selection control structures which let us choose between alternative statements
Conditions 4 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 4 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 4 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 4 We can form more complicated conditions using the logical operators && (and), || (or), ! (not) –salary 5 –temperature > 90.0 && humidity > 0.90 –n >= 0 && n <= 100 –0 <= n && n <= 100 –!(0 <= n && n <= 100) 4 Logical expressions are evaluated as are arithmetic expressions
The && Operator (and)
The || Operator (or)
The ! Operator (not)
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 0.0 x + y < min + max (x + y) < (min + max)
Operator Precedence 4 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 4 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 4 This also works for the ‘and’ operator where the first operand is ‘false’ 4 Why is it important to know this feature of C?
Comparing Characters 4 We can also compare characters using the relational and equality operators –‘9’ >= ‘0’ –‘a’ < ‘e’ –‘Z’ == ‘z’ –‘a’ <= ‘A’
Logical Assignment 4 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 4 We can use the assignment statement to give a logical value to a variable: –senior_cititzen = 1; –senior_citizen = (age >= 65);
Logical Assignment 4 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 4 A logical expression may be complemented by preceding it with the ! Operator 4 A simple condition can be complemented by changing the condition 4 DeMorgan’s theorem tells us that the complement of expr 1 && expr 2 is comp 1 || comp 2, the complement of expr 1 || expr 2 is comp 1 && comp 2
Complementing a Condition 4 We can write the complement of –age > 25 && (status == ‘S’ || status == ‘D’) –age <= 25 || (status != ‘S’ && status != ‘D’)
The if Statement 4 Now that we are able to write conditions, we will use these conditions to choose among alternative groups of statements 4 The C statement which accomplishes this is the if statement 4 An example of an if statement is if (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’); 4 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 4 We can also write an if statement with a single alternative if (x != 0.0) product = product * x; 4 In this case, if the condition is false, we simply go on to the statement following the if
The if Statement 4 If we want to have more than one statement in an alternative, we must use the compound statement construct if (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; 4 What happens in this case? –Indentation has no effect on program execution!
The if Statement 4 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 4 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 4 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 statement if (type == ‘m’) price = 25; else if (type == ‘f’) price = 20; else /* type is ‘c’ */ price = 15;
The if Statement 4 The general form for the nested if statement is (each statement may be compound): if (condition 1 ) statement 1 else if (condition 2 ) statement 2... else if (condition n ) statement n else statement e