Skip to main content

Conditional Statements In C Programming

Conditional Statements:

What are conditional Statements:

Ans.
Conditional statements are those statements which are used to check whether a particular condition is true or t is false and if it is true the task given to it gets executed.

Types of conditional statements:

There 3 types of conditional statements

  • if statement
  • else if statement
  • else statement

They all work together to give the desired output.

Syntax:

if(condition here)
{code to be written here;}

Note:

  • The condition which needs to be checked should be written inside the round brackets.
  • Code which needs to be executed if the condition is true is written inside curly braces.
  • Semicolon will come only inside curly braces and not after condition.

1. if statement:

It is the main statement and it is a standalone statement i.e it doesn't require any other conditional statement for its existence.

Syntax:

if(condition)

{Code to be written here;}

If the condition in the above code is true the code within the curly braces get executed.

Example Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=12,b=6;
clrscr();
if(a>b)
{
printf("\n a is greater than b");
}
getch();
}

Output:

a is greater than b

Explanation of the above code:

In the above code we have declared two variables a and b and assigned the value 12 and 6 to a and b respectively and the condition that is a>b is true as 12 is greater  than 6 therefore the code gets executed and it prints the statement which we have written in the printf statement here in this case "a is greater than b".
If on the contrary the condition is reversed then there will be no output.

2. else statement:

This statement does not require any condition and is used in combination with the if statement .
It executes the code  written in it when the condition for the if statement is false.

Syntax:

if(condition 1)
{Code to be written here for condition 1;}
else
{Code if the condition fails;}

Example code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=6,b=12;
clrscr();
if(a>b)
{
printf("\n a is greater than b");
}
else
{
printf("\n a is less than b");

}
getch();
}

Output:

a is less than b

Explanation of the above code:

In the above code we have given the values 6 and 12 to a and b respectively since the first condition fails as a is not greater than b the code written inside the else statement gets executed.

AND Operator (&&):

If multiple conditions needs to be checked simultaneously and all the conditions needs to be true then we use the AND operator.

Syntax:

if(condition 1 && condition 2)
{code if both conditions are true;}
else
{code if they are false;}

Example Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n enter the number");
scanf("%d",&a);
if(a>0 && a<10)
{
printf("\n the number is between 0 to 10");
}
else
{
printf("\n the number does not satisfies the condition");

}
getch();
}

Output:

enter the number 5
the number is between 0 to 10

Explanation of the above code:

The above code is designed to check whether the number is between 0 to 10. As the input given is 5 both conditions of the if statement are satisfied hence the code written inside the if statement gets executed.

OR Operator (||):

In cases where anyone of the multiple condition needs to be true the OR operator comes into role. It is also used in the same way as AND operator.

Syntax:

if(condition 1 || condition 2)
{code executed if anyone of the two condition is true;}
else
{code executed if both conditions is false;} 

Example Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n enter the number");
scanf("%d",&a);
if(a<0 || a>=100)
{
printf("\n condition satisfied");
}
else
{
printf("\n condition failed");

}
getch();
}

Output:

enter the number 5
condition failed

Explanation of the above code:

The above code is designed to check whether the number entered is negative or its is greater than or equal to 100 since according to input 5 here the condition is not satisfied as the number isn't negative nor it is greater than or equal to 100 hence the code written inside the else statement gets executed.

3. else if statement:

It is also used in combination with the if statement. Imagine a case where multiple conditions needs to be checked example in grading system multiple grades are awarded according to the marks obtained by the students this is where else if statement comes into role

Syntax:

if(condition 1)
{code here as per condition 1;}
else if(condition 2)
{code here as per condition 2;}
else if(condition 3)
{code here as per condition 3;}
else 
{code here if all conditions fails;}

Note:

There is a space between else and if of else if statement.

Example Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int m;
clrscr();
printf("\n enter the marks of student");
scanf("%d",&m);
if(m<36)
{
printf("\n failed");
}
else if(m>=36 && m<50)
{
printf("\n Grade D");

}
else if(m>=50 && m<60)
{
printf("\n Grade C");
}
else if(m>=60 && m<75)
{
printf("\n Grade B");
}
else
{
printf("\n Grade A");
}
getch();
}

Output:

enter the marks of student 78
Grade A

Explanation of the above code:

In the above code we have created a grading system it checks all the condition according to the marks entered and then gives the desired grade to the student as we have given input 78 as is does not satisfy any condition the code in the else statement gets executed similarly if the input was 60 then the grade would be  B according to condition.

I hope you understood the topic conditional statements.
If you don't know the basics of C programming you can checkout my blog 
https://www.codingninjaa.com/2020/05/C-programming-basics.html?m=1
If you want to learn further topics of conditional statements click on the link given below
https://www.codingninjaa.com/2020/05/nested-if-statements-in-c-programming.html


If you have any doubts please comment down below.

Comments

Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS:
1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases.
1.1 Header Files:

Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h".
Example: #include<stdio.h> ↓                   ⇡
         Tells computer to                  header file with 
         include the header                 extension .h
         file          
There are many header files for different functions such as the following: <conio.h><math.h><string.h>Etc.

1.2 Starting of code and basic Syntax:

void main() or int main()

This tells the computer from where the code is starting. It is wri…

How to start programming for beginners

HOW TO START PROGRAMMING:
1. What is Programming?
Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs.
Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program.Programming can also be used to build a software to determine whether a particular number is prime or not.2. Can anyone learn Programming ?The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming. The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming?If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now.The other important Factor is Money if you become a master in programming then there is no limit on how…

Basic C Programming Questions

Basic Coding Questions:Exercise 1:Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth.
Ans:
#include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); }

The above image code is written in code blocks
Output:
enter the length and breadth of rectangle 20(press enter) 10(press enter) the area of rectangle is 200 and the perimeter is 60


Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers.Taking input of length and breadth together by using scanf function.Substituting the values of length and breadth stored in l and b to find out area and perimeter.Displaying the result.
Q2. Write a programme to calculate average of 5 numbers by taking input from user.
Ans: