Decision making statements if and if…else in C

Spread the love
if-else-in-programming-decision-making-in-c-language

if-else-in-programming-decision-making-in-c-language

   Decision-making statements or Control statements will help us to control the program execution based on a certain condition.  In many situations, we need to choose one option among the multiple options.

For examples, you’re trying to write a program where you will take input number from the user and you want to say if the number is Even or Odd. In this case, We can use Control statements to execute the appropriate set of statements depending on the user’s input.

Examples of Control statements :

  • if statement
  • if else statement
  • switch statement

In this article, We are going to discuss the if and if else statements. Let’s start with the if statement.

if statement in C:

As the name suggests, If statements check the specific condition and if the condition is true then it will execute the set of statements.

The syntax of If statement :

First of all, if statement will Evaluate the condition, If the condition is TRUE then the statements within the BRACES executed. If the condition is evaluated as FALSE then if will skip the code within the Braces.

if the condition is TRUE, then if will execute the block associated with it

Example :

The program will take the input from the user. Prints if it is EVEN number and it won’t do anything if the given number is ODD.

Output : 

If you have only one statement inside the if block, then you no need specify your statement inside the block quotes. Above if block can also write like below.

If you only have single statement inside the if, you no need to specify it in the block quotes

else Statement in C :

else like the alternative block for if, Means we can have else block along with the if. If the condition is TRUE then if block will be executed, If the condition is false then else block will be executed. 

Syntax of else statement :

Now let’s rewrite above EVEN number program to print the ODD numbers also. Add the else block just after the if condition.

Program to understand else statment:

Output :

By looking at the output of above program,

If the user enters EVEN number then, IF condition will became TRUE and The program will display, given number is EVEN. ELSE part of the code will be ignored.

If the user enters ODD number then, IF condition will be FALSE and statements in the if block will be skipped and ELSE part of the code will be executed. So program will display, Given number is ODD.

Only one of the if and else blocks will be executed, Both blocks won’t execute.

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

Leave a Reply