Relational operators are
operators which we use to evaluate two or more statements , like weather statement
A is equal to statement B and so on , different relational operators are
= =
|
Equal to
operator
|
! =
|
Not equal
to operator
|
>
|
Greater than
operator
|
<
|
Less than
operator
|
>=
|
Greater than
equal to
|
<=
|
Less than
equal to
|
These are our basic relational
operator . But these operator does not work alone , they are used with other
operators in order to get some output and these are called selection statements .
Selection statements and relational operators have to work side by side in order to get result , so let us see what are selection statements and how we use relational operators with them
Selection statements and relational operators have to work side by side in order to get result , so let us see what are selection statements and how we use relational operators with them
Selection
Statements
These are used in a program to
make logical decision which is the heart of programming language , so let us
see different selection statement with working examples
If statement – in
order to understand if statement let us consider a example , suppose you have a
variable that stores age of a person and you want that if the age is above 80
you display a special message , so how you are going to do it , we do it with
if statement and relational operator ,
The syntax for if
statement looks like
if (condition)
{
}
So our above example will look
like
If ( age > 80)
{
printf(“ Welcome sir ! you have made a long way “);
}
That is how if statement works ,
now if you want to display another message for those whose age is not 80 , we
do it with if else statement , like this
If ( age > 80)
{
printf(“ Welcome sir ! you have made a long way “);
}
else
{
printf(“ Welcome sir”);
}
this is how we use if else
statement , now imagine a situation in which we want to display multiple
message based on multiple conditions , so in this situation we use else if
statement like this
If ( age > 80)
{
printf(“ Welcome sir ! you have made a long way “);
}
else if(age >70)
{
printf(“ Welcome sir”);
}
else if(age >50)
{
printf(“good day”);
}
else if(age > 30)
{
printf(“hello”);
}
else
{
printf(“hi”);
}
This is how we use the if
statement , we can use the if statement with all the relational operators to
get the output
In order to understand the if statement
let us make a program to accept a single number from the user from 0 to 9 and
display a quote for each number
main()
{
int number;
printf("Please enter a number between 0 to 9\n");
scanf("%d",&number);
if(number == 0)
{
printf("I like the dreams of the future better than the history of
the past");
}
else if(number == 1)
{
printf("You cannot dream yourself into a character: you must hammer
and forge yourself
into
one");
}
else if(number == 2)
{
printf("Commitment leads to action. Action brings your dream
closer");
}
else if(number == 3)
{
printf("To unpathed waters, undreamed shores.");
}
else if(number == 4)
{
printf("Hope is the dream of the waking man");
}
else if(number == 5)
{
printf("The best way to make your dreams come true is to wake
up.");
}
else if(number == 6)
{
printf("The future belongs to those who believe in the beauty of
their dreams");
}
else if(number == 7)
{
printf("Imagination is more
important than knowledge.");
}
else if(number == 8)
{
printf("Your imagination is your preview of life's coming
attractions");
}
else
{
printf("Believe that you have it, and you have it");
}
getch();
}
This is a very simple program ,
just type a digit and it will display a message corresponding to the digit
Switch Statement
As we can see if statement can be
very useful but switch is another selection statement that is widely used ,
unlike if statement switch statement only compare a single value with a
multiple conditions , the switch syntax is
switch(integer_expression)
{
Case 1: printf(“statement “);
Break;
Case 2: printf(“statement “);
Break;
Case 3: printf(“statement “);
Break;
default: printf(“statement”);
}
Switch statement does not accept
conditions or comparisons , it takes a value and evaluates according to the
cases , we add a break; because if the required case is obtained then the
program comes out of the structure saving resources , So the above example on
quotes can be re-written as
main()
{
int number;
printf("Please enter a number between 0 to 9\n");
scanf("%d",&number);
switch(number)
{
case 0:
printf("I like the dreams of the future better than the history of
the past");
case 1:
printf("You cannot dream yourself into a character: you must hammer
and forge yourself into one");
break;
case 2:
printf("Commitment leads to action. Action brings your dream
closer");
break;
case
3:
printf("To unpathed waters, undreamed shores.");
break;
case
4:
printf("Hope is the dream of the waking man");
break;
case
5:
printf("The best way to make your dreams come true is to wake
up.");
break;
case
6:
printf("The future belongs to those who believe in the beauty of
their dreams");
break;
case
7:
printf("Imagination is more important
than knowledge.");
break;
case
8:
printf("Your imagination is your preview of life's coming
attractions");
break;
default:
printf("Believe that you have it, and you have it");
}
getch();
}
This program also does the same
thing , just type a digit and it will
display a message corresponding to the digit
No comments:
Post a Comment