Comparison Operators:
------------------------------
As the name suggests comparison operators are used to compare two expressions, values or variables. Note, Comparison Operators returns their result in either true or false.
These are the Comparison Operators: ==, !=, >, =, <=.
== (Equals to) operator is used to compare two expressions. Say we have two variables X and Y with any values stored in them. Then, we can compare their values like,
if(X==10)
{
//ny statement
}
It will return true if value in x equals 10 or false otherwise.
Same as, we can compare for a expression that is not equals a value in a variable with !=(Not Equal to) operator. i.e.
If(X!=40)
{
//any statements
}
It will return true if value in x is not equals to 40 and false otherwise.
Same as above you can use the remaining comparison operators which are (> Greater than operator), (= Greater than equal to operator), (<= Less than equal to operator). you are also familiar with these operators as these are the basic operators used in mathematics.
----------------------------------------
Now we'll continue with Logical Operators.
Logical Operators:
-----------------------
Logical operators are used to test any two or more conditions. C++ provides the AND(&&), OR(||) and Not(!) logical operators.
AND(&&) Operator:
----------------------
It evaluates to true if all the specified conditions(two or more) are true or it returns false if any of the specified conditions results in false. Like, we have two variables, say, X and Y with values 976 and -29 respectively. We can check the values in these variables using &&(AND) operator. For eg.
if(X>897 && Y>0)
{
//any statements
}
Now what the above expression will return. Just guess!
It is going to return false because
the first expression (X>897) will return true, but
the second expression (Y>0) will return false coz y having value of (-29) i.e. less than zero.
-------------------------------
It evaluates to true if any of the specified conditions(two or more) are true or it returns false if all of the specified conditions results in false. Like, we have two variables, say, X and Y with values 976 and -29 respectively. We can check the values in these variables using &&(AND) operator. For eg.
if(X>897 || Y>0)
{
//any statements
}
Now what the above expression will return. Just guess!
It is going to return true because first statement will return true and if any conditions results a true in OR operator it returns true.
the first expression (X>897) will return true, but
the second expression (Y>0) will return false coz y having value of (-29) i.e. less than zero which will not effect the result (True this time).
Not(!) Logical Operator:
-------------------------------
The effect of this operator is that the logical value(result) of its expression is reversed.
Let's understand it with an example, suppose we have a variable X with value 12.
and if we check its value, like.
If(!(X>10))
{
////any statements...
}
Now the interesting question is, what the above exression will result. Just think by urself.
It is going to return false.
I'll tell you why.
firstly the inner expression (X>10) will return true because x is having a value greater than 10 means x is equals to 12.
now the use of NOT(!) operator has reversed the result, as it is intended to do.
That's why the above expression will return false.
---------------------------
You can exchange simple if-else code for a single operator – the conditional operator. The conditional operator is the only C++ ternary operator (working on three values).
FORMAT:
-----------
conditional Expression ? expression1 : expression2;
** if the conditional Expression is true, expression1 executes, otherwise if the conditional Expression is false, expression 2 executes.
If both the true and false expressions assign values to the same variable, you can improve the efficiency by assigning the variable one time:
(a>b) ? (c=25) : (c=45);
can be written as:
c = (a>b) ? 25 : 45;
Or Let's take another example:
Suppose X=10 and we can check for a condition i.e. X equals to 10 or not. Like
(x==10)? cout<<"x is ten": cout<<"x is not ten";
or the same work can be done also by the if else construct instead of Conditional operator. Like
if(X==10)
{
cout<<"X equals ten";
}
else
{
cout<<"Y not equals ten";
}
Conditional constructs
The If... else Construct.
The if...else conditional construct is used to make decisions for selective execution or programming statements based on the result of the comparisons. If conditional construct is followed by a logical expression where data is compared in the specified expression.
Syntax:
if(expression)
{
///any statements here.
}
else
{
///any statements here.
}
Eg.
#include
void main()
{
clrscr();
char ch;
cout<<"enter a character";
cin>>ch;
if(ch=="A")
{
cout<<"The character is A"<
else
{
cout<<"The character is not A"<
getch();
}
if we execute the above program. Firstly it'll prompt the user to enter a character. Based on the character entered it'll show the result by making a decision whether the entered character was "A" or "not A".