For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
Debugging a C program without proper control flow is like tracing a straight wire through a maze. You know where it starts but not where it could have branched. Conditional statements in C help eliminate such guesswork. They guide the compiler to follow specific paths based on conditions, making logic visible and traceable during debugging. Without them, pinpointing logical flaws becomes tedious and inefficient.
Explore our Online Software Development Courses from top universities.
As code complexity grows, the ability to control program flow becomes critical. These condition-based statements not only simplify decision-making but also make the program more readable and maintainable. They help isolate problems early and assist in testing different logical branches efficiently. Whether you're dealing with menu options or validating user input, conditional statements in C make debugging manageable and the logic crystal clear.
Writing error-free code is only half the challenge when developing in C. The real skill lies in making decisions based on dynamic conditions. Conditional statements in C help your program execute specific blocks of code only when certain criteria are met. This is vital in building logic that adapts to user input, environmental changes, or runtime data.
Whether you're validating a password, comparing scores, or navigating menu options, these conditional paths control the program's direction. Without them, every program would run in a straight line—rigid and impractical for real-world tasks.
Boost your career journey with the following online courses:
Below are the core types of Conditional Statements in C. Each includes syntax, a practical example, the output, and an explanation.
The if statement checks whether a given condition is true. If it is, the code block inside the if statement runs.
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
#include <stdio.h>
int main() {
int marks = 75;
if (marks > 50) {
printf("Ram passed the exam.\n");
}
return 0;
}
Output:
Ram passed the exam.
Explanation: The condition marks > 50 evaluates to true, so the message is printed. If the condition were false, nothing would happen.
The if-else statement lets you handle both true and false outcomes of a condition.
Syntax:
if (condition) {
// code if true
} else {
// code if false
}
Example:
#include <stdio.h>
int main() {
int age = 16;
if (age >= 18) {
printf("Aniket is eligible to vote.\n");
} else {
printf("Aniket is not eligible to vote.\n");
}
return 0;
}
Output:
Aniket is not eligible to vote.
Explanation: Since age >= 18 is false, the program jumps to the else block.
You can nest one if-else inside another to check multiple conditions in a hierarchy.
Syntax:
if (condition1) {
if (condition2) {
// code if both are true
} else {
// code if only condition1 is true
}
} else {
// code if condition1 is false
}
Example:
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 60) {
if (marks >= 80) {
printf("Pooja got distinction.\n");
} else {
printf("Pooja passed with first class.\n");
}
} else {
printf("Pooja did not pass.\n");
}
return 0;
}
Output:
Pooja got distinction.
Explanation: Both conditions marks >= 60 and marks >= 80 are true, so the program executes the nested block.
Learn how nested if-else statements enhance the control flow by exploring their structure and use.
This is useful when you want to evaluate multiple conditions sequentially.
Syntax:
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// default code
}
Example:
#include <stdio.h>
int main() {
int score = 70;
if (score >= 90) {
printf("Prasun got Grade A.\n");
} else if (score >= 75) {
printf("Prasun got Grade B.\n");
} else if (score >= 60) {
printf("Prasun got Grade C.\n");
} else {
printf("Prasun failed.\n");
}
return 0;
}
Output:
Prasun got Grade C.
Explanation: Only the score >= 60 condition is true, so the corresponding block runs. The ladder skips the rest once a condition is satisfied.
switch is used for handling multiple discrete cases efficiently.
Syntax:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no match
}
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
Output:
Wednesday
Explanation: Since day is 3, the program jumps to case 3 and prints the correct day.
The ternary operator in C provides a shorthand for if-else expressions, making code more concise. This is a compact version of if-else, written in a single line.
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
#include <stdio.h>
int main() {
int age = 20;
age >= 18 ? printf("Shyam is an adult.\n") : printf("Shyam is a minor.\n");
return 0;
}
Output:
Shyam is an adult.
Explanation: The condition evaluates to true, so the expression before the colon is executed.
Yes, logical operators play a crucial role in building compound conditions inside if, else-if, or while statements. You can combine multiple conditions using && (AND), || (OR), and ! (NOT) to make decisions more precise and reliable.
These logical operators improve the clarity and flexibility of conditional logic, especially when checking multiple criteria in one line.
Example:
#include <stdio.h>
int main() {
int age = 25;
int salary = 30000;
if (age >= 21 && salary >= 25000) {
printf("Ankita is eligible for the loan.\n");
} else {
printf("Ankita is not eligible for the loan.\n");
}
if (!(age < 18)) {
printf("Ankita is not a minor.\n");
}
return 0;
}
Output:
Ankita is eligible for the loan.
Ankita is not a minor.
Explanation The first condition uses AND to check both age and salary. The second uses NOT to invert the logic. Logical operators make conditions more expressive.
Several common mistakes can lead to unexpected outcomes:
Avoiding these mistakes makes your code more robust and maintainable. Control the flow of loops and conditionals with break and continue statements in C to refine your decision-making logic.
Below are three real-world examples categorized by difficulty level. Each one is complete with input/output and explanation.
Example:
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Output (Sample):
Enter a number: 4
The number is even.
Explanation: The modulus operator checks if the number is divisible by 2. Based on the result, it prints whether it's even or odd.
Example:
#include <stdio.h>
int main() {
int age = 30;
int experience = 6;
if (age > 25) {
if (experience > 5) {
printf("Rohan is eligible for the senior role.\n");
} else {
printf("Rohan needs more experience.\n");
}
} else {
printf("Rohan does not meet the age criteria.\n");
}
return 0;
}
Output:
Rohan is eligible for the senior role.
Explanation: This example uses nested if conditions to check both age and experience. Only if both are valid does the final condition pass.
Example:
#include <stdio.h>
int main() {
int option;
printf("Choose a number (1-3): ");
scanf("%d", &option);
switch (option) {
case 1: printf("You selected Breakfast.\n"); break;
case 2: printf("You selected Lunch.\n"); break;
case 3: printf("You selected Dinner.\n"); break;
default: printf("Invalid choice.\n");
}
return 0;
}
Output :
Choose a number (1-3): 2
You selected Lunch.
Explanation: The switch statement evaluates option and matches it with a specific case. If no match is found, the default message appears.
Mastering Conditional Statements in C is essential for writing logical and interactive programs. From simple if checks to complex nested decisions and switch cases, these statements form the control flow backbone of any C application.
Once you understand how to use each type effectively—along with logical operators—you’ll be able to solve real-world programming challenges with precision and clarity.
The if statement in C checks whether a given condition is true. If it is, the associated block executes. If the condition is false, the block is skipped and the program continues with the next statement.
An if-else statement allows two paths: one executes when the condition is true, the other when it's false. It helps implement binary decisions and is one of the most widely used conditional statements in C.
Nested if-else lets you place one conditional block inside another. It checks multiple related conditions in a hierarchy, helping implement more complex decision logic by narrowing down choices step-by-step.
The example of a Nested If-Else statement is as follows :-
int marks = 85;
if (marks >= 60) {
if (marks >= 80) printf("Distinction");
else printf("First Class");
}
else printf("Fail");
Output: Distinction
Explanation: Both conditions are evaluated in sequence. Since 85 ≥ 80, the innermost block runs.
The if-else-if ladder handles multiple conditions in order. It evaluates each condition from top to bottom until one is true, then executes the matching block and skips the rest.
The switch statement evaluates an expression and matches its value to a case label. Once matched, the corresponding block runs. If no match is found, the default block executes, if present.
Switch is used for fixed, discrete values, while if-else handles complex logical conditions. Switch is cleaner for multiple constant comparisons, whereas if-else supports ranges and compound conditions.
Yes, logical operators like &&, ||, and ! are commonly used inside conditions to combine or negate expressions, allowing better control and more expressive logic in if, while, or for blocks.
The ternary operator ?: is a shorthand if-else. It evaluates a condition and returns one of two values. It's mostly used for short, inline decisions within expressions or assignments.
Yes, conditional statements can be placed inside for, while, or do-while loops to control logic on each iteration. This allows you to filter, skip, or exit based on specific runtime conditions.
If you skip the else block, only the if condition is checked. If it fails, nothing happens unless other code follows. The program continues normally unless further logic depends on that branch.
Yes, you can nest one switch inside another. However, it’s crucial to manage break statements properly and ensure readability, as deeply nested switch blocks can become hard to trace or debug.
No, braces {} are optional if only one statement follows the if. But omitting them is discouraged, as adding more statements later without braces can introduce logical bugs and readability issues.
A common mistake is misplacing semicolons or missing braces, leading to code that looks correct but doesn't behave as expected. Also, incorrect condition grouping often causes unexpected branching.
Yes, technically all switch logic can be written using if-else. However, switch offers better clarity and performance when dealing with many constant values or menu-style choices.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.