Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Computer Programming 1 Week 3 Prof.

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.

C++ has the following conditional statements

 Use if to specify a block of code to be executed, if a specified condition is true


 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of C++ code to be executed if a condition is true.

The if statement evaluates the condition inside the parentheses ( ).


If the condition evaluates to true, the code inside the body of if is executed.
If the condition evaluates to false, the code inside the body of if is skipped.

Example 1 Program Code: Program Code:


#include <iostream> #include <iostream>
if (20 > 18) {
using namespace std; using namespace std;
  cout << "20 is greater than 18";
int main() { int main() {
} int x = 20;
if (20 > 18) {
_________________________ int y = 18;
cout << "20 is greater than 18";
if (x > y) {
int x = 20; } cout << "x is greater than y";
int y = 18; return 0; }
if (x > y) { } return 0;
  cout << "x is greater than y"; }
}

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

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

If condition1 evaluates to true, the code block 1 is executed.


If condition1 evaluates to false, then condition2 is evaluated.
If condition2 is true, the code block 2 is executed.
If condition2 is false, the code block 3 is executed.
Computer Programming 1 Week 3 Prof. Marvin Ramos
Example
Page | 3 Program Code:
int time = 22; #include <iostream>
if (time < 10) { using namespace std;
  cout << "Good morning."; int main() {
} else if (time < 20) { int time = 22;
  cout << "Good day."; if (time < 10) {
} else { cout << "Good morning.";
  cout << "Good evening."; } else if (time < 20) {
} cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
Test Yourself

Write a program to check whether an integer is positive, negative or zero.

C++ Switch Statements

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";
}

The break Keyword

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

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

You might also like