Selection Statements (if/else)
Control Structures - C++ has three types of control structures
Selection Structures
if (condition)
statement;
if (condition)
statement1;
else
statement2;
if (condition 1 is true)
if (condition 2 is true)
statement a;
else
statement b;
else
statement c;
Decision Tree
if (condition is true)
{
statement a1;
statement a2;
statement a3;
}
//Program to determine age status
#include <iostream.h>
int main()
{
int age;
//Prompt user for age
cout << endl << "Enter age in
years: ";
cin >> age;
if (age > 40)
cout << endl <<
"A " << age << "year old person is old";
else
cout << endl <<
"A " << age << "year old person is young";
return 0;
}
Lab 4 Core 1 & Core 2 background & warmup (show
debugger also)
show if( i > 10); does nothing
Logical Operators
Nested ifs are useful for double or overlapping conditions
e.g.
if (age >= 0)
if (age > 40)
cout << endl <<
"A " << age << "year old is old";
else
cout << endl <<
"A " << age << "year old is young";
Another way to do this is to use logical operators:
! NOT (changes true to false, false to true)
&& AND (both conditions must be true)
|| OR (at least one of the condtions
must be true)
e.g.
if (age >= 0) && (age > 40)
cout << endl << "A " <<
age << " year old person is old";
else
if (age >= 0) && (age <= 40)
cout << endl <<
"A " << age << " year old person is young";
Lab 4 Core 3 background & warmup
Boolean expressions - Operators needed for
Lab 4 and their precedence (highest to lowest)
( )
! (NOT)
*, /, %
+, -
<, <=, >, >= (relational operators)
==, != (equality operators)
&& (AND)
||(OR)
= (assignment)
Complete ACTIVITY program using the following specification:
Write a program that displays a suggested activity based on the outdoor
temperature (in fahrenheit) and whether or not it is raining/snowing. Use
the following table for suggested activities. Show both with nested loops
and with logical operators.
Temperature |
Raining/Snowing |
Activity |
>=80 |
no |
beach volleyball |
>=80 |
yes |
movie |
<80 and >=32 |
no |
running |
<80 and >=32 |
yes |
racquetball |
<32 |
no |
ice fishing |
<32 |
yes |
skiing |
"switch" structure ("switch" or "case" statement) - is a multiple selection structure (selects among many different actions); it can only be used for evaluation a constant integral condition (any combination of character constants (single character in quotes) and integer constants (integer value) that evaluates to a constant integer value); the result of the switch statement's expression is compared with each of the case labels; if the comparison with label1 is true, the statement following this case is executed; if the comparison is false, the next case is evaluated and so on; if no match occurs, the default statement (optional) is executed; it is recommended that you end each case structure with a break statement which ensures that only one statement sequence is executed (otherwise "fall-through" occurs with a match and the remaining statements after the match are executed
switch (expression)
{
case label1:
statement1;
break;
case label2:
statement2;
break;
case label3:
statement3;
break;
default:
defaultStatement;
}
// Sample program using switch statement.
#include <iostream.h>
int main ()
{
char letterGrade;
cout << endl << "Enter a letter
grade: ";
cin >> letterGrade;
// Execute the selected test.
switch ( letterGrade )
{
case 'A' : cout <<
"1st case" << endl; break;
case 'B' : cout <<
"2nd case" << endl; break;
case 'C' : cout <<
"3rd case" << endl; break;
default : cout <<
"Invalid input" << endl;
}
return 0;
}
Lab 4 Core 4 background & warmup, discuss pseudo-code for application exercise
Copyright Matthew Bauer, Computer Science, Illinois Institute of Technology, Fall 2000