Decision making statements if and if else in C
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 :
1 2 3 4 5 6 7 |
if(condition) { // executes the following code if the above condition is true. // Skips the following code if the above condition is false ....... ....... } |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> int main() { int a; printf("Enter positive number : "); scanf("%d",&a); // The following if condition will executes if the given number a is // Even, Otherwise program will do nothing. if(a%2 == 0) { printf("%d is Even number \n", a); } return 0; } |
Program Output :
1 2 3 4 5 6 7 8 9 10 11 |
venkey@venkey:~/Code/c$ vim if.c venkey@venkey:~/Code/c$ cc if.c venkey@venkey:~/Code/c$ ./a.out Enter positive number : 20 20 is Even number venkey@venkey:~/Code/c$ ./a.out Enter positive number : 3 venkey@venkey:~/Code/c$ ./a.out Enter positive number : 6 6 is Even number venkey@venkey:~/Code/c$ |
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.
1 2 |
if(a%2 == 0) printf("%d is Even number \n", a); |
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.
Syntax of else statement :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition) { // if above condition is evaluted as TRUE then following // statements will execute. ....; ....; } else { // if condition is fails, Means condition return as FALSE, // Then following statements will execute. ....; ....; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<stdio.h> int main() { int a; printf("Enter positive number : "); scanf("%d",&a); if(a%2 == 0) { // If the above condition is true then following statments will // execute printf("%d is Even number \n", a); } else { // If above condition a%2 fails, then following statement will // execute printf("%d is ODD number \n", a); } return 0; } |
Program Output :
1 2 3 4 5 6 7 8 9 10 |
venkey@venkey:~/Code/c$ cc if.c venkey@venkey:~/Code/c$ ./a.out Enter positive number : 5 5 is ODD number venkey@venkey:~/Code/c$ ./a.out Enter positive number : 7 7 is ODD number venkey@venkey:~/Code/c$ ./a.out Enter positive number : 10 10 is Even number |
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.
I want… for loop…examples….