If and Else Statement in C
Hello guys,
In the last time we learn about the format specifires and
datatypes in C. In this lecture we will learn about the If and else statements
in C.
Firstly understand the meaning of if and else we use it in
the general life. For example “if tomorrow is holiday we will plan for the tour
else we will continue our work”.
Here else is not much important, in the same manner in C
else has not much importance in if and else statement. But we use it for
clearing the programs meaning.
Let see the above example here condition is applied i.e. tomorrow
is holiday
Like these conditions are in C
Let us understand these conditions
The syntax is
if()
{
//code;
}
else
{
//code;
}
Lets see the program
Here we can see that IF and ELSE statement.
Here we assigned the value 1 in variable named as n.
we simply used equal condition for execution of the program, we already assigned the value in the variable so it will continue the program and ignore the else statement.
When we enters in the if statement compiler checks the
condition, if the condition is true then the code from if statement runs i.e. It
will continue the program, if condition is false it will jump to the else statement.
If you want to add more conditions than one then user have
to use else if statement.
The syntax is as follow
if()
{
//code;
}
else if ()
{
//code;
}
else
{
//code;
}
Here the compiler simply check the conditions if the
condition is true it will continue the program, else the compiler will jump
from that code and will continue execution
Exercise
You have to make the program based on the multiple
conditions.
Program
Days |
Paper |
Monday |
Math |
Tuesday |
English |
Thursday |
Computer science |
Wenesday |
History |
Friday |
Geography |
Saterday |
Biology |
In the above table 6 days shedulde and the paper are given.
You have to take input from the user in the form of number
declared specific number for the day.
Then simply you have to print the exact subject shedulded on
the day.
If user enters wrong statement then simply print “Invalid
input”.
Output
On Monday the paper was of Math
I hope you have absorbed the information from this post.
Thanks for reading
0 Comments