If _else Statement

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.
If_else

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;
                   }
Note that their will be no condition written in the else statement for example:-
   else(a<10); It is incorrect.

Example 1
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
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

  1. 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).
  2. No condition or expression is their after else keyword.
  3. The condition is compulsory in the If_else statement.
  4. 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...

Post a Comment

5 Comments

  1. Good brother, nice work with intelligence and I like your posts.

    ReplyDelete
    Replies
    1. Thanks, Hope the upcoming post will help you further my friend.

      Delete
  2. It's very nice my friend. You are helping many people who can't understand these things easily like me bro

    ReplyDelete
  3. Thank u so much ..it is very helpful for me

    ReplyDelete