Relational
Operators used in decision or selection statements. It compares status of
one variable with another variable value as greater than, less than or equal
to. Following is a list of relational operators:
Operator
Name
|
Syntax
|
Less than or equal to
|
<=
|
Greater than
|
>
|
Less than
|
<
|
Greater than or equal to
|
>=
|
Equal to
|
==
|
Not equal to
|
!=
|
Logical
Operators used in decision or selection statements and are three in number
such as &&, ||, and ! read as AND, OR and NOT
respectively. The first two logical operators allow two or more conditions to
be combined in if-statement. The following table shows logical operators:
Logical
operators
|
Syntax
|
Logical AND
|
a && b
|
Logical OR
|
a || b
|
Logical Negation (NOT)
|
!a
|
if statement is used to take decision about specific
condition to execute. The body of if-statement is executed if value of the
expression or condition specified in the if-statement is true. For example if
there is raining outside, I will take umbrella so here condition is rain for
which umbrella must be taken in case to go outside.
Syntax of if-statement is as follows:
if
(condition)
{
Block of statements;
}
if-else
statement is similar to if-statement
with an addition of else statement.
If the condition is false in if-statement, then if-statement body will be
skipped and else body will be
executed. For example if there is raining outside, I will take an umbrella else
I will go outside without an umbrella. So when first condition doesn’t satisfy
then second condition will be followed. Syntax of if-else statement is as follow:
if
(condition)
{
Block of statements;
}
Else
{
Block
of statements;
}
switch
statement allows us to make a decision from a number of choices. The syntax
for switch statement is as follows:
switch
(integer expression)
{
Case constant 1:
Do this;
Break;
Case constant 2:
Do this;
Break;
Default;
Do this;
Break;
}
The integer expression can be any C expression
that yields to integer value and could be an integer constant like 1, 2, 3 or
an expression that evaluates to an integer value. case is keyword and must be followed by integer or character constant and constant
in
case must be different from all
other constants.
Comments
Post a Comment
if you have any confusion then ask hear.