C Programming • C Basics, Operators, Loops
C Programming / Control Structures: if-else

Control Structures: if-else

Notes 2 C Basics, Operators, Loops

The if-else statement is a decision-making control structure in C. It executes one block of statements when a condition is true and another block when the condition is false.

Notes

Control Structure: if-else

The if-else statement is a decision-making control structure in C. It executes one block of statements when a condition is true and another block when the condition is false.

💡 Example
if (marks >= 40) printf("Pass"); else printf("Fail");

If marks are 40 or more, the program displays Pass; otherwise, it displays Fail.

Syntax of if-else

if (condition) { statement; } else { statement; }

How if-else Works

Condition ↓ TRUE → Execute if block ↓ Continue FALSE → Execute else block ↓ Continue

Real-World Example

A college result system can check whether a student has passed or failed based on the marks obtained.

💡 Example

marks >= 40Pass

Otherwise → Fail

Practical Example

Problem Statement

Write a C program that accepts a student's marks and displays whether the student has passed or failed. A student is considered passed if the marks are 40 or above.

Learning Outcomes

  • Understand the working of the if-else statement.
  • Use a relational operator to create a condition.
  • Handle two mutually exclusive outcomes.

Hint

Use marks >= 40 as the condition. Display Pass when it is true and Fail otherwise.

Theory

The if-else statement provides two possible execution paths. If the condition is true, the if block executes; otherwise, the else block executes.

Program

#include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); // check whether the student has passed if (marks >= 40) printf("Pass. "); else printf("Fail. "); return 0; }

Expected Output

Enter marks: 72

Pass.

Note

The else block executes only when the condition in the if statement is false. The two branches provide alternative outcomes for the same condition.

Difference Between if and if-else

if if-else
Provides one decision path. Provides two alternative paths.
Executes a block only when condition is true. Executes one of the two blocks.
No alternative block is required. Contains an else block.

Quick Revision

📌 Remember

if → condition true

if-else → condition true OR condition false

Important Exam Questions

Short Answer Questions

  1. What is an if-else statement in C?
  2. Write the syntax of if-else.
  3. What happens when the if condition is false?
  4. What is the purpose of the else block?
  5. Write one example of an if-else statement.

Long Answer Questions

  1. Explain the if-else statement with syntax and suitable example.
  2. Write a C program to check whether a student has passed or failed using if-else.
  3. Differentiate between if and if-else statements.
🎥 Recommended Learning

Watch a beginner-friendly explanation of the if-else statement in C programming.

▶ Watch: if-else in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for if-else will be provided here.

🧠 Mind Map

Use the mind map for quick revision of condition, if branch and else branch.