0% found this document useful (0 votes)
7 views

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

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

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

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TOPIC Assignment Operators

SUB-TOPIC Operators used to assign values to variables. In the


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

OPERATOR Example Same As


int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250) = x=5 x=5
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 <<= x <<= 3 x = x << 3


from another

* Multiplication Multiplies two


values
x*y Assignment Operators
Comparison operators are used to compare two
/ Division Divides one value by x/y values. Note: The return value of a comparison is either
another
true (1) or false (0). In the following example, we use the
% Modulus Returns the division x%y greater than operator (>) to find out if 5 is greater than 3:
remainder

++ Increment Increases the value x ++ y int x = 5;


of a variable by 1 int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater
-- Decrement Decreases the value --x than 3
of a variable by 1

1
A list of all comparison operators:
if to specify a block of code to be
executed, if a specified condition is true
OPERATOR Name Example
else to specify a block of code to be
== Equal to x == y
executed, if the same condition is false
!= Not equal x != y
else if to specify a new condition to test, if the
> Greater than x>y first condition is false
< Less than x<y to specify many alternative blocks of
switch
>= Greater than or equal x >= y code to be executed
to

<= Less than or equal to x <= y The if Statement


Use the if statement to specify a block of C++ code to be
executed if a condition is true.
Comparison Operators
Logical operators are used to determine the Examples:
logic between variables or values: Test two values to find out if 20 is greater than 18. If the
condition is true, print some text:
OPERATOR Name Description Example if (20 > 18) {
cout << "20 is greater than 18";
&& Logical Returns true x < 5 && x < 10 }
and if both
statements
are true
We can also test variables:
|| Logical Returns true x < 5 || x < 10
or if one of the int x = 20;
statements int y = 18;
is true
if (x > y) {
! Logical Reverse the !(x < 5 && x < 10) cout << "x is greater than y";
not result, }
returns false
if the result
is true
The else Statement
Use the else statement to specify a block of code to be
II. C++ Conditions and If Statements executed if the condition is false.
C++ supports the usual logical conditions from
mathematics:
if (condition) {
● Less than: a < b // block of code to be executed if the
● Less than or equal to: a <= b condition is true
● Greater than: a > b } else {
● Greater than or equal to: a >= b // block of code to be executed if the
condition is false
● Equal to a == b }
● Not Equal to: a != b
You can use these conditions to perform
different actions for different decisions.
C++ has the following conditional statements:

2
int time = 20; The Short Hand If…Else (Ternary Operator)
if (time < 18) { There is also a short-hand if else, which is known
cout << "Good day."; as the ternary operator because it consists of three
} else {
cout << "Good evening."; operands. It can be used to replace multiple lines of code
} with a single line. It is often used to replace simple if else
// Outputs "Good evening." statements:

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

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

You might also like