- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - nested if statement in C
![]() Share with a Friend |
C Programming - nested if statement in C
Nested if Statement in C
A nested if statement in C is an if statement that appears inside another if or else block. It allows you to test multiple conditions in a hierarchical manner, where one condition is checked only if the outer condition is true (or false, depending on the logic).
Syntax of Nested if Statement:
C
if (condition1) {
// Code to be executed if condition1 is true
if (condition2) {
// Code to be executed if condition1 and condition2 are true
} else {
// Code to be executed if condition1 is true and condition2 is false
}
} else {
// Code to be executed if condition1 is false
if (condition3) {
// Code to be executed if condition1 is false and condition3 is true
}
}
- The outer if condition is checked first.
- If the outer condition is true, the inner if or else block will be checked.
- Nested if statements allow more complex decision-making.
Example 1: Basic Nested if Statement
C
#include <stdio.h>
int main() {
int a = 10, b = 5;
if (a > 5) { // Outer if
if (b < 10) { // Inner if
printf("a is greater than 5 and b is less than 10\n");
} else {
printf("a is greater than 5 but b is not less than 10\n");
}
} else {
printf("a is not greater than 5\n");
}
return 0;
}
Explanation:
- The outer if checks if a > 5. Since a is 10, it is true.
- Inside the outer if, there is another if that checks if b < 10. Since b is 5, it is true.
- Therefore, the message "a is greater than 5 and b is less than 10" is printed.
Output:
a is greater than 5 and b is less than 10
Example 2: Nested if-else Statement
C
#include <stdio.h>
int main() {
int a = 15, b = 10, c = 20;
if (a > 10) { // Outer if
if (b > 5) { // Inner if
printf("Both a is greater than 10 and b is greater than 5\n");
} else {
printf("a is greater than 10 but b is not greater than 5\n");
}
} else {
if (c < 25) {
printf("a is not greater than 10, but c is less than 25\n");
}
}
return 0;
}
Explanation:
- The outer if checks if a > 10. Since a is 15, the condition is true.
- Inside the outer if, it checks if b > 5. Since b is 10, it is true.
- Therefore, "Both a is greater than 10 and b is greater than 5" is printed.
Output:
Both a is greater than 10 and b is greater than 5
Example 3: Nested if Statement with Multiple Conditions
C
#include <stdio.h>
int main() {
int a = 20, b = 15, c = 10;
if (a > b) { // Outer if
if (b > c) { // Inner if
printf("a is greater than b, and b is greater than c\n");
} else {
printf("a is greater than b, but b is not greater than c\n");
}
} else {
printf("a is not greater than b\n");
}
return 0;
}
Explanation:
- The outer if checks if a > b. Since a is 20 and b is 15, the condition is true.
- Inside the outer if, it checks if b > c. Since b is 15 and c is 10, the condition is also true.
- Thus, "a is greater than b, and b is greater than c" is printed.
Output:
a is greater than b, and b is greater than c
Example 4: Nested if-else with Multiple Levels
C
#include <stdio.h>
int main() {
int a = 30, b = 25, c = 10;
if (a > b) { // Outer if
if (b > c) { // Inner if
if (c < 15) {
printf("a is greater than b, b is greater than c, and c is less than 15\n");
} else {
printf("a is greater than b, b is greater than c, and c is not less than 15\n");
}
} else {
printf("a is greater than b, but b is not greater than c\n");
}
} else {
printf("a is not greater than b\n");
}
return 0;
}
Explanation:
- The outer if checks if a > b. Since a is 30 and b is 25, the condition is true.
- The inner if checks if b > c. Since b is 25 and c is 10, it is true.
- The innermost if checks if c < 15. Since c is 10, it is true, so the message "a is greater than b, b is greater than c, and c is less than 15" is printed.
Output:
a is greater than b, b is greater than c, and c is less than 15
Important Points to Remember:
- Nesting Depth: if statements can be nested to any depth, allowing you to handle complex decision-making scenarios.
- Logical Flow: A nested if block allows you to check further conditions only after a previous condition is true or false.
- Clarity: While nested if statements are useful, they can become difficult to read if overused. It's best to structure code for readability.
Summary of Nested if Statements:
- Nested if statements are if statements inside other if or else blocks.
- They allow you to handle complex decision-making by testing multiple conditions in sequence.
- It's essential to ensure the logical flow is clear and that the conditions are well-defined to avoid confusion.
