Selection
Selection allows us to include choices in our program. Is it going to rain? Let's grab an umbrella!
Syntax
Relational and Logical Expressions
for relational and logical operator precedence and associativity, please see Operator Precedence and Associativity
Most choices depend on comparing conditions or whether or not certain conditions are true. These relationships and conditions can be evaluated through relational and logical expressions.
For example, if the temperature outside is below a certain threshold we would want to grab a jacket. The relationship between too cold and the current temperature could be determined with the following expression:
currentTemp < tooCold
Relational and logical expressions always evaluate to true or false (although the C programming language uses 1
and 0
instead) and because they have lower precedence than arithmetic operators, the expressions can get quite complex:
currentTemp - 10 < tooCold
Logical operators have even lower precedence than relational operators!
currentTemp < tooCold && currentTemp > 32
also checks that the current temperature is above freezing.
if
and else
These expressions can be used in if/else
statements to determine which blocks (parts) of code to execute, resulting in our programs behaving in different ways! These start with the if
keyword, are followed by the expression to evaluate in the parenthesis, and should include curly braces surrounding the block of code you want executed:
if(currentTemp < tooCold){
printf("Don't forget a jacket!\n");
}
When there's an alternative path to the true condition which must be taken, use the
else
keyword and curly braces for that block of code. Note! There is no expression to evaluate after the else
!if(currentTemp < tooCold){
printf("Don't forget a jacket!\n");
}
else{
printf("So fresh and so clean!\n");
}
When there's multiple alternative pathes, use a "cascading"
if/else
format:if(currentTemp < 32){
printf("Oof, too cold! Stay home.\n");
}
else if(currentTemp < tooCold){
printf("Don't forget a jacket!\n");
}
else{
printf("So fresh and so clean!\n");
}
There are a ton of different ways to use them, depending on the problem and how you approach it. if
statements can even be used on their own. As long as the expression between the parenthesis evaluates to 1
(true), the code between the parenthesis executes:
if(raining == 'y' || raining == 'Y'){
umbrella = 1;
}
If there's no action to take when it's not raining, then we don't need an else
block.switch
switch
statements provide a convenient format when decisions are made based on specific integer or character values. The block starts with the switch
keyword and the variable whose value is going to be checked. Then there is a list of "cases" for each useful value. Here's a generic form:
switch(expression){ //the expression must be an int or char variable
case x:
//code to execute
break;
case y:
//different code to execute
break;
default:
//code to execute if no other cases are true
}
And a more specific example:
switch(currentTemp){
case 32:
printf("It's freezing!\n");
break;
case 451:
printf("The temperature at which books burn.\n");
break;
case 212:
printf("I'm boiling!\n");
break;
default:
printf("Nothing interesting about this temperature.\n");
}
Since there aren't any curly braces after the
case
value, the break
statement is what keeps the code from continuing to execute. Sometimes it's useful to take advantage of that feature and omit the break
statements!switch(studentGrade){
case 'A':
case 'B':
case 'C':
printf("Have fun in 202!\n");
break;
case 'D':
case 'F':
printf("See you next semester!\n");
break;
default:
printf("Invalid grade.\n");
}
If the
studentGrade
variable stores either A
, B
, or C
then Have fun in 202!
will display to the screen. While this allows for using a switch
for a range, I'd only recommend it if there are very few possible matches.Behavior
Relational and Logical Expressions
Relational operators should look familiar, as they mirror their mathematical counterparts, however logical operators are often a new concept.
- AND
&&
expressions evaluate to true or1
when both conditions are true. - OR
||
expressions evaluate to false or0
when both conditions are false.
The AND truth table:
first part | logic | second part | whole statement |
---|---|---|---|
We're in CS 135 | AND | Erin is teaching. | 1 (true) |
We're in CS 135 | AND | Sara is teaching. | 0 (false) |
We're in CS 202 | AND | Erin is teaching. | 0 (false) |
We're in CS 202 | AND | Sara is teaching. | 0 (false) |
The OR truth table:
first part | logic | second part | whole statement |
---|---|---|---|
We're in CS 135 | OR | Erin is teaching. | 1 (true) |
We're in CS 135 | OR | Sara is teaching. | 1 (true) |
We're in CS 202 | OR | Erin is teaching. | 1 (true) |
We're in CS 202 | OR | Sara is teaching. | 0 (false) |
Generic Truth Tables
p is the first statment or clause and q is the second statment or clause
AND
p | q | p && q |
---|---|---|
1 (true) | 1 (true) | 1 (true) |
1 (true) | 0 (false) | 0 (false) |
0 (false) | 1 (true) | 0 (false) |
0 (false) | 0 (false) | 0 (false) |
OR
p | q | p || q |
---|---|---|
1 (true) | 1 (true) | 1 (true) |
1 (true) | 0 (false) | 1 (true) |
0 (false) | 1 (true) | 1 (true) |
0 (false) | 0 (false) | 0 (false) |