C – Conditional Statements

After reading this conditional statements topic, you will understand conditional statements types and you will know the conditional statements flowchart, theory, and examples.

Like the real-life situation, C language provides the facility to user to take decisions based on any condition in a program. The C statements through which we can control the execution of the program is called decision control statements.

The conditional statements execute different statements based on different conditions. It can be classified as shown below into uni-directive statement, bi-directive statement and multi-way branching statement.

The uni-directive statement executes statements when the true condition evaluates, for example, if statement.

Bi-directive statement executes statements either under true condition or under false condition evaluates, examples like if-else, if-else-if statement.

In multi-way branching statement, statements execute only under true condition choice out of many choices and switch statement is the example of multiple branching statement.

C if statement

The if statement also called uni-directional statement i.e. to execute statements only under true condition evaluates.

General Form (Syntax):

The syntax of if statement is given below,

if(expression)
{ 
statement(s);
}

The if statement executes statement(s) written within the curly braces (i.e. { } ) as expression evaluates to true (non zero) condition and when expression evaluates to false (zero) condition, statements within the curly braces do not execute. The expression can be relational expression and/or logical expression must be put inside the parenthesis (i.e. ( ) ) followed with the keyword if and should not be terminated with semicolon. For single statement braces (i.e. { }) are optional.

if statement flowchart:

The flow chart of if statement is given below,

Example:

Program (1): To check the number, enter by the user is positive using if statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
   int num;
   printf("Enter a Number : ");
   scanf("%d",&num);
if (num>0)
{
printf("Number is positive");
}
getch();
}

Output (1)

Enter a Number : 123
Number is positive

Program (1): To find the absolute value of a number using if statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
   int num;
   printf("Enter a number : ");
   scanf("%d",&num);
   if(num<0)
   num=num*-1;
printf("absolute value of number is %d",num);
getch();
}

Output (1)

Enter a number : -123
absolute value of number is 123

C– if-else statement

The if-else statement also called bidirectional statement in which statements under either true (non zero) or false (zero) condition executed.

General Form:

The syntax of if-else statement is given below,

if(expression)
{ 
statement(s);
}
else
{ 
statement(s);
}

The if-else statement executes if statement(s) written within the curly braces (i.e. { } ) as expression evaluates to true (non zero) condition and skip else statement(s).

The if-else statement executes else statement(s) written within the curly braces (i.e. { } ) when expression evaluates to false (zero) condition and skip if statement(s).

The expression can be relational expression and/or logical expression must be put inside the parenthesis (i.e. ( ) ) followed with keywords if. The keywords if and else should not be terminated with semicolon.

if-else statement flowchart:

The flow chart of if-else statement is given below,

Examples

Program (1): To check the number, enter by the user is positive or negative using if-else statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
   int num;
   printf("Enter a Number : ");
   scanf("%d",&num);
if (num>0)
printf("Number is positive");
   else
       printf("Number is negative");
getch();
}

Output (1)

Enter a Number : -123
Number is negative

Program (1): To check number is even or odd using if-else statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
   int num,rem;
   printf("Enter a number : ");
   scanf("%d",&num);
   rem=num%2;
   if(rem==0)
     printf("number %d is even",num);
  else
     printf("number %d is odd",num);
getch();
}

Output (1)

Enter a number : 25
number 25 is odd

C – if-else-if statement

General Form:

The syntax of if-else-if statement is given below,

if(expression1)
{ 
statement(s);
}
else if(expression2)
{ 
statement(s);
}
.
.
.
else
{
statement(s);
}

The if-else-if statement executes if statement(s) written within the curly braces (i.e. { } ) when expression1 evaluates to true (non zero) condition as well as skips else and else-if statement(s).

The if-else-if statement executes else-if statement(s) written within the curly braces (i.e. { } ) when expression2 evaluates to true (non zero) condition as well as skips if and else statement(s).

The if-else-if statement executes else statements written within the curly braces (i.e. { } ) when expression1 and expression2 both evaluate to false (zero) condition as well as skips if and else-if statements.

Example:

Program (1): To check the number, enter by user is positive, negative or zero using if-else-if statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int num;
printf("Enter a Number : ");
scanf("%d",&num);
if (num>0)
printf("Number is positive");
else if (num<0)
printf("Number is negative");
else
printf("Number is zero");
getch();
}

Output (1)

Enter a Number : 23
Number is positive

Program (1): To find the roots of quadratic equation, the coefficients of the quadratic equation are entered by the user.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
float a,b,c,d,x1,x2,x3;
printf("Eneter a,b,and c of qudratioc equation\n");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("The x1 is %f\n the x2 is %f\n",x1,x2);
}
else
if(d==0)
{
x3=(-b)/(2*a);
printf("the real and equal roots are %f\n",x3);
}
else
{
printf("The roots are complex\n");
}
printf("the value of d is %f\n",d);
getch();
}

Output (1)

Eneter a,b,and c of qudratioc equation
2
3
6
The roots are complex
the value of d is -39.000000

Program (1): To check number entered by user Armstrong number or not.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int arm,num,d1,d2,d3,d4;
printf("Enter number = ");
scanf("%d",&num);
d1=num%10;
d2=num/10;
d3=d2%10;
d4=d2/10;
arm=d1*d1*d1+d3*d3*d3+d4*d4*d4;
if(num==arm)
{
printf("Enter number is armstrong\n");
}
else
{
printf("Enter number is not armstrong\n");
}
getch();
}

Output (1)

Enter number = 234
Enter number is not armstrong

Program (1): To check year enter by the user is Leap year or not.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int yr;
printf("Enter any year = ");
scanf("%d",&yr);
//condition for leap year
if((yr%400==0)||(yr%100!=0&&yr%4==0))
{
printf("%d is leap year\n",yr);
}
else
{
printf("%d is not leap year\n",yr);
}
getch();
}

Output (1)

Enter any year = 1989
1989 is not leap year

Program (1): To find addition, subtraction, multiplication, and division of two numbers entered by the user.

#include<stdio.h>
#include<conio.h>
main()
{
float a,b,sum,sub,mul,div;
printf(" Enter two numbers\n");
scanf("%f %f",&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
if(b==0)
{ 
printf("For division, enter second number except 0\n");
}
else
{
div=a/b;
}
printf("sum is%f\n sub is %f\n multiplication is %f\n division is %f\n ",sum,sub,mul,div);
getch();
}

Output (1)

 Enter two numbers
23
45
sum is68.000000
 sub is -22.000000
 multiplication is 1035.000000
 division is 0.511111

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *