Decision making statements if and if else in C

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.

if-statement-in-c-language

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 : 

If you have only one statement inside the if block, then you no need to 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

The if else Statement in C :

The else is like an 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. 

if-else-statement-in-c-language

Syntax of else statement :

Now let’s rewrite the 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 :

By looking at the output of the above program,

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

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

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

Related Articles:

Venkatesh

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

17 Responses

  1. Manali says:

    I want… for loop…examples….

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

  2. […] Source link […]

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

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

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

  6. […] if else statement in C language […]

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

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

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

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

  11. […] need to know the basics of the decision-making statements like the if..else statement, if else ladder, and switch case. please go through the following articles to learn […]

  12. […] If else statement […]

  13. […] If else statement […]

Leave a Reply