0 0

What is Conditional Statement in C programming

- Advertisement -
Read Time:3 Minute, 27 Second
What is Conditional Statement in C programming
What is Conditional Statement in C programming

What is Conditional Statement in C programming

Conditional Statement

Conditional Statements in C programming are used to make decisions based on the different conditions. Conditional statements execute sequentially when there is no condition around the statements. If you put some condition for a block of statements, the execution flow may change based on the result evaluated by the condition. This process is called decision making in C programming.

There are the following types of conditional operators in c:

  1. If
  2. If else
  3. else if
  4. Switch statement

If Statement 

The single if statement in C language is used to execute the code if a condition is true otherwise it show nothing. It is also called a one-way selection statement. When we use the if condition, we pass the argument and if the argument will be satisfied then the respective code will be executed otherwise nothing can happen.

Below is the if statement followed by the conditional expression.

Syntax

if (expression)
{
//Code to be executed
}

Example

#include<stdio.h>
int main(){
int age;

printf("Enter age:");
scanf("%d", &age);

if(age >=18){
printf("Adult");
}
}

If else Statement

The if-else statement in C programming language is used to execute the code if the condition is true or false. It is also called a two-way selection statement.

The single if statement may work pretty well, but if you want to work with multiple variables or the extended conditional parameters, then the if-else statement is the optimum choice. By using the if statement, only one block of the code executes after the condition is true but by using the if-else statement, there are two possible blocks of code where the first block is used for handling the success part and the other one for the failure condition.

Syntax

if(expression)
{
 //Statements
}
else
{
 //Statements
}

Example

#include<stdio.h>
int main(){
int age;
printf("Enter age:");
scanf("%d", &age);

if(age >=18){
printf("Adult");
}

else{
 printf("Not Adult");
}
}

Else if Statement

The Else-if statement is used to execute one code from multiple conditions. It is also called a multipath decision statement. It is a chain of if.else statements in which each if statement is associated with an else if statement and the last would be an else statement.

Syntax

if(condition1)
{
 //statements
} 
else if(condition2)
{
 //statements
}
else if(condition3)
{
 //statements
}
else
{
 //statements
}

 

Example

#include<stdio.h>
int main(){
int age;

printf ("Enter age:");
scanf("%d", &age);

if (age < 12){
printf("Child");
}

else if (age < 19){
printf("teenager");
}

else if (age < 60){
printf("Adult");
}
else {
printf("Old");
}
}

Switch Statement

switch statement acts as a substitute for a long if-else-if ladder that is used to test a list of cases. A switch statement contains one or more case labels that are tested against the switch expression. When the expression match to a case then the associated statements with that case would be executed.

We have seen the way of using conditional statements such as if, if-else. if-else ladder, but the need for an additional way of dealing with conditional statements may seem unnecessary but based on the certain usage, switch case was defined to check for the single condition, and based on the multiple cases, code can be executed.

Syntax

Switch (expression)
{
 case value1:
 //Statements 
 break;
 case value 2:
 //Statements
 break; 
 case value 3:
 //Statements 
 case value n:
 //Statements
 break;
 Default:
 //Statements
}

Example

#include<stdio.h>
int main(){
int day;

printf("Enter day:");
scanf("%d", &day);

 switch (day){

    case 1 : printf("Monday");

    break;

    case 2 : printf("Tuesday");

    break;

    case 3 : printf("Wednesday");

    break;

    case 4 : printf("Thrusday");

    break;

    case 5 : printf("Friday");

    break;

    case 6 : printf("Saturday");

    break;

    case 7 : printf("Sunday");

    break;

    default : printf("Not a valid day");

 }
 return 0;
}

BASIC C PROGRAMMING CONCEPT AND PROGRAMS: HELLO WORLD, ADDING TWO INTEGERS, AREA OF SQUARE AND CIRCLE USING VS CODE

 

HOW TO FIX INDEXED PROBLEM IN GOOGLE SEARCH CONSOLE: COVERAGE ISSUE, SITEMAP PROBLEM

Happy
Happy
100 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
- Advertisement -

Follow US!

FansLike
FollowersFollow
FollowersFollow
SubscribersSubscribe

Latest Post

Introduction to Online Analytical Processing and Data Mart

Introduction to Online Analytical Processing and Data Mart

Introduction to Online Analytical Processing and Data Mart Online Analytical Processing OLAP is computer processing that enables a user to easily and selectively extract and view...
A data warehouse is a relational database that is designed for query and analysis rather than for transaction processing

Introduction to Data Warehouse

Introduction to Data Warehouse A data warehouse is a relational database that is designed for query and analysis rather than for transaction processing. It usually...
Introduction to Advance Database System

Introduction to Advance Database System

Introduction to Advance Database System ABSTRACT Data is collection of raw facts and figure which is basic for decision making. Anything which is unorganized is called...
What is Ternary Operator in C programming with example

What is Ternary Operator in C programming with example

What is Ternary Operator in C programming with example Ternary Operator In the C language ternary operator is allowing for executing or running any code based...
C programming if else statement

C programming if else statement

C programming if else statement Conditional Statements in C programming are used to make decisions based on the conditions. Conditional statements execute sequentially when there...
Types of instruction in C programming

Types of instructions in C programming

Types of instructions in C programming: C Instructions C instructions are the commands in the program that instructs the compiler to do certain action. Basically it...