0% found this document useful (0 votes)
24 views4 pages

CMPE 011 - Module 3-Flow Control - Conditions and If Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

CMPE 011 - Module 3-Flow Control - Conditions and If Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TOPIC Operators used to assign values to variables.

In the
example below, we use the assignment operator (=) to
SUB-TOPIC
assign the value 10 to a variable called x:
SUB-SUB-TOPIC
int x = 10;
I. Operators
Operators are used to perform operations on The addition assignment operator (+=) adds a
variables and values. In the example below, we use value to a variable:
the + operator to add together two values:
int x = 10;
x += 5;
int x = 100 + 50;
Although the + operator is often used to add
together two values, like in the example above, it can A list of all assignment operators:
also be used to add together a variable and a value, or a
variable and another variable: OPERATOR Example Same As

= x=5 x=5
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250) += x += 3 x=x+3
int sum3 = sum2 + sum2; // 800 (400 + 400)
-= x -= 3 x=x-3

C++ divides the operators into the following groups: *= x *= 3 x=x*3


● Arithmetic operators, Assignment operators,
/= x /= 3 x=x/3
Comparison operators, Logical operators,
Bitwise operators %= x %= 3 x=x%3

&= x &= 3 x=x&3


Arithmetic Operators
Operators used to perform common |= x |= 3 x=x|3
mathematical operations. ^= x ^= 3 x=x^3
OPERATOR Name Description Example
>>= x >>= 3 x = x >> 3
+ Addition Adds together two x+y
values <<= x <<= 3 x = x << 3

- Subtraction Subtracts one value x-y


from another Assignment Operators
* Multiplication Multiplies two x*y Comparison operators are used to compare two
values
values. Note: The return value of a comparison is either
/ Division Divides one value by x/y true (1) or false (0). In the following example, we use the
another greater than operator (>) to find out if 5 is greater than 3:
% Modulus Returns the division x%y int x = 5;
remainder
int y = 3;
++ Increment Increases the value x ++ y cout << (x > y); // returns 1 (true) because 5 is greater
of a variable by 1 than 3
-- Decrement Decreases the value --x
of a variable by 1
A list of all comparison operators:

OPERATOR Name Example


Assignment Operators

1
== Equal to x == y else if to specify a new condition to test, if the
first condition is false
!= Not equal x != y

> Greater than x>y switch to specify many alternative blocks of


code to be executed
< Less than x<y

>= Greater than or equal x >= y The if Statement


to
Use the if statement to specify a block of C++ code to be
<= Less than or equal to x <= y executed if a condition is true.

Examples:
Comparison Operators Test two values to find out if 20 is greater than 18. If the
Logical operators are used to determine the condition is true, print some text:
logic between variables or values:
if (20 > 18) {
cout << "20 is greater than 18";
OPERATOR Name Description Example }

&& Logical Returns true x < 5 && x < 10


and if both We can also test variables:
statements
are true int x = 20;
int y = 18;
|| Logical Returns true x < 5 || x < 10 if (x > y) {
or if one of the
statements cout << "x is greater than y";
is true }
! Logical Reverse the !(x < 5 && x < 10)
not result,
returns false The else Statement
if the result Use the else statement to specify a block of code to be
is true
executed if the condition is false.

II. C++ Conditions and If Statements


if (condition) {
C++ supports the usual logical conditions from // block of code to be executed if the
mathematics: condition is true
● Less than: a < b } else {
● Less than or equal to: a <= b // block of code to be executed if the
condition is false
● Greater than: a > b
}
● Greater than or equal to: a >= b
● Equal to a == b
● Not Equal to: a != b
You can use these conditions to perform
different actions for different decisions.
int time = 20;
C++ has the following conditional statements: if (time < 18) {
cout << "Good day.";
} else {
if to specify a block of code to be cout << "Good evening.";
executed, if a specified condition is true }
// Outputs "Good evening."
else to specify a block of code to be
executed, if the same condition is false

2
In the example above, time (20) is greater than 18, so the
expressionFalse;
condition is false. Because of this, we move on to the else
condition and print to the screen "Good evening". If the time
was less than 18, the program would print "Good day". Instead of writing:
int time = 20;
The else if Statement if (time < 18) {
Use the else if statement to specify a new condition if the cout << "Good day.";
first condition is false. } else {
cout << "Good evening.";
}
if (condition1) {
// block of code to be executed if
condition1 is true You can simply write:
} else if (condition2) {
// block of code to be executed if the int time = 20;
condition1 is false and condition2 is true string result = (time < 18) ? "Good day."
} else { : "Good evening.";
// block of code to be executed if the cout << result;
condition1 is false and condition2 is
false
}

int time = 22;


if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."

Example explained
● In the example above, time (22) is greater than 10,
so the first condition is false. The next condition, in
the else if statement, is also false, so we move on to
the else condition since condition1 and condition2
is both false - and print to the screen "Good
evening".
● However, if the time was 14, our program would
print "Good day."

The 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:

variable = (condition) ? expressionTrue :

3
SOURCES:
● https://www.w3schools.com/cpp/cpp_operator
s.asp

You might also like