If_else statement is used to control the flow/way of the Compiler in a program. Basically it works on whether the condition is "True" or "False" and so the block is executed accordingly.
Syntax:- if(expression) // Here "expression" means "Condition"
{
Statement 1; /* Here are the statements which will execute only
Statement 2; when condition is right*/
Statement 3;
:
:
Statement n;
}
else
{
Statement a; /*Here are the statements which will execute only
Statement b; when condition is false or wrong */
Statement c;
:
:
Statement z;
}
{
Statement 1; /* Here are the statements which will execute only
Statement 2; when condition is right*/
Statement 3;
:
:
Statement n;
}
else
{
Statement a; /*Here are the statements which will execute only
Statement b; when condition is false or wrong */
Statement c;
:
:
Statement z;
}
Note that their will be no condition written in the else statement for example:-
else(a<10); It is incorrect.
⇾If all the statements are written using only "If" statement then the compiler is going to check each and every if condition (Example 2) and print the results as per the condition given. It may print one or more results.
Example 1 |
⇾If all the statements are written using only "If" statement then the compiler is going to check each and every if condition (Example 2) and print the results as per the condition given. It may print one or more results.
Example 2 |
⇾If the statements where written using both if_else statement then the compiler will check it until and unless the condition is satisfied. And after then the compiler will move out of if_else statement and it is not going to check the conditions anymore i.e, if one condition is true then it will not go to the next if_else condition even it satisfied the condition(Example 3). It will print only one result.
Example 3 |
Things to Remember
- If no braces("{}") are used after the keyword "If "or"else" then only the next line is going to be a part of If_else (Example 1).
- No condition or expression is their after else keyword.
- The condition is compulsory in the If_else statement.
- While writing "else if" don't forget to leave the space between "else" and "if".
For giving some suggestion please check our "Contact us" button. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog. Thank you for your support and time...
5 Comments
Good brother, nice work with intelligence and I like your posts.
ReplyDeleteThanks, Hope the upcoming post will help you further my friend.
DeleteIt's very nice my friend. You are helping many people who can't understand these things easily like me bro
ReplyDeleteThank u so much ..it is very helpful for me
ReplyDeleteThanks. Hope it will help you in future too.
Delete