×

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

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 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.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute 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.