You are on page 1of 31

ITE 6102

Computer Programming 1
Virtual Class – September 2, 2020

Speaker: Ms. Karren V. de Lara


OLC Computing
Warm-up and Review

 Brief introduction about C++


 Example of a C++ program
 C++ Comments
 C++ Variables and Data types
 C++ Identifiers and Rules in naming identifiers
 C++ Constants
 C++ User input
 C++ Operators
C++ Conditions

C++ supports the usual logical conditions from mathematics:

Less than: a < b


Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
C++ Conditions
 You can use these conditions to perform different actions for
different decisions.
 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
Types of Control Statements/Control Structures

 if statement
 if-else statement
 if-else-if statement
 switch/case statement
The if Statement

Use the if statement to specify a block of C++ code to be


executed if a condition is true.

Syntax:

if (condition) {
// block of code to be executed if the condition is true
}
Flow Diagram of if Statement
Example:

#include <iostream>
using namespace std;

int main() {
if (20 > 18) {
cout << "20 is greater than 18";
OUTPUT:
}
return 0; 20 is greater than 18

}
Example:
#include <iostream>
using namespace std;

int main() {
int x = 20;
int y = 18;
if (x > y) {
OUTPUT:
cout << "x is greater than y";
} x is greater than y
return 0;
}
The if else Statement
Use the if else statement to specify a block of code to be executed if the
condition is false.

Syntax:

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
}
Flow Diagram of if else Statement
Example:
#include <iostream>
using namespace std;

int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
OUTPUT:
cout << "Good evening.";
} Good evening.
return 0;
}
The if-else-if Statement
if-else-if statement is used when we need to check multiple conditions. In
this control structure we have only one “if” and one “else”, however we can
have multiple “else if” blocks.
Syntax:

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
}
Example:
#include <iostream>
using namespace std;
int main() {
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) { OUTPUT:
cout << "Good day.";
Good evening.
} else {
cout << "Good evening.";
}
return 0;
}
C++ Short Hand if else
Short Hand If...Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary


operator because it consists of three operands. It can be used
to replace multiple lines of code with a single line. It is often
used to replace simple if else statements:

Syntax:

variable = (condition) ? expressionTrue : expressionFalse;


Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}
C++ switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax:

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
switch case Flow Diagram
C++ switch Statements
This is how it works:

 The switch expression is evaluated once.


 The value of the expression is compared with the values of
each case.
 If there is a match, the associated block of code is executed.
 The break and default keywords are optional.
Example:
#include <iostream>
using namespace std;
int main() {
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break; OUTPUT:
case 7:
Thursday
cout << "Sunday";
break;
}
return 0;
}
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:

Example:

#include <iostream>
using namespace std;
int main() {
int day = 4;
switch (day) {
case 6:
cout << "Today is Saturday";
break;
case 7:
cout << "Today is Sunday";
break;
default:
cout << "Looking forward to the Weekend";
}
return 0; }

OUTPUT:

Looking forward to the Weekend


Important Notes:
 Case doesn’t always need to have order 1, 2, 3 and so on. It can have
any integer value after case keyword. Also, case doesn’t need to be in
an ascending order always, you can specify them in any order based on
the requirement.

 Nesting of switch statements are allowed, which means you can have
switch statements inside another switch. However, nested switch
statements should be avoided as it makes program more complex and
less readable.

 You can also use characters in switch case.


Example:
#include <iostream>
using namespace std;

int main(){
char ch='b';
switch(ch) {
case 'd': cout<<"Case1 "; case 'y': cout<<"Case4 ";
break; break;
case 'b': cout<<"Case2 "; default: cout<<"Default ";
break; }
case 'x': cout<<"Case3 "; return 0;
break; }
Exercise No. 1:

Print "Hello World" if x is greater than y.


Answer:

int x = 50;
int y = 10;
if
(x > y)
{
cout << "Hello World";
}
Exercise No. 2:
Insert the missing parts to complete the following switch statement.

int day = 2;
switch (
){
1:
cout << "Saturday";
break;
2:
cout << "Sunday";
;
}
Answer:
int day = 2;
switch (day) {

case1:
cout << "Saturday";
break;

case 2:
cout << "Sunday";
break;
}
Thank you for listening.

If you have any questions please send a message thru LMS chat box or
email me at kvdelara@amaes.edu.ph

You might also like