You are on page 1of 31

Relational Operators

And
Decisions

Computer Science Department


Relational Operators
• A Relational Operator compares two
values.
• Values can be any built-in C data type.
• The comparison involves such relationship
as equal to, less than and greater than.
• The result of the comparison is either true
or false.

Computer Science Department


Relational Operator
Operator Meaning
> Greater than
< Less than
== Equal To
!= Not Equal To
>= Greater Than or equal to
<= Less Than or equal to

Computer Science Department


Decisions
• In a program a decision causes a one-time jump
to a different part of the program, depending on
the values of an expression.

• Decision can be made in C++ in several ways.


– IF Statement.
– IF and ELSE Statement.
– Switch Statement.

Computer Science Department


IF Statement
• C++ uses keyword if to implement the decision control
instruction.
if ( this condition is true )
execute this statement;
• The if keyword if followed by a test expression in
parenthesis.
• If the condition is true than compiler will execute the
immediate statement. But if the condition is false than
the immediate statement will not execute, and the
compiler will skip the immediate statement and execute
the next statement.

Computer Science Department


IF Statement Example
void main ( )
{
int x;
cout<<“Enter a number”;
cin>> x;
if ( x > 100 )
cout<<“the number is greater than 100”;
getche( );
}
Computer Science Department
Multiple Statements in the IF
body
if ( condition )
{
statement 1;
statement 2
.
.
statement n;
}

Computer Science Department


If … else statement
• Sometimes we want to do one thing if the condition is
true and other if the condition is false. That’s where the
if.. else statement is used.
if ( condition )
{
statement 1;
statement 2;
}
else
{
statement 1;
statement 2;
}

Computer Science Department


If..else Example
void main ( )
{
float b_salary, m_allowance, h_allowance;
cout<<“Enter Basic Salary”;
cin>>b_salary ;
if ( b_salary >= 50000 )
{
m_allowance = 40.0 / 100 * b_salary;
h_allowance = 30.0 / 100 * b_salary;
}
else
{
m_allowance = 30.0 / 100 * b_salary;
h_allowance = 20.0 / 100 * b_salary;
}
}
Computer Science Department
Nested if..else Statements
• If we write the entire if..else statement within the body of if
statement or else statement. This is called nesting of ifs.
if ( condition )
{
if ( condition )
statement 1;
else
statement 2;
}
else
{
statement 3;
}
Computer Science Department
The else..if Construction
if (condition1)
statement 1;
else if (condition2 )
statement 2;
else if (condition3)
statement 3;
else if (condition4)
statement 4;
else
statement 5;

Computer Science Department


Else..if example
void main ( )
{
int month;
cout<<“ Enter Month in Number= ”
cin>>month;
if ( month == 1 )
cout<<“ January”;
else if ( month == 2 )
cout << “ February ”;
else if ( month == 3 )
cout<< “ March ”;
.
.
else
cout << “ December ”;
}

Computer Science Department


LOGICAL OPERATORS

Computer Science Department


Logical Operators
• These operator allows you to logically combines
variables or expressions.

• There are three logical operators


– && (for AND Operation)
– || ( for OR Operation)
– ! ( for NOT Operation)

Computer Science Department


Logical AND Operator
• The AND operator ( && ) requires both relationships to
be true in order for the overall result to be true. If either
or both of the relationships are false, the entire result is
false.
• Example
cout<<“Enter Marks”;
cin>> marks;
if ( ( marks > 100 ) && ( marks < 0 ) )
cout<< “ Marks are invalid”;

Computer Science Department


Logical OR Operator
• The OR Operator ( || ) takes two logical expressions and
combines them. If either or both are true, the result is
true. Both values must be false for the result to be false.
• Example
char t_grade, p_grade;
cout << “Enter the Theory Grade: P for pass, F for Fail”;
cin>> t_grade;
cout<< “Enter the Practical Grade: P for pass, F for Fail”;
cin>> p_grade;
if ( ( t_grade == ‘F’ ) || ( p_grade == ‘F’ ) )
cout<<“Overall Fail”;
else
cout<<“Overall Pass”;

Computer Science Department


Logical NOT Operator
• The ( && ) and ( II ) operators always appears between
two expressions. Also known as binary operators.
• The NOT operator ( ! ) is a unary operator.
• It precedes a single logical expression and gives its
opposite as a result.
• Example
if !( marks > 50)
is same as
if (marks <= 50)

Computer Science Department


SWITCH STATEMENT

Computer Science Department


Switch Statement
• If you have a large decision tree, and all the decisions depend on
the value of the same variable, switch statement is suitable instead
of if..else or else..if statement.
switch ( integer expression )
{
case constant 1:
statement 1;
case constant 2:
statement 2;
case constant 3:
statement 3;
default:
statement 4;
}

Computer Science Department


Switch Statement
• The integer expression following the keyword
switch is an expression that will yield an integer
value. It could be an integer constant like 1, 2,or
3, or an expression that evaluates to an integer.

• The keyword case is followed by an integer or a


character constant. The constant in each case
must be different from other.

Computer Science Department


Example
void main ( )
{
int i = 2;
switch (i)
{
case 1:
cout<<“case 1 \n”;
case 2:
cout<<“case 2 \n”;
case 3: Output of the program
cout<<“case 3 \n”;
Case 2
default:
Case 3
cout<<“default \n”; default
}
}
Computer Science Department
Break Statement
• If we want that only particular case should be executed than break
statement should be used to get out of the control structure.
int i = 2;
switch (i)
{
case 1:
cout<<“case 1 \n”;
break;
case 2:
cout<<“case 2 \n”;
break;
case 3:
cout<<“case 3 \n”;
break;
default:
cout<<“default \n”;
}

Computer Science Department


Tips of using Switch Statement
a) As in the previous examples the cases are
arranged in ascending order 1,2,3 and default.
You can in fact put the cases in any order you
want.
b) You are also allowed to use char values in
case and switch.
c) You can write a case in switch, which is not
followed by any statement. As shown in the
next example

Computer Science Department


Tips of using Switch Statement
char ch;
cout<<“enter any of the alphabet a, b, or c”;
cin>>ch
switch(ch)
{
case ‘a’:
case ‘A’:
cout<<“you entered a”;
break;
case ‘b’:
case ‘B’:
cout<<“you entered b”;
break;
case ‘c’:
case ‘C’:
cout<<“you entered c”;
}

Computer Science Department


Tips of using Switch Statement
d) Even if there are multiple statements to be executed in
each case there is no need to enclose these within a
pair of braces.
e) If we have no default case, then the program simply
falls through the entire switch and continues with the
next instruction that follows the control structure.
f) The disadvantage of switch is that one cannot have a
case in a switch which looks like
case i<=20;
All that we can have after the case is an int constant or
a char constant. Even a float is not allowed.

Computer Science Department


Tips of using Switch Statement
g) The break statement when used in a switch takes the
control outside the switch.
h) In principle, a switch may occur within another, but in
practice it is rarely done.
i) The switch statement is very useful while writing menu
driven programs.

Computer Science Department


The Conditional Operator

Computer Science Department


Conditional Operator
• The conditional operator ? and : are sometime
called as ternary operators since they take three
arguments

expression1 ? expression2 : expression3

• The above statement means that


“ if expression1 is true (i.e. if its value is non-zero),
then the value returned will be expression2
otherwise the value returned will be expression3”

Computer Science Department


Example

int x, y;
cin>>x;
y = (x>5 ? 3 : 4);
cout<< y;

Computer Science Department


Points to be noted about
Conditional Operators
• It is not necessary that the conditional operators
should be used only in arithmetic statements

int i;
cin>> i;
(i==1 ? cout<<“ hello” : cout<<“helloworld”);

Computer Science Department


Points to be noted about
Conditional Operators
• The conditional operators can be nested as shown below
int k, num = 30;
k=(num>5 ? (num <=10 ? 100 : 200) : 500);
cout<< k;

• The limitation of the conditional operator is that after


the ? Or after the : only one C++ statement can
occur.

Computer Science Department

You might also like