The if and else statement
![qa-featured](https://www.techyv.com/sites/default/users/superadmin/qa-featured-300x270.png)
My previous question was about break and continue and i forget to ask one more question that is tell me about if and else statement.why we use this and also the syntax
![](https://techyv.com/sites/default/2016/10/techpedia_logo.png)
My previous question was about break and continue and i forget to ask one more question that is tell me about if and else statement.why we use this and also the syntax
The if and else statements can be considered as the decision making statements.In simple words its like daily life decisions,for example IF ALIC GOES TO THE PARTY THEN I LL GO ELSE I LL NOT GO.So in programming terms if the first condition is true then else statement ll not be executed.If first condition gets false then else condition ll be executed.
There is a simple C program to show how if and else statements works.
for(i=0;i<5;i++)
{
if(x==5)
cout<<"this is the last value of the loop";
else
cout<<"this is not the last value of the loop";
}
These are two commands we give to computer and computer do some thing on this behalf. The if statement is used where the condition is dependent on if mean that if some thing happens then do this thing and else statement is used where you need to specify that if this thing does not happen then do some thing else. This is the basic thing in if and else statement. You need to use them on your demand. You can only use if statement or else statement and also you can use them together. You will be then able to get your result as you require. The thing here is the result you want form this statement the statement does not contain any result but if you want to control a airplane then it does matter. You can even use it in a mouse where a click is for if statement and else is for another click. This is the basic thing you need to keep in mind.
If there are two conditions then we can use IF ELSE statements in the programming language. IF & ELSE statement is two most important & salient features in the C Programming Language. These two statements are used greatly in writing code in C programming language. Now I am giving a very simple example of these two statements in a few words.
Suppose you have to finish a work. You have two options to finish by hiring Michel Or John. Michel is demanding more money than John wants. But you choose that Michel does the work. Then the algorithm is like this;
IF(Michel Agree)
Prinf( Michel will finish the Job);
Else
Prinf(John will work the Job);
Basically, if-else statement is condition-based statement. Whenever we want to take any action which is based on the outcomes of the given condition, (True or False) , then we need to use the if-else statement.
The syntax for this statement is as follows:
if(condition)
the statement // x1
else
the statement . // x2
If the condition in the syntax is true, then the x1 is executed and if the condition in the syntax is false then x2 is executed. Let us consider an example,
if (y>z)
printf ("the greater no is %d", y);Â // x1
else
printf("the greater no is %d", z);Â // x2
Here, if y is greater than z then x1 is executed, otherwise x2 will be executed.
As a whole, if-else statement is totally based on the condition given in the synatax.