Lab 3 - Relational Operators
Lab 3 - Relational Operators
Expression Is true if
x==y x is equal to y
x != y x is not equal to y
x>y x is greater than y
x<y x is less than y
x >= y x is greater than or equal to y
x <= y x is less than or equal to y
The ‘NOT’ Operator: The Operator ! is the C++ operator to perform the Boolean operation NOT, it has
only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing
false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of
evaluating its operand. For example:
The logical operators && and | | are used when evaluating two expressions to obtain a single relational
result.
The ‘AND’ Operator: The operator && corresponds with Boolean logical operation AND. This operation
results true if both its two operands are true, and false otherwise. The following panel shows the result of
operator && evaluating the expression a && b:
&& OPERATOR
A B A && B
True True True
True False False
False True False
False False False
The ‘OR’ Operator: The operator | | corresponds with Boolean logical operation OR. This operation results
true if either one of its two operands is true, thus being false only when both operands are false themselves.
Here are the possible results of a | | b:
| | OPERATOR
A B A||B
True True True
True False True
False True True
False False False
For example:
When using the logical operators, C only evaluates what is necessary from left to right to come up with the
combined relational result, ignoring the rest. Therefore, in this last example ((5 = = 5) | | (3 > 6)), C would
evaluate first whether 5 = = 5 is true, and if so, it would never check whether 3 > 6 is true or not. This is
known as short-circuit evaluation, and works like this for these operators:
operator short-circuit
&& if the left-hand side expression is false, the combined result is false (right-hand side
expression not evaluated).
|| if the left-hand side expression is true, the combined result is true (right-hand side expression
not evaluated).
Forms of if:
If Statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition
returns false, then the statements inside “if” are skipped.
Syntax:
if (condition) {
//body of if
}
Code:
Output:
Code:
Output:
If Else Statement:
If condition returns true then the statements inside the body of “if” are executed and the statements
inside body of “else” statements are skipped. If condition returns false then the statements inside
the body of “if” are skipped and the statements in “else” are executed.
Syntax:
If (condition) {
// statement inside body of if
}
Else {
// statement inside body of else
}
Example 3:
Write a program which will take age of a person from user and decide whether a person is
eligible to vote or not. The person must be greater than or equal to 18 years to be eligible to
vote.
Code:
Output:
Note: If there is only one statement is present in the “if” or “else” body then you do not need to
use the braces. For example, the above program can be rewritten like this:
Code:
Output:
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
Example 4:
Write a program to take two numbers as input and check whether a first number is smaller,
greater or equal. Use Nested if-else statement.
Code:
Output:
Else-if Statement:
The else if statement is useful when you need to check multiple conditions within the program,
nesting of if-else blocks can be avoided using else if statement.
Syntax:
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return
false.
}
Example 5:
Output:
Important Points:
1. else and else..if are optional statements, a program having only “if” statement would run fine.
2. else and else..if cannot be used without the “if”.
3. There can be any number of else..if statement in a if else..if block.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND (&&), OR(||) and
NOT(!).
TASKS:
1. Write a program to determine whether a number is even or odd using if statement and modulus
operator.
• Take value from user and store in an already declared variable.
• Use if statement and modulus operator to check for divisibility by 2
• Display appropriate message i.e “Number is Even” or “Number is Odd”
2. Take from user time in 24-hour format and convert to 12 hour and display.
• Declare and initialize 3 variables for hour, minutes and seconds in 24 hour format as Thour,
Tmin, Tsec
• Declare and initialize three variables for hours minutes and seconds in 12 hour format as
thour, tmin, tsec.
• Get user values for Thour, Tmin, Tsec.
• If Thour greater than 12, subtract 12 from Thours and store in thour. Copy tmin and tsec to
Tmin and Tsec respectively. Display the 12 hour values.
• If Thour is lesser than or equal to 12, copy (24 hour) values to 12 hour variables and display.
3. Given as input an integer number of seconds, print as output the equivalent time in hours, minutes
and seconds. Recommended output format is something like
7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.
• Declare and initialize a variable and take input from user.
• Find corresponding values for hours, minutes using formulas:
o 1 min = 60 sec
o 1 hour = 60 x 60 sec
• Use successive division and modulus operations.
4. Write a program to check whether a triangle is valid or not, when the three angles of the triangle
are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal
to 180 degrees.