C if-else Statement

 

What is the if-else Statement?

The if-else statement is a conditional statement in C that allows you to execute different blocks of code based on a condition. It is used to control the flow of your program, making decisions based on whether a given condition evaluates to true or false.

C if-else Statement



The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.

There are the following variants of if statement in C language.


1. If statement

2. If-else statement

3. If else-if ladder

4. Nested if

If Statement

The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below.

Flowchart of if statement in C

if Statement


Syntax of the if-else Statement

The basic syntax of the if-else statement in C is as follows:
c
if (condition) { // Code to execute if the condition is true } else { // Code to execute if the condition is false }

1. condition: An expression that evaluates to either true or false.

2. The code block inside the first set of curly braces {} is executed if the condition is true.

3. The code block inside the second set of curly braces {} is executed if the condition is false.

if-else Statements in C

if-else Statement


Let's explore some examples to see how if-else statements are used in C.


Checking if a Number is Positive or Negative


c
#include <stdio.h> int main() { int num; // Prompt the user for input printf("Enter a number: "); scanf("%d", &num); // Check if the number is positive or negative if (num > 0) { printf("The number is positive.\n"); } else { printf("The number is non-positive (zero or negative).\n"); } return 0; }
In this example, the program takes an integer input from the user and uses an if-else statement to check whether the number is positive or non-positive (zero or negative).

 Determining Eligibility for Voting

c
#include <stdio.h> int main() { int age; // Prompt the user for age printf("Enter your age: "); scanf("%d", &age); // Check if the person is eligible to vote if (age >= 18) { printf("You are eligible to vote.\n"); } else { printf("You are not eligible to vote yet.\n"); } return 0; }
In this example, the program asks the user for their age and uses an if-else statement to determine whether the person is eligible to vote based on the age condition.

Finding the Largest of Three Numbers

c
#include <stdio.h> int main() { int num1, num2, num3; // Prompt the user for three numbers printf("Enter three numbers: "); scanf("%d %d %d", &num1, &num2, &num3); // Find the largest among the three numbers if (num1 >= num2 && num1 >= num3) { printf("%d is the largest number.\n", num1); } else if (num2 >= num1 && num2 >= num3) { printf("%d is the largest number.\n", num2); } else { printf("%d is the largest number.\n", num3); } return 0; }
In this example, the program takes three numbers as input and uses multiple if-else statements to determine and display the largest of the three numbers.

 Grade Calculation

c
#include <stdio.h> int main() { int score; // Prompt the user for a test score printf("Enter your test score: "); scanf("%d", &score); // Determine the grade based on the score if (score >= 90) { printf("Grade: A\n"); } else if (score >= 80) { printf("Grade: B\n"); } else if (score >= 70) { printf("Grade: C\n"); } else if (score >= 60) { printf("Grade: D\n"); } else { printf("Grade: F (Fail)\n"); } return 0; }
In this example, the program takes a test score as input and uses a series of if-else statements to determine and display the corresponding grade.

 Nesting if-else Statements

c
#include <stdio.h> int main() { int num; // Prompt the user for a number printf("Enter a number: "); scanf("%d", &num); // Check if the number is positive, negative, or zero if (num > 0) { printf("The number is positive.\n"); } else if (num < 0) { printf("The number is negative.\n"); } else { printf("The number is zero.\n"); } return 0; }
In this example, the program takes a number as input and uses nested if-else statements to check whether it is positive, negative, or zero.

Conclusion

The if-else statement is a fundamental control structure in the C programming language that allows you to make decisions in your code based on conditions. By using if-else statements, you can create more dynamic and responsive programs that respond to different situations.

In this article, we've covered the syntax of the if-else statement and provided several examples of its usage in C. These examples demonstrate how if-else statements can be used to perform tasks such as checking conditions, making decisions, and controlling program flow. As you continue to learn and work with C, mastering the if-else statement will be essential for writing efficient and robust programs.

Post a Comment

0 Comments