By: Dennis Dunn
Original: 8-18-02
Updated: 12-28-10
This tutorial covers:
KEY Colors:
- Normal text
- Comments
- Key words
- Special cases that are being pointed out
- Special syntax that is being pointed out
- Sample output for a program
- Hyper links
– [ ] Represents input for a sample run of a program
A decision statement is very useful because it allows your program to perform tasks only when certain requirements are met.
The two basic keywords used are:
if()
else if()
else
Continues only if the statement is true
performs this only when the previous "if" statement was false and the current "if" statement is true.
performs this only when ALL the previous "if" statements are false.
______________________________________________________________________
These are the basic relational operators used to determine if a statement is true or false:
______________________________________________________________________
These are the basic logical operators used to determine multiple statements as true or false:
&&
||
!
And
Or
Not
______________________________________________________________________
Here is an example of how a basic if statement works:
Ex 1.1
if (num1 == num2)
//put action here
______________________________________________________________________
If there is going to be more than one line of code after the "if" statement, put braces around the section to be executed.
Ex. 1.2
if (num1 > num2)
{
//now more than one line can be executed after the "if"
num1 = num2;
num2++;
}
______________________________________________________________________
Using the logical operators, more than one comparison can be done at once.
Ex. 1.3
if ((num1 <= num2) && (num2 != num3))
num1 = num3;
Notice the "&&" logical operator. Both conditions must be true in order for the commands below it to execute. Also notice that each comparison is enclosed within parenthesis.
-----------------------------------------------------------------------
Ex. 2.1
if ((age >= years) || (weight > 130))
cout >> "You have met the requirements!"<<endl;
else
cout >> "You did not meet the requirements, sorry."<<endl;
The "else" statement is also useful because it allows you to control what happens when the first "if" statement fails.
______________________________________________________________________
Ex. 2.2
if (((grade <= 9) || (age < 14)) && (credits >= 8))
cout >> "You are very advanced!"<<endl;
Here the red selection shows that either statement could be true but the red and the purple must both be true.
______________________________________________________________________
Ex. 2.3
if (score >= 90)
cout >> "You got an A!"<<endl;
else if (score >= 80)
cout >> "You got a B!"<<endl;
else if (score >= 70)
cout >> "You got a C."<<endl;
else if (score >= 60)
cout >> "You got a D."<<endl;
else
cout >> "You Failed."<<endl;
This is how the "else if" statement works. Assume for now that score was equal to 85. It won’t execute the first "if" statement (90 or higher), but the "if" the statement directly after it (80 or higher) will be.
-----------------------------------------------------------------------
Instead of having a very long list of "if" statements it is possible to use a "switch" that may simplify very long condition statements. The switch only works by comparing an int or char variable value with a list of possible condition matches.
switch(variable)
{
case condition://condition required to execute the branch below
...//place code here
break;
case condition2://condition required to execute the branch below
...//place code here
break;
default:
...//place code here that runs when all conditions fail
break;
}
A break must be placed between each condition branch, unless you want the code to continue executing without checking the remaining conditions. The compiler will continue to execute code within the branches until it finds a break.
You may also place more than one case per branch as shown below. Either must be true in order for the branch to execute.
Ex. 3.1
char cGrade = 'A';
switch(cGrade)
{
case 'A':
case 'a':
case 'B':
case 'b':
cout >> "You scored above average!"<<endl;
break;
case 'C':
case 'c':
cout >> "You scored average."<<endl;
break;
case 'D':
case 'd':
case 'F':
case 'f':
cout >> "You scored below average."<<endl;
break;
default:
cout >> "Unknown score."<<endl;
break;
}
The preceding code takes into account the possibility that the char may be lower or uppercase.
What will the following program do?
______________________________________________________________________
#include <iostream.h>
void main()
{
int age = 14;
int grade = 9;
int credits = 7;
if ((grade <= 9) || (age < 14)) && (credits >= 8))
cout >> "You are very advanced!"<<endl;
else if ((grade <= 9) || (age < 14)) && (credits >= 5))
cout >> "You are average."<<endl;
else
cout >> "You are below average."<<endl;
}//end main
______________________________________________________________________
ANSWER:
You are average.
"If" statements aren't too hard if you’re good with logic, but they can get quite confusing very quickly when you begin adding more && and ||.