If else in C

What is the if-else statement in C?

if else
if-else in C
  • If-else in C is a very important statement used to check the condition and accordingly execute a set of statements, based on whether the condition is true or false. Hence it is called a selective statement
  • It is the condition specified in the if the statement is false then the statement of else will be executed
  • It selectively executes some statements and doesn’t execute some
SYNTAX of if-else statement
if(condition)
{
-
statement 1;
-
-
}
 else
{
-
-
statement 2;
-
-
}
  • The set of statements named as a statement in the above syntax are executed if the condition given with the if statements are true
  • The set of statements statements2 are not executed in this case
  • If the condition specified in the if the statement is false then the statements named as statements2 in the above syntax are executed
  • The set of statements statements1 are not executed in this case
  • The else part is optional i.e. we can have an if statement without the else statement
  • The keyword if tells the compiler that what follows is a decision control instruction
  • The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true then the statement is executed

1. Display the user entered number is a prime number or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=2,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(n%i==0)
{
i++;
}
if(n==i)
{
printf("prime number");
}
else
{
printf("not a prime number");
}
getch();
}
OUTPUT:
enter the number:17
Prime number

2. Display the entered number is Armstrong or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,digit,x,copy;
clrscr();
printf("enter the number:");
scanf("%d",&x);
copy=x;
while(x!=0)
{
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
}
if(sum==copy)
{
printf("Armstrong number");
}
else
{
printf("Not an Armstrong number");
}
getch();
}
OUTPUT:
enter the number:789
Not an Armstrong number

If-else Ladder or if-else if

  • In some case, we have to check multiple cases of a particular condition
  • In such a case, we have to use an if-else ladder
  • A set of if-else statements as shown below is called an if-else ladder
Syntax for if-else if
if(condition)
{
statements;
}
else
{
if(condition)
{
statements;
}
else
{
if(condition)
{
statements;
}
else
{
if(condition)
{
statements;
}
|
|
|
else
{
statements;
}
}
}
}
}
  • In this case, the first condition is checked, if it is true the statements inside the if statement are executed
  • But if the condition is false it goes to else statement
  • Again there is a condition with if; the statements are executed if this second condition is true
  • Else it again goes to the else and again checks the condition associated with this if statement
  • Thus if one condition satisfies no other else is checked thereafter

Display a program according to the marks scored by a student.

MarksClass
70-100Distinction
60-69First Class
50-59Second Class
40-49Pass Class
0-39Fail
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("Enter marks scored (0-100):");
scanf("%d",&marks);
if(marks>=70)
{
printf("Distinction");
}
else
{
if(marks>=60)
{
printf("First Class");
}
else
{
if(marks>=50)
{
printf("Second Class");
}
else
{
if(marks>=40)
{
printf("Pass");
}
else
{
printf("Fail");
}
}
}
}
getch();
}
Translate »