Professional Documents
Culture Documents
Chapter 2 - Conditions
Chapter 2 - Conditions
Chapter 2: Conditions
C++ supports the usual logical conditions from mathematics:
You can use these conditions to perform different actions for different decisions.
if statement
if (condition) {
// block of code to be executed if the condition is true
}
if-else statement
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
else-if statement
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Shorthand if…else
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Example:
int time = 20;
if (time < 18) {
cout << "Good day."; int time = 20;
} else { (time < 18) ? cout << "Good day." : cout << "Good evening.";
cout << "Good evening.";
}
switch-case statement
Example:
int day = 4;
switch (day) {
case 1:
cout << "Monday";
switch(expression) { break;
case x: case 2:
// code block cout << "Tuesday";
break; break;
case y: case 3:
// code block cout << "Wednesday";
break; break;
default: case 4:
// code block cout << "Thursday";
} break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Problem 1: Write a program that allows the user to input an integer and output the parity of
that number to the screen.
Input: “Please input an integer number:” 15
Output: “15 is an odd number”
Input: “Please input an integer number:” 8
Output: “8 is an even number”
Problem 2: This course is assessed through five exercises. The final score is calculated based on
the average of five scores. If the final score is greater than 4.0, the student will pass the course,
otherwise, the student will fail. Write a program that allows the user to input the scores of five
scores and print the final score and course result to the screen.
Input: “Please enter 5 scores:” 7.5 4 8 9.5 8.5
Output: “You got the final score of 7.5. Congas!! You passed the course”
Input: “Please enter 5 scores:” 3.5 4 2.5 3 6.5
Output: “You got the final score of 3.9. Oh no!! You failed the course”
Problem 3: Again, this course is assessed through the average of five exercises. The letter grade
will be converted based on the table below. Write a program that allows the user to input the
scores of five scores and print the final score and letter grade to the screen.
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Problem 4: Write a program that allows input from the keyboard 2 real numbers 𝑎 and 𝑏
corresponding to the coefficients of the linear function: 𝑎𝑥 + 𝑏 = 0. Solve the equation and
output the possible solutions to the screen.
Problem 5: Write a program that allows input from the keyboard 3 real numbers 𝑎, 𝑏 and 𝑐
corresponding to the coefficients of the quadratic function: 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0. Solve the
equation and output the possible solutions to the screen.
Problem 6: Write a program that allows input 2 values respectively to the month and year of a
date. Print the number of days in the month and year.
11 2011 → “This month has 30 days”
2 2008 → “This month has 29 days”
Suggestion: A leap year is a year in which the number of years is divisible by 400 or the
number of years is divisible by 4 but not by 100.
Problem 7: Write a program to input from the keyboard 3 values as day, month and year of a
date. Check if these values represent a valid date, print the result to the screen.
11 3 2018 → YES
29 2 1990 → NO
Problem 8: Write a program to input from the keyboard 3 values as day, month and year of a
date. Calculate and display the day, month and year value of the next day of that day on the
screen.
11 3 2018 → 12/03/2018
28 2 1990 → 01/03/1990
Problem 9: Write a program to input from the keyboard 3 values as day, month and year of a
date. Calculate what day of the year it is.
Problem 10: Write a program to input from keyboard 3 numbers 𝑎, 𝑏 and 𝑐. Display the
entered values in ascending order using only one extra variable.
Input: 34 12 28
Output: 12 28 34
Problem 11: Write a program to input a positive 2-digit number. Convert that number to word
and display it on the screen.
12 → twelve
97 → ninety-seven
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Problem 12: Calculate the cost of electricity for a household. Write a program that allows input
from the keyboard the old and new indicators. Calculate the amount of kWh that the
household consumes and the amount that this household must pay. Know the electricity price
is calculated according to the following table:
Level Price
First 120 kWh per month 1.63 TWD
Next 210 kWh per month 2.38 TWD
Next 170 kWh per month 3.52 TWD
Next 200 kWh per month 4.80 TWD
Next 300 kWh per month 5.66 TWD
Over 1000 kWh per month 6.99 TWD
Lecturer: Quang-Thai Ho