Conditional Statements in C Programming with Examples (if, if-else, switch)

0
Conditional Statements in C Programming with Examples (if, if-else, switch)
Conditional Statements in C Programming with Examples (if, if-else, switch)

📑 Table of Contents

Conditional Statements Definition in C

Conditional Statement in C: A conditional statement can be using decision making control system. It can be used condition are satisfied or not satisfied. If the given condition are true and given condition are false than return compiler are false. It is very most important in every computer programming languages because conditional statement are handle every situation of condition. Conditional statement are different name is called like Decision making, Condition based, and more.  I can explain every topic of conditional statement related simple and easy language. 

Read C++ Conditional Statement

Types of Decision Control Statements in C

  • If Statement
  • If-Else Statement
  • Else-If Statement
  • Nested If Statement
  • Switch Statement

If Statement in C Programming

If Statement: If the given condition is true than if body will be executed or given condition are false than no if body will be executed. In the case of place if in the place of condition always 0 and non 0 values is checked. Its means condition false and non 0 condition is true. It is called if conditional statement.

Syntax of If Statement in C

if(condition)
{
Statements;
}
 

Explain If Statement With Syntax

int age=18;
if(age>=18)
{
printf("Your age is 18 this statement are executed");
}

Example of If Statement in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int age;
printf("Enter Your Age:");
scanf("%d",&age);
if(age>=18)
{
printf("Your Age 18 You can derive a car");
}
getch();
}

============OUTPUT============
Enter Your Age:18
Your Age 18 You can derive a car

If Else Statement in C Programming

If Else Statement: A if-else statemen can be used two condition are applied if condition are true and if statement/ body will be executed or if condition is false than else statement/body will be executed. It can be most useful statement in every programming language.

Syntax of If Else Statement in C

if(condition)
{
Statements;
}
else
{
Statements;
}

Explain If Else Statement With Syntax

int age=18;
if(age>=17)
{
printf("Your age is 18 this statement are executed");
}
else
{
printf("Your age is less than 18 this statement are executed");
}

Example of If Else Statement in C Programming

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

if(age>=18)
{
printf("Your Age 18 You can derive a car");
}
else
{
printf("Your Age is less 18 you can not derived a car");
}
getch();
}

============OUTPUT============
Enter Your Age:17
Your Age is less 18 you can not derived a car

Else If Statement in C Programming

Else-If Statement: A else-if statement can be used where multiple condition are applied that use else if statement. It is a part of decision making  that execute only one condition at a time. If all condition is false than else part will be execute. If the condition are first condition are true than first statement are executed. if the first condition are false than checked second condition when second condition are true than second statement are executed. 

Syntax of Else If Statement in C

if(condition1)
{
Statements 1;
}
else if(condition2)
{
Statement 2;
}
else if(condition3)
{
Statement 3;
}
else
{
Else Statement
}

Explain Else If Statement With Syntax

int age=18;
if(age>=18)
{
printf("Your age is 18 this statement are executed");
}

Example of Else If Statement in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int per;
printf("Enter Your Percentage:");
scanf("%d",&per);
if(per<=100&&per>=60){
printf("You got First Division");
}
else if(per<=60 &&per>=50){
printf("You got Second Division");
}
else if(per<=50 &&per>=33){
printf("You got Third Division");
}
else{
printf("You are Fail...");
}

getch();
}
============OUTPUT============
Enter Your Percentage:61
You got First Division

Nested If Statement in C Programming

Nested If Statement: A nested statement can be used one inside if to another if that is called nested if statement. In the case if if in the place of condition always 0 and non 0 value is checked in which 0 means condition is false and non 0 means condition is true.

Syntax of Nested If Statement in C

if(condition1)
{
if(condition 2)
{
Statements;
}
}

Explain Nested If Statement With Syntax

int x,y;
if(x>=y)
{
    if(x<=y){
        printf("This statement Executed");
}
}

Example of Nested If Statement in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int x=10;
if(x>5){
     if(x<15){
printf("x is greater than 5 and less than 15");
}
}
getch();
}

============OUTPUT============
x is greater than 5 and less than 15

Switch Statement in C Programming

Switch Statement: A switch statement is allows us to execute one statement from many statement and that statements are called case. Switch statement inside the body of switch a number of cases are used and a parameter are passed. If parameter number is matched the any cases than statement are executed. If the parameter number are not matched than default case are executed. 

Syntax of Switch Statement in C

switch(condition1)
{
case 1:
        statement 1;
        break;
case 2:
        statement 2;
        break;
case 3:
        statement 3;
        break;
default:
       default Statement;
}

Explain Switch Statement With Syntax

int n=2;
switch(2)
{
case 1:
        printf("Case 1 executed");
        break;
case 2:
        printf("Case 2 executed");
        break;
case 3:
        printf("Case 3 executed");
        break;
default:
        printf("Case are not matched");
}

Example of Switch Statement in C

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int n;
printf("Enter any Number:");
scanf("%d",&n);
switch(n)
{
case 1:
        printf("Case 1 executed");
                break;
case 2:
        printf("Case 2 executed");
                break;
case 3:
        printf("Case 3 executed");
                break;
default:
        printf("Case are not Matched");
}
getch();
}

============OUTPUT============
Enter any Number:3
Case 3 executed

Conclusion of Conditional Statement in C Language

Conclusion: This is all about conditional statement it is very most important features of every programming languages. It can be used condition creaking true and false. I can explain every topic in this article if your any question or query you can contact me by email or comments. I can provide solution simple and easy way.   

Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top