And Operator in C

Introduction: And Operator in C Programming

Best Explanation of AND Operator in C Programming Language. With Student Grading Program

An expression containing a logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

The ‘&&’ operator returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).

To know more about C Programming and it’s history please read this blog first “C Programming Complete Guide 2020

You can watch the video over here which explains the blog in simple video with VFX animation.

Declaring Variables in C Program

We will declare a float variable as “percentage” to take input from the user.

main()
{
	float percentage;
}

We have printf statements, followed by the scanf statement to accept user input.

main()
{
	float percentage;	
	
	scanf("%f",percentage);
}

To make it more friendly we will add a few statements using printf.

main()
{
	float percentage;
	
	printf("Calculate Division by Percentage");
	printf("\nPlease Enter Percentage: ");
	scanf("%f",&percentage);

}


Now your program will be more user-friendly to understand the user.

Business Logic for Student Grading Program

Let’s implement the Business Logic for Student Grading Program in C language.

if percentage >= 60, its first Division.

main()
{
	float percentage;	

	printf("Calculate Division by Score");
	printf("\nPlease Enter Percentage: ");
	scanf("%f",&percentage);

	if(percentage >= 60)
		printf("First Division ");
}

Here we have used the “AND” operator. And this operator requires both the conditions to be true.

Please make sure that all the statements are followed by a semicolon.

Next line we will write the condition for the 3rd division. i.e. percentage greater than 40 and less than 50.

 main()
{
	float percentage;	

	printf("Calculate Division by Score");
	printf("\nPlease Enter Percentage: ");
	scanf("%f",&percentage);

	if(percentage >= 60)
		printf("First Division ");
	if(percentage >= 50 && percentage <60 )
		printf("Second Division ");
	if(percentage >= 40 && percentage <50 )
		printf("Third Division ");

}

Now we have the final condition, i.e less than 40. Its failed.

main()
{
	float percentage;
	
	printf("Calculate Division by Score");
	printf("\nPlease Enter Percentage: ");
	scanf("%f",&percentage);

	if(percentage >= 60)
		printf("First Division ");
	if(percentage >= 50 && percentage <60 )
		printf("Second Division ");
	if(percentage >= 40 && percentage <50 )
		printf("Third Division ");
	if(percentage < 40)
		printf("FAILED");

}

Let’s execute the program

Output :

Let’s see the output.

Calculate Division by Score
Please Enter Percentage: 55
Second Division 

Well, Let’s iterate through the program.  You entered the value 55 and the Computer reaches the first condition. It checks whether the percentage is greater than 60 or not.

The answer is negative, hence this statement of printf is not executed.

In the next line, we will check two conditions. First, it’s greater than 50, yes that’s true. Next is less than 60, it’s also true. When we put “AND” to join the conditions, the result of both the conditions should be true, then only it is considered as true

Well, the statement “Second Division” is printed. But the program won’t stop here. Since there are few more lines yet to be executed, It will go to the next line.

Here it will check a percentage greater than 50, the answer is true, the Next condition in the

same “IF” statement is a percentage less than 50. The result is false. So in this, the “IF” statement we have one condition as true and one condition is false. So the final result is false since we have  AND operator.

I hope you understand this part. If we have both conditions true in “IF” ‘s  && statement, then its completely considered as true.

Well, the program doesn’t stop here, it goes to the next line again. Here the condition is a percentage of less than 40. The answer is false.

Now it’s the last line, end of the function.

Well, this is how we handle multiple conditions in “if and else” using operators. There is more to be learned about IF and ELSE statements.

How to view all topics on C Programming

Watch full programming series on Amazon Prime free with Amazon Prime Membership.

Amazon Prime link for the USA: https://www.amazon.com/dp/B07WFYJW6G

Amazon Prime link for the UK: https://www.amazon.co.uk/dp/B07WFYQZ1G


For Other Countries: To watch C Programming Content in another country, please visit the URL:https://newtum.com/course-details/c-programming-online

Frequently Asked question about And Operator in C Programming

Types of operators in C Programming

six types of operators namely
1.Greater than — x > y
2.Greater than equal to — x>= y
3.Less than — x < y
4.Less than equal to — x<= y
5.Equal to — x==y
6.Not equal to — x!=y
If and else can have multiple conditions using ANDOR and Not Operators.
We can use else if statements to avoid unwanted execution. Well, that’s about IF and ELSE. We will study more about it in the coming chapters. Practice well.

Facebook Comments
(Visited 43 times, 1 visits today)

Leave A Comment

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