Conditional Statement in C Program.
Hi Everyone,
Today we will discuss about condition/decision statement in C like If..else, Nested If..else and else..if Statement with basic example. Before Start with Conditional statement in C , Please go through my previous blogs for your reference where i defined the syntax and keywords with basic C programs included one IF conditional statement so it’s easy for you to go through this blog . Previous blog link is as below :
https://dheeraj60.blogspot.com/2020/06/introduction-to-c-program-dotcmdb.html
https://dheeraj60.blogspot.com/2020/06/c-basics-programs-with-with-different.html
Let’s Start with Conditional/decision statement in C . Type of Condition/Decision statement in C is as below :
IF
IF –ELSE statement
Nested IF _ELSE Statement
ELSE –IF Statement
In Previous blog i already defined IF statement with basic programs so let’s start with IF-ELSE statement in C.
IF-ELSE – If condition is true then the statements inside the body of if are executed and the statements inside body of else are skipped. If condition is false then the statements inside the body of if are skipped and the statements in elseare executed. In Simple word we can say we can opt another option for check if condition are true first statement is executed and if it is false then it will go to other statement that is else.
Syntax of IF – ELSE Statement :
if(condition) {
// Statements in body executed if condition is true
}
else {
//Statements will execute if statement is false
}
Let’s take one simple program to understand this conditional statement in which we take input from user in number and check if number equals to 18 or greater than 18 it will return number is equal or greater than 18 if user enter less than 18 number the program will give number is less than 18 . So it will check first statement if that is true then it will execute first statement otherwise it will go to else statement and give the output.
Below is the Program .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
printf("Enter any number:");
scanf("%d",&number);
if(number >=21)
{
/* This statement will execute if the
* condition (number>=21) returns true
*/
printf("number is equal or greater than 21");
}
else
{
/* This statement will only execute if first statement is false
*/
printf("Number is less than 21 !");
}
return 0;
}
Here in Above Program below is comment only for first if statement you can omit this part, but i suggest you to make comment if you write any big program then it’s helpful for you to understand which statement is doing what .
/* This statement will execute if the
* condition (number>=21) returns true
*/
Once you run this program below is the output of the program in code block.
Here we have given number 67 which is greater than 21 so first statement will executed , if we enter less than 21 then else statement will execute and ouput will be Number is less than 21 ! , but here the first statement is true so output will be number is equal or greater than 21. You can write anything in printf as output for your requirement.
In IF – ELSE statement you can write any program if you are not sure if first statement is true then you can check for another condition and execute the program as per your requirement.
NESTED – IF ELSE STATEMENT: When an if else statement is present inside the body of another if or else then this is known as NESTED –IF ELSE Statement. In other word we can say we can say inner and outer statement.
See the Below syntax of NESTED – IF ELSE STEMENT
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
Let’s understand with simple example of NESTED IF – ELSE Statement where we can put multiple condition in nested IF – ELSE by taking input number from user and display the output against our requirement.
Below is the program where i use two variables with integer type.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2;
printf("Enter the value of num1:");
scanf("%d", &num1);
printf("Enter the value of num2:");
scanf("%d",&num2);
if (num1 != num2)
{
printf("num1 is not equal to num2\n");
//Nested if else
if (num1 > num2)
{
printf("num1 is greater than num2\n");
}
else
{
printf("num2 is greater than num1\n");
}
}
else
{
printf("num1 is equal to num2\n");
}
return 0;
}
In Above program we use != in first if statement that operator means not equals to . so from the program we will get 2 output as if user will pass 2 different numbers then 1st output will be num1 is not equals to num2 and other output which satisfy the condition passed by user . If user will pass the same number for both variable then only 1 output that is else condition num1 is equal to num2.
Below is the output of above program in code block .
The ELSE - IF statement is mainly used when we need to check multiple conditions within the program, nesting of if-else statement can be avoided using ELSE-IF statement.
Syntax of ELSE- IF Statement
if (condition1)
{
//This statement execute if the condition1 is true
}
else if(condition2)
{
//This statement is execute if the condition2 is true
}
else if (condition3)
{
//This statements is execute if the condition3 is true
}
.
.
else
{
//This statements execute when all the above conditions is false.
}
Let’s take the same example nested if else with two variables now we will do same by using ELSE- IF statement and see how it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2;
printf("Enter the value of num1:");
scanf("%d", &num1);
printf("Enter the value of num2:");
scanf("%d",&num2);
if (num1 !=num2)
{
printf("num1 is not equal to num2\n");
}
else if (num1 > num2)
{
printf("num1 is greater than num2\n");
}
else if (num2 > num1)
{
printf("num2 is greater than num1\n");
}
else
{
printf("num1 is equal to num2\n");
}
return 0;
}
Below is the output of above program in code block compiler .
Important Points :
Conditional Statement else and else..if are optional statements,as a program having only if statement would run fine. else and else..if cannot be used without the if. We can have any number of else..if statement in a if else..if block. If no condition Matches then last else block will execute . We can also use logical operators such as AND (&&), OR(||) and NOT(!) in conditional statement.
Pictorial representation of conditional Statement in C .
Using Condition statement you can now able to write any logical programs by using conditional statement in C for checking numbers greater or equals to etc , also you can check by using condition whether the number passed by user is odd or even . You can write so many basic programs by using conditional statement also for calculation and other programs which we used in previous blog you can use Conditional logic. So try to write some logical programs by using operators or Logical operator and make good program as per your requirement.
I hope you are now able to write some calculation programs used for calculator applications and can also able to use logical/Conditional operator to make control over program and can pass multiple condition in Program. Please try to write as many programs as you can as in next blog we will move to Loop concept in C Program which is very important for programmer and very interesting as well.
This is all about Condition statements in C program. Please read this blog carefully and try to write some good programs so in next blog when we start loop we can use conditional statement in loop.
Please let me know if you have any doubts in this blog.
Thanks.
Very well explained !
ReplyDelete