Conditional statements in C++
C++ has if
-else
and switch
to determine whether statements would execute based on a given condition.
if
statement
Use if
to specify a block of code to be executed, if a specified condition is true
. The syntax is as follows.
if (condition) {
// statements
}
For example,
test-if.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "a is ";
cin >> a;
cout << "b is ";
cin >> b;
if (a > b)
{
cout << "a is greater than b" << endl;
}
return 0;
}
|
else
statement
Use else
to specify a block of code to be executed, if the same condition is false
. The syntax is as follows.
if (condition)
{
// statements
}
else
{
// other statements
}
For example,
test-if-else.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "a is ";
cin >> a;
cout << "b is ";
cin >> b;
if (a > b)
{
cout << "a is greater than b" << endl;
}
else
{
cout << "a is no greater than b" << endl;
}
return 0;
}
|
else if
statement
Use the else if
statement to specify a new condition if the first condition is false
. The syntax is as follows.
if (condition1)
{
// statements for condition1
}
else if (condition2)
{
// statements for condition2
}
else
{
// other statements
}
For example,
test-if-elif-else.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "a is ";
cin >> a;
cout << "b is ";
cin >> b;
if (a > b)
{
cout << "a is greater than b" << endl;
}
else if (a == b)
{
cout << "a equals b" << endl;
}
else
{
cout << "a is less than b" << endl;
}
return 0;
}
|
Takeaway
Only if
is required, else
and else if
are both optional.
Ternary Operator
variable = (condition) ? valueIfTrue: valueIfFalse
This is equivalent to
if (condition)
{
variable = valueIfTrue;
}
else
{
variable = valueIfFalse;
}
For example,
show-num-records.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
int num;
cout << "num is ";
cin >> num;
string be = (num > 1) ? "are " : "is ";
char s = (num > 1) ? 's' : '\0';
cout << "There " << be << num << " record" << s << "." << endl;
return 0;
}
|
Assignment 4
Create a C++ program named bmi-calculator.cpp
that prompts for weight in pounds and height in inches. It does not only calculates and displays the body mass index (BMI), but will also prompt the BMI classification.
FYI, BMI = (Weight in Pounds / (Height in inches x Height in inches)) x 703
The BMI classification is as follows.
BMI | BMI Classification |
18.5 or less | Underweight |
18.5 to 24.99 | Normal Weight |
25 to 29.99 | Overweight |
30 to 34.99 | Obesity (Class 1) |
35 to 39.99 | Obesity (Class 2) |
40 or greater | Morbid Obesity |
Sample Solution
bmi-calculator.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
float weight, height;
cout << "weight (in pound) is ";
cin >> weight;
cout << "height (in inch) is ";
cin >> height;
float bmi = weight / (height * height) * 703;
string classification;
if (bmi <= 18.5)
{
classification = "Underweight";
}
else if (bmi <= 25)
{
classification = "Normal Weight";
}
else if (bmi <= 30)
{
classification = "Overweight";
}
else if (bmi <= 35)
{
classification = "Obesity (Class 1)";
}
else if (bmi <= 40)
{
classification = "Obesity (Class 2)";
}
else
{
classification = "Morbid Obesity";
}
cout << "BMI is " << bmi << endl;
cout << "BMI classification is " << classification << endl;
return 0;
}
|
switch
statement
In C++ we can use the switch
statement to select one of many code blocks to be executed. The syntax is as follows.
switch(expression) {
case value1:
// code block 1
break;
case value2:
// code block 2
break;
case value3:
// code block 3
default:
// code block default
}
- The
expression
is evaluated once. - The value of
expression
is then compared with values in each of case
. When there's a match, the according code bock will execute. - The
break
allows us to break out of switch
block and to stop further case
testing. Usually, if there's a match, we can break
afterwards. But there are exceptions. - The
default
specifies what to do if there's no match.
Here's a regular use case of switch
.
month-num-to-name.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
int month;
cout << "month is ";
cin >> month;
switch (month)
{
case 1:
cout << "Jan" << endl;
break;
case 2:
cout << "Feb" << endl;
break;
case 3:
cout << "Mar" << endl;
break;
case 4:
cout << "Apr" << endl;
break;
case 5:
cout << "May" << endl;
break;
case 6:
cout << "Jun" << endl;
break;
case 7:
cout << "Jul" << endl;
break;
case 8:
cout << "Aug" << endl;
break;
case 9:
cout << "Sept" << endl;
break;
case 10:
cout << "Oct" << endl;
break;
case 11:
cout << "Nov" << endl;
break;
case 12:
cout << "Dec" << endl;
break;
default:
cout << "Invalid" << endl;
break;
}
return 0;
}
|
Here's a special usage of not haveing to break
month-num-to-season.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
int month;
cout << "month is ";
cin >> month;
switch (month)
{
case 12:
case 1:
case 2:
cout << "Winter" << endl;
break;
case 3:
case 4:
case 5:
cout << "Spring" << endl;
break;
case 6:
case 7:
case 8:
cout << "Summer" << endl;
break;
case 9:
case 10:
case 11:
cout << "Fall" << endl;
break;
default:
cout << "Invalid" << endl;
break;
}
return 0;
}
|
Assignment 5
Create a C++ program named grades.cpp
that prompts for a grade in percentage and translates it to a letter grade.
By the way the letter grade assignment is as follow.
Percentage | Letter Grade |
90-100 | A |
80-89 | B |
70-79 | C |
60-69 | D |
< 60 | F |
Sample Solution
grades.cpp |
---|
| #include <iostream>
using namespace std;
int main()
{
float grade;
cout << "percentage grade is ";
cin >> grade;
if ((grade > 100) || (grade < 0))
{
cout << "Invalid" << endl;
}
else
{
int level = grade / 10;
switch (level)
{
case 10:
case 9:
cout << "A" << endl;
break;
case 8:
cout << "B" << endl;
break;
case 7:
cout << "C" << endl;
break;
case 6:
cout << "D" << endl;
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
cout << "F" << endl;
break;
}
}
return 0;
}
|