Professional Documents
Culture Documents
Handout C++ Week3
Handout C++ Week3
Marvin Ramos
Learning
Page | 1 Objectives
• Use conditional statement (if-statement), multibranch (ladder) if statement and nested if statement to solve
common problems.
• Write switch statement and use it to write programs.
• Understand how to evaluate boolean expressions (conditions) and compound boolean expressions.
The if Statement
Use the if statement to specify a block of C++ code to be executed if a condition is true.
Test Yourself
Write the program to print positive number entered by the user If the user enters a negative number, it is skipped.
Program Code:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
Computer Programming 1 Week 3 Prof. Marvin Ramos
Page | 2 If the condition evaluates true,
the code inside the body of if is executed
the code inside the body of else is skipped from execution
If the condition evaluates false,
the code inside the body of else is executed
the code inside the body of if is skipped from execution
Example
Program Code:
int time = 20; #include <iostream>
if (time < 18) { using namespace std;
cout << "Good day."; int main() {
} else { int time = 20;
cout << "Good evening."; if (time < 18) {
} cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
Test Yourself
Write a program to check whether an integer is positive or negative. This program considers 0 as a positive number
Use the else if statement to specify a new condition if the first condition is false.
Use the switch statement to select one of many code blocks to be executed.
Computer Programming 1 Week 3 Prof. Marvin Ramos
Example
Page | 4
int day = 4; Program Code: break;
switch (day) { #include <iostream> case 7:
case 1: using namespace std; cout << "Sunday";
cout << "Monday"; int main() { break;
break; int day = 4; }
case 2: switch (day) { return 0;
cout << "Tuesday"; case 1: }
break; cout << "Monday";
case 3: break;
cout << "Wednesday"; case 2:
break; cout << "Tuesday";
case 4: break;
cout << "Thursday"; case 3:
break; cout << "Wednesday";
case 5: break;
cout << "Friday"; case 4:
break; cout << "Thursday";
case 6: break;
cout << "Saturday"; case 5:
break; cout << "Friday";
case 7: break;
cout << "Sunday"; case 6:
break; cout << "Saturday";
}
When C++ reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
The default keyword specifies some code to run if there is no case match.
Program Code:
int day = 4;
#include <iostream>
switch (day) {
using namespace std;
case 6:
int main() {
cout << "Today is Saturday";
int day = 4;
break;
switch (day) {
case 7:
case 6:
cout << "Today is Sunday";
cout << "Today is
break;
Saturday";
default:
break;
cout << "Looking forward to the Weekend";
case 7:
}
cout << "Today is
Sunday";
break;
default:
cout << "Looking forward
to the Weekend";
Computer Programming 1 Week 3 Prof. Marvin Ramos
Page | 5
Example 2 – Simple Calculator using Switch Statement
Computer Programming 1 Week 3 Prof. Marvin Ramos
Page | 6