×

C Decision control statement

Decision Making

C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the condition becomes false. C language supports the following statement:
     
  • If statement
  •  
  • Switch Statement
  •  
  • Conditional operator statement
  •  
  • goto statement
If statement
If statement is used to perform the operation according to condition. In other words, we can say that it controls the flow of execution of the statement. The condition may be true or false. There are various ways to use if statement:
     
  • If statement
  •  
  • If-else statement
  •  
  • Else if Ladder
If Statement
In C language, if statement is used to execute the code if the condition is true. The following syntax of if statement is:
Syntax:
if(expression/ condition)
{
//code to be executed
}
Example
#include <stdio.h>
int main()
{
int n=0;
printf("---- To Chcek The Number is Even or Odd ---- \n");
printf("Enter any number : ");
scanf("%d",&n);
if(n%2==0){
printf("Even Number");
}
return 0;
}
Output
---- To Chcek The Number is Even or Odd ----
Enter any number : Even Number

If- else statement:

If-else statement is used to execute the code if the condition is true or false. The given following

 

Syntax of if-else statement:

if(expression)
{
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
Example
#include <stdio.h>
int main()
{
int n=0;
printf("---- To Chcek The Number is Even or Odd ---- \n");
printf("Enter any number : ");
scanf("%d",&n);
if(n%2==0){
printf("Even Number");
}
else{
printf("Odd Number");
}
return 0;
}
Output
---- To Chcek The Number is Even or Odd ----
Enter any number : Even Number

If else-if ladder statement

It is used to execute one code from multiple conditions.

Syntax:

if(expression1)
{
statement block1;
}
else if(expression2)


{
statement block2;
}
else if(expression3 )
{
statement block3;
}
Else
default statement;

Switch Statement

The switch statement is used to execute the code from multiple condition or case. Following is the flow chart of the switch statement.

Syntax:

switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}

Rules for switch statement

The switch(expression) must be an integer or character type.

     
  • We can use only byte, short, int, char data type with a switch statement.
  •  
  • With switch statement not use float data type.
  •  
  • We can use any number of case statements within a switch.
  •  
  • The case value can be used only inside the switch statement.
  •  
  • Value for a case must be the same as the variable in a switch.
Let us consider an example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch;
printf("Enter any Number: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Today is Sunday");
break;
case 2:
printf("Today is Monday");
break;
case 3:
printf("Today is Tuesday");
break;
case 4:
printf("Today is Wednesday");
break;
case 5:
printf("Today is Thursday");
break;
case 6:
printf("Today is Friday");
break;
case 7:
printf("Today is saturday");
break;
default:
printf("Please Enter Value between 1 to 7");
return 0;
}
}
Output
Enter any Number: Please Enter Value between 1 to 7
Example
#include <stdio.h>
int main()
{
int day;
printf("Enter any Week day Number: ");
scanf("%d",&day);
if(day==1){
printf("1st day Sunday");
}
else if(day==2){
printf("2nd Day Monday");
}
else if(day==3){
printf("3rd Day Tuesday");
}
else if(day==4){
printf("4th Day Wednersday ");
}
else if(day==5){
printf("5th Day Thursday");
}
else if(day==6){
printf("6th Day Friday");
}
else if(day==7){
printf("7th Day Saurday");
}
else{
printf("Not a Day of Week");
}
return 0;
}
Output
Enter any Week day Number: Not a Day of Week

Related Topics

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.