- 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 statement in C
![]() Share with a Friend |
C Programming - if statement in C
if Statement in C
The if statement in C is used to evaluate a condition and execute a block of code if the condition is true. It is one of the most basic control flow statements in programming. If the condition evaluates to true, the code inside the if block will run. If it evaluates to false, the code inside the if block will be skipped.
Syntax of if Statement:
C
if (condition) {
// Code to be executed if the condition is true
}
- condition: An expression that evaluates to true (non-zero) or false (zero).
- Code block: The statements inside {} that execute when the condition is true.
Example 1: Basic if 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");
}
return 0;
}
Explanation:
- The condition a > 5 is true because a is 10.
- Therefore, the statement printf("a is greater than 5\n"); is executed.
Output:
a is greater than 5
Example 2: if Statement with a 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 program jumps to the else block, which prints "a is not greater than 5".
Output:
a is not greater than 5
Example 3: if with Multiple Conditions (Logical Operators)
You can combine multiple conditions 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");
}
return 0;
}
Explanation:
- The condition a > 5 && b < 10 is true because both conditions are true (a is 10 and b is 5).
- Thus, the statement printf("a is greater than 5 and b is less than 10\n"); is executed.
Output:
a is greater than 5 and b is less than 10
Example 4: if Statement Without {} (Single Statement Block)
If there is only one statement inside the if block, curly braces {} can be omitted, but it's good practice to use them for clarity.
C
#include <stdio.h>
int main() {
int a = 10;
// Check if a is greater than 5 (without using braces)
if (a > 5)
printf("a is greater than 5\n");
return 0;
}
Explanation:
- Since there is only one statement in the if block, the curly braces are optional.
- The condition a > 5 is true, so the message "a is greater than 5" is printed.
Output:
a is greater than 5
Important Points to Remember:
- Condition: The condition inside the if statement must evaluate to a boolean value (true or false). In C, any non-zero value is considered true, and 0 is considered false.
- Scope: If you omit the {} around the block of code, only the first statement after the if condition will be considered part of the if block.
- Nesting: You can nest if statements inside each other to handle more complex decision-making.
Summary of if Statement:
- The if statement is used to check if a condition is true, and execute a block of code accordingly.
- If the condition is true, the block of code inside the if statement is executed.
- If the condition is false, the program skips the block and continues executing the remaining code.
