conditional statements in C Programming
c programming notes for gate C programming Tutorials gate study material for cse

Conditional Statements in C Programming

Conditional Statements in C Programming

Condition statements in C are used almost in every program written in the C programming language. This is a very important concept and a useful building block of programming construct.

Every student should know how to use conditional statements in a C program ?

The objective of this tutorial is to help the students understand the concepts of conditional statements in C and how to use them in a C program.

Different types of Conditional statements used in C Programming are properly explained in this tutorial. Students are requested to read the tutorial completely.

Control statements in C is another term used for conditional statements.

Frequently Asked Questions

Some widely asked questions from conditional statements in c are given below.

  • What are conditional statements in c?
  • What is the difference between if-else and nested if-else?
  • Write a program for an if-else ladder?
  • What is Switch Case Statement in C?
  • What are 4 conditional statements in C ?
  • Can we use if-else in switch case ?
  • What is difference between if -else and switch case?
  • Which is better switch case or if else ?
  • Is switch case a conditional statement ?
  • How do you use if-else ?

After reading this tutorial, students can answer all the above questions and also can write the program using conditional statements in the C Programming language.

Let’s start with the introduction of the Conditional Statement in C Programming.

conditional statements in C Programming

Also Read Evolutionary Model in Software Engineering

What are Conditional Statements in C Programming?

The conditional statements in c are the statements that are used to check any particular condition according to the user’s requirement. These conditional statements in c help us to make a decision in the program in any condition.

  • We specify the condition as a set of the conditional statements having the Boolean expression, which may be either true or false.
  • The execution flow of a c program is changed as per the condition specified in conditional statements.
  • Conditional statements are also helpful to make a control flow graph of a C Program.
  • Helpful in Calculating the Cyclomatic Complexity of a Program.
  • Conditional statements are also used in finding the coverage criteria’s

Types of Conditional Statements in C

In C programming language, there are the following types of conditional statements in c.

  1. if Statement
  2. if-else Statement
  3. nested if-else Statement
  4. switch Statement
  5. if-else if ladder

Let’s see these conditional statements in C one by one.

1.If Statement in C

Definition

If statement is a basic conditional statement in C Programming. If Statement is used as a single statement in C program, then code inside the if block will execute if a condition is true. It is also called a one-way selection statement.

Syntax of If Statement

The general syntax of if Statement in c is given below

if(expression)

{

//code to be executed

}

If the condition is evaluated as true, then the block statement is executed, but if the condition is evaluated as false, then control is passed to the next Statement following it.

C Program for if Statement

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

printf(“enter the number”);

scanf(“%d”,&n);

if(n%2==0)

{

printf(“%d number in even”,n);

}

getch();

}

Program Output

We have executed this program on Turbo C Compiler and got the output as shown in the following picture.

Key Point to Remember- In C Programming Relational Operators such as < less than, <= less than or equal to, > greater than, >= greater than or equal to, == equal to, != not equal to are used to formulate expression for making a decision or testing conditions.

2.if-else Statement in C

Definition

if-else Statement in c allows two-way selection. If the given condition is true, then program control goes inside the if block and executes the Statement.

The condition is false, then program control goes inside the else block and executes the corresponding Statement.

Syntax of if-else Statement

The general syntax of if-else Statement in c is given as following.

if(expression or condition)

{

// statement ;

}

else

{

//statement ;

}

C Program for if Statement

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter the number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“%d number in even”,n);
}
getch();
}

 

Program Output

We have executed this program on Turbo C Compiler and got the output as shown in following picture.

if conditional statement in c

Key Point to Remember- In C Programming Relational Operators such as <  less than, <= less than or equal to ,  > greater than , >= greater than or equal to, == equal to, != not equal to are used to formulate expression for making a decision or testing conditions.

Also Read – Merge Sort and It’s Time Complexity Tutorial

2.if-else Statement in  C

Definition

if else statement in c allows two-way selection. If the given condition is true. hen program control goes inside the if block and execute the statement.

The condition is false then program control goes inside the else block and execute the corresponding statement.

Syntax of if-else Statement

The general syntax of if else statement in c is given as following.

if(expression or condition)
{
// statement ;
}
else
{
//statement ;
}

Program for if-else Statement

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter the number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“%d number in even”,n);
}
else
{
Printf(“%d number is odd”,n);
}
getch();
}

 

Program Output

We have executed this program on Turbo C Compiler and got the output as shown in following picture.

if conditional statement in c

Point to Remember – Conditional Operator in C is an alternative of simple if statement.

3. Nested if-else Statement

Point to Remember – Conditional Operator in C is an alternative to simple if Statement.

Definition

A nested if-else statement is used to check more than one condition. Nested if-else is also known as a multi-way selection statement. If there is a series of decisions is involved in a statement, we use if else in nested form.

Syntax of Nested if-else Statement

The general syntax of else if-else-if is as follows.

if(expression)

{

Statement

}

else if

{

// Statement

}

else if

{

//Statement

}

else

{

// Statement

}

The execution of the above nested if-else Statement is as follows.

First, it will check the given condition associated with the if statement; if the result of the given condition is True, then the Statement inside the if block is executed.

If the given condition is false, then it will check the first else if part and the condition of is true then the statements related to else if is Executed otherwise the pointer goes to next else if and this process is contained And if the result of the all the else if a condition is false then the pointer is automatically going to else part and the Statement of the else automatically executed.

Nested if-else Program

Following the program to find the greatest number among the three number.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr();
printf(“Please Enter three number”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf(“a is greatest number”);
}
else
{
printf(“c is greatest number”);
}
}
else
{
if(b>c)
{
printf(“b is greatest number”);
}
else
{
printf(“c is greatest”);
}
}
getch();
}

 

Program Output

We have executed this program on Turbo C Compiler and got the output as shown in following picture.

if else statement in c

 

4.if-else-if Ladder Statement in C

Definition

The if-else-if conditional Statement in c is used to execute one code from multiple conditions. It is also known as a multi-path decision statement. It is a sequence of if-else statements where every if a statement is associated with else if Statement and last would be an else statement.

Syntax of If-else-if Ladder Statement

The general syntax of if-else- if a ladder is shown below

if(expression1)

{

//statements

}

else if(expression2)

{

//statements

}

else if(expression3)

{

//statements

}

else

{

//statements

}

if else if ladder Program

#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf(“enter a number”);
scanf(“%d”,&a);
if( a%5==0 && a%8==0)
{
printf(“This number divisible by both 5 and 8”);
}
else if( a%8==0 )
{
printf(“This number is divisible by 8”);
}
else if(a%5==0)
{
printf(“this number is divisible by 5”);
}
else
{
printf(“This number is divisible by neither 8 nor 5”);
}
getch();
}

 

Program Output

We have executed this program on Turbo C Compiler and got the output as shown in following picture.

nested if-else statement in c

5 . Switch Case Statement

A switch case statement in C Programming tests the value of a choice variable and compares it with multiple cases. When the case match is found, a block of statements associated with that specific case is executed.

Each case in a block of a switch has a different choice which is referred to as an identifier. When the user enters a value, then the value provided by the user is compared with all the cases inside the switch block until the exact match is found.

If a case match is not found, then the default statement is executed, and the control goes out of the switch block.

Switch Case Statement Program

Program to demonstrate the use of switch statement is as follow

#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
printf( “Enter any valuen” );
scanf( “%d”, & choice );
switch ( choice ) {
case 1:
printf( “It is hot weather!n” );
break;
case 2:
printf( “It is a stormy weather!n” );
break;
case 3:
printf( “It is a sticky weather!n” );
break;
default:
printf( “It is a pleasant weather!n” );
break;
}
getch();
}

 

Program Output

We have executed this program on Turbo C Compiler and got the output as shown in following picture.

switch case statement in C

Key Point to Remember about Switch Case 

There is a certain rule for the use of a switch case statement that you to should remember at the time of writing the program using a switch case statement. These rules are given below

  • Case labels must have constants or constant expressions.
  • Case label must be of integral Type (Integer, Character).
  • Case label should not be ‘float type. ‘
  • Switch case should have at most one default label
  • The default label is Optional
  • The default can be placed anywhere in the switch
  • Break Statement takes control out of the switch
  • Two or more cases may share one break statement.

Conclusion and Summary

In this tutorial of Conditional Statements in C programming, we have explained different types of statements with the program and their output.

I hope this tutorial will be helpful for the computer science student in understanding the concept of conditional statements in C Programming.

Students can also study other c programming tutorials.

Don’t stop learning and practice.

Click Here  For- Complete Course of C Programing 

Next C Programming TutorialLooping Statement in C Programming

 

 

Leave a Reply

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