Table of Contents

What is if-else statement in C?
The if-else statement is a statement used in C programming for making decisions. It is a core concept of C programming and builds on the basic if statement by adding an else block to it.
The if-else statement is a construct in C programming that helps make decisions about which part of the code to execute based on a test expression. If the test expression is true, the code within the if block is executed, but if it is false, the code within the else block is executed instead.
Syntax of if-else Statement in C Programming
if (test_expression) { // Code to be executed if the test expression is true } else { // Code to be executed if the test expression is false }
Example Of if else Statement in C Programming
#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); if (number % 2 == 0) { printf("%d is even.\n", number); } else { printf("%d is odd.\n", number); } return 0; }
This C program checks if a number entered by the user is odd or even. It starts by including the standard input-output library, “stdio.h”.
The main function initializes an integer variable called “number” and prompts the user to input a number using the printf function. The entered number is then stored in the “number” variable using the scanf function.
Then, an if-else statement is used to determine whether the entered number is even or odd. If the number is even (which means that it is divisible by 2 without any remainder), the program prints a message stating that the number is even. If the number is odd (which means that it is not divisible by 2 without any remainder), the program prints a message indicating that it is odd.
Finally, the program returns a value of 0 to indicate that it has finished successfully.
Nested If-Else Statements in C Programming: Explained with Examples
Nested if-else is a programming construct in which an if-else statement is placed inside another if-else statement. It is used to test multiple conditions in a C program. When a condition in the outer if statement is true, the program checks the inner if statement to see if that condition is true as well. If both conditions are true, the program executes the code inside the nested if block. If either condition is false, the program moves to the next else block or statement. Nested if-else statements can be used to build complex decision-making logic in a program.
Syntax of nested if-else statement in C?
if (condition1) { // code to execute if condition1 is true if (condition2) { // code to execute if both condition1 and condition2 are true } else { // code to execute if condition1 is true but condition2 is false } } else { // code to execute if condition1 is false }
Note that the if statement can contain another if-else statement inside it, forming a nested structure. The inner if-else statement can also contain another if-else statement, allowing for further nesting. by using this syntax we can understand the structure of the nested if else of the c programming
Example of Nested If-Else Statements in C Programming:
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); if (age >= 18) { printf("You are an adult.\n"); if (age >= 65) { printf("You are a senior citizen.\n"); } else { printf("You are not a senior citizen.\n"); } } else { printf("You are not an adult.\n"); } return 0; }
This code prompts the user to enter their age and stores it in the age variable using the scanf function. It then uses a nested if-else statement to determine whether the person is an adult or not. If the age is 18 or older, the program prints a message indicating that the person is an adult.
If the person is an adult, there is another if-else statement nested within the first one to check if the person is also a senior citizen (age 65 or older). If the age is 65 or older, the program prints a message indicating that the person is a senior citizen. If the age is less than 65, the program prints a different message.
If the person is not an adult (i.e. they’re under 18), the program prints a different message indicating that they’re not an adult. This is an example of how nested if-else statements can be used to perform multiple conditional checks within a single block of code.
Recent Posts
- How to Get Started with Deepseek: A Step-by-Step Guide for Beginners
- The Future of Deepseek: Trends and Predictions for 2024 and Beyond
- Top 10 Use Cases of Deepseek for Businesses and Individuals
- Deepseek vs. Competitors: Why It Stands Out in the Market in 2025
- How Deepseek is Revolutionizing [Industry/Field]: Key Insights in 2025
Recent Posts
- How to Get Started with Deepseek: A Step-by-Step Guide for Beginners
- 1The Future of Deepseek: Trends and Predictions for 2024 and Beyond
- 2Top 10 Use Cases of Deepseek for Businesses and Individuals
- 3Deepseek vs. Competitors: Why It Stands Out in the Market in 2025
- 4How Deepseek is Revolutionizing [Industry/Field]: Key Insights in 2025
- 5What is Deepseek? A Comprehensive Guide to Its Features and Benefits 2025