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

if and if else statements Introduction:   

Decision-making statements or Control statements ( if and if else 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.

The if statement in C:

As the name suggests, The 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 if statement :

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.

Program Output : 

[/crayon]

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.

[/crayon]

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

The if else Statement in C :

The else is like a 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 :

[/crayon]

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 if and if else statement:

Program Output :

[/crayon]

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.

Related Articles:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. February 6, 2020

    […] our previous post, we discussed the if and if…else statements. Today we will discuss Nested if…else and if…else […]

  2. May 24, 2021

    […] Source link […]

  3. June 15, 2021

    […] Decision making statements if and if…else in C […]

  4. July 25, 2021

    […] Operator works exactly like the if-else statements in C Langauge. Whenever possible use the Conditional operator, It is simple and […]

Leave a Reply