- 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 - if else statement in C
![]() Share with a Friend |
C Programming - if else statement in C
if-else Statement in C
The if-else statement in C allows you to execute one block of code if a condition is true and another block if the condition is false. It is used when you need to choose between two possible outcomes based on a condition.
Syntax of if-else Statement:
C
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
- condition: An expression that evaluates to true (non-zero) or false (zero).
- Code block: The statements inside {} that execute based on whether the condition is true or false.
Example 1: Basic if-else Statement
C
#include <stdio.h>
int main() {
int a = 10;
// Check if a is greater than 5
if (a > 5) {
printf("a is greater than 5\n");
} else {
printf("a is not greater than 5\n");
}
return 0;
}
Explanation:
- The condition a > 5 is true because a is 10.
- Since the condition is true, the first block of code is executed, printing "a is greater than 5".
Output:
a is greater than 5
Example 2: if-else with False Condition
C
#include <stdio.h>
int main() {
int a = 3;
// Check if a is greater than 5
if (a > 5) {
printf("a is greater than 5\n");
} else {
printf("a is not greater than 5\n");
}
return 0;
}
Explanation:
- The condition a > 5 is false because a is 3.
- Since the condition is false, the else block is executed, printing "a is not greater than 5".
Output:
a is not greater than 5
Example 3: Using if-else with Logical Operators
You can combine multiple conditions in an if-else statement using logical operators like && (AND) and || (OR).
C
#include <stdio.h>
int main() {
int a = 10, b = 5;
// Check if a is greater than 5 and b is less than 10
if (a > 5 && b < 10) {
printf("a is greater than 5 and b is less than 10\n");
} else {
printf("Either a is not greater than 5, or b is not less than 10\n");
}
return 0;
}
Explanation:
- The condition a > 5 && b < 10 is true because both conditions are true (a is 10 and b is 5).
- The first block of code is executed, printing "a is greater than 5 and b is less than 10".
Output:
a is greater than 5 and b is less than 10
Example 4: if-else for Multiple Conditions (Nested if-else)
In some cases, you may need to check multiple conditions. You can nest if-else statements to handle this.
C
#include <stdio.h>
int main() {
int a = 15;
if (a > 20) {
printf("a is greater than 20\n");
} else {
if (a > 10) {
printf("a is greater than 10 but less than or equal to 20\n");
} else {
printf("a is less than or equal to 10\n");
}
}
return 0;
}
Explanation:
- The first condition a > 20 is false, so the program checks the second condition a > 10.
- Since a is 15, the second condition is true, and "a is greater than 10 but less than or equal to 20" is printed.
Output:
a is greater than 10 but less than or equal to 20
Example 5: if-else Without {} (Single Statement Block)
If there is only one statement in the if or else block, you can omit the curly braces {}. However, using {} is recommended for clarity and to avoid errors.
C
#include <stdio.h>
int main() {
int a = 3;
// Check if a is greater than 5 (without using braces)
if (a > 5)
printf("a is greater than 5\n");
else
printf("a is not greater than 5\n");
return 0;
}
Explanation:
- Since there is only one statement in both the if and else blocks, the curly braces are optional.
- The condition a > 5 is false, so the else block is executed, printing "a is not greater than 5".
Output:
a is not greater than 5
Important Points:
- Condition: The condition inside the if statement should evaluate to either true (non-zero) or false (zero).
- Single Statement Blocks: If there is only one statement in the if or else block, you can omit the curly braces {}, but it's better to use them for readability and to avoid errors.
- Nesting: You can nest if-else statements to handle more complex decision-making.
Summary of if-else Statement:
- The if-else statement is used when you need to execute one block of code if a condition is true and another block if it is false.
- It is often used for binary decision-making and can be combined with logical operators to handle more complex conditions.
