4/4/21

If-Else Condition in C Programming Part - II

Hey Friend, till now we have studied about only if statement. If you do not know about IF statement please read the previous tutorial before reading this. In if statement whenever the condition is true a set of instructions will proceed running. But every coin has two faces, till now we only learned about one condition. If the condition is true then the code will execute. In high-level programming, you have to code both situations such as if the first condition is true then a set of instructions will execute & if the condition is not true then the second set of instructions execute. This work is done using if-else statements.

Syntax of IF-Else Condition 

If (condition)

{

Statement 1;

Statement 2;

So on…;

}

Else{

{

Statement 1;

Statement 2 and so on…;

}


Note that:-

1. The Statement inside if and else keyword and generally known as if block and else block respectively.

2. The parenthesis inside the if-else block can be dropped if there is only one condition exists. 

3. You should not use any condition in the else keyword. The statements under else will be executed only if the condition with if statement is false. So it is treated as the default choice.

4.  Notice that the else part or statement of the program is placed just below the if part. And the statement of those blocks has indented to the right. This is a formatting convention that means just to write a program easier or understandable way. Look at the below program to understand in a better way. 


Program to find that the given number is a positive number or a negative number. 

#include <stdio.h>

void main()

{

 int number;

 printf("Enter a number to check if it is negative or positive :");

 scanf("%d",&number);

 if(number<0)

 {

  printf("Number is negative"); 

 } 

 else

 {

  printf("Number is positive");

 }

}

Output :- 


Explanation of the Program

I have already told the meaning of these (include, printf, int main) functions earlier. If you don’t know please check the previous post. In this program, we declare a variable name (number) as an integer value given by the user. 

Now we are going to check the number whether it is a positive number or a negative number.  So in the if block we check, is this number is smaller than zero? If yes then the program of (if block) executes and gives us the output. 

Or if the number is greater than zero then the else block starts to execute and give us the output.


Hope you all understand the program and if you have any query then comment below I will definitely try to explain with another example.  


"+y+""}else{if(A==5){c+='
  • '+w+""+y+"
  • "}else{if(A==6){c+='
  • "+w+'
    '+u+""+y+"
  • "}else{c+='
  • "+w+"
  • "}}}}}s.innerHTML=c+=""+y;d.callBack()};randomRelatedIndex=h;showRelatedPost=g;j(d.homePage.replace(/\/$/,"")+"/feeds/posts/summary"+e+"?alt=json-in-script&orderby=updated&max-results=0&callback=randomRelatedIndex")})(window,document,document.getElementsByTagName("head")[0]); //]]>

    1 comment:

    1. Their personalized care for each project and quick response time continues to stand out.
      San Francisco design companies

      ReplyDelete

    Follow on Twitter

    Linkedin

    Categories

    Contact Form

    Name

    Email *

    Message *

    Mad About Computer. Powered by Blogger.
    This site is in no way affiliated with or endorsed by specified business. It exists as a compendium of supporting information intended for informational purposes only. If you want to buy this website, please don't hesitate to contact us via e-mail: "d e n a c c 9 7 7 (at) g m a i l (dot) c o m" (delete spaces) or you can find and buy it on Afternic domain auctions.