Operators and Decision Control Structure
Operators and Decision Control Structure
Control Structure
By
Amjad Khan Khalil
[email protected]
Operators
Operators are symbols which have
some meaning in programming
languages and which operate on
some operands.
Operators
For example
Op1 = Op2 + Op3
Operands
Types of Operators in C++
The following are the three main
types of operators used in C++.
Arithmetic Operators
Relational Operators
Logical Operators
Arithmetic Operators
The following are the arithmetic
operators used for arithmetic
operations
+ used for addition
- used for subtraction
* used for multiplication
/ used for division
% used for remainder
Relational Operators
The following operators are known as
relational operators
> greater than
< less than
>= greater than or equal
<= less than or equal
== equal
The relational operators will find out the true or
false status of a condition and inform the
compiler accordingly.
Logical Operators
The following operators are used for
logical decision and will often
combine two or more conditions
&& logical AND
|| logical OR
!= logical NOT
&& OPERATOR
All the condition must be true to
show the result
Condition Condition 2 Condition1 && Condition 2
1
True True True
True False False
False True False
False False False
OR OPERATOR
Only one of the conditions must
be true to show the result
Condition Condition Condition1 OR Condition 2
1 2
under
if(condition)
statement;
In the above syntax “if” is a keyword.
Drawback of If-statement
The main disadvantage of if-
statement is that, when the
condition is true it will execute a
statement but if the condition is
false it will do nothing.
To remove this drawback of if-
statement then we use if-else
If-else statement
The general syntax of if-else statement
is as under
if(condition)
statement1;
else
statement2;
The above syntax tells the compiler that
execute statement1 if the condition is
true and execute statement2 in case
the condition is false
Con’t
Write down a computer program in C++ to find out
the number entered through the keyboard is Even
or Odd?
Write down a computer program in C++ to show
that a character entered through keyboard is upper
case or lower case character and convert the upper
case to lower and vice versa?
Write down a computer program in C++ to accept
the ASCII code as input for all special characters
except English alphabets and display the
corresponding character.