Logical Operators in C

The nested if-else statement helps in making multiple selections within a program. however there are some disadvantages of victimisation them that are given below.

Indentation is most vital whereas victimisation nested if-else statements. however if we tend to add a pair of layer nesting within on if-else block.

Then you’ll notice that your program is locomotion towards the right. And it’ll build tough to browse that program.

Braces are wont to show the scope of statements. There are probabilities that you simply could commit a slip whereas writing those braces.

One single mistake can fill your program with tons of errors.

As there are multiple if-else statements so you furthermore might got to beware of matching the proper else block with if statement.

So these are some serious issues whereas writing nested if-else statements. however wait! it’s not mandatory to use nested if-else statements anytime.

we are able to accomplish identical task by victimisation Logical operators.

What are the logical operators?

Basically C uses 3 logical operators that are AND (&&), OR (||) and NOT (!). I hope you want to have learnt concerning formal logic.

These 3 operators are employed in that.

Remember whereas writing AND and OR operator, it ought to be written with 2 symbols that are && for AND and || for OR. A single image has fully totally different which means in C programming.

So now let’s take one example to grasp the utilization of logical operators.

Question: build a program to print the division of student. Take marks from the user. Calculate the share and calculate their division.
Explanation.

#include<stdio.h>
 
void main( ) 
{ 
 int  n1,n2,n3,n4,n5,perc;
 printf("Enter marks of student in five subjectsn"); 
 scanf("%d %d %d %d %d",&n1,&n2,&n3,&n4,&n5);
 perc=(n1+n2+n3+n4+n5)/5;
 
 if(perc>=60)
  printf ("You have got First division");  
 
 if((perc >=50)&&(perc<60))   
  printf ("You have got Second division");  
 
 if((perc>= 40)&&(perc<50))   
  printf("You have got Third division");  
 
 if(perc<40)   
  printf("Sorry you are Fail"); 
}

Output

Enter marks of student in five subjects
50 46 64 62 55
You have got Second division 

In the initial steps, it’s asked to enter student’s marks. subsequently I even have calculated the share of the scholar supported the marks.

currently I even have to see the division of student.

Now within the 1st if statement I even have given the condition if (perc>=60) it’ll check if the share is larger than sixty then it will print the message “You have gotten first division”.

Now within the next if statement I even have written the condition if ((perc>=50) && (perc

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

14 + 4 =

Recent Content