🟡 Control Flow  ·  Lesson 15

if, else-if and else Statements

if, else-if and else Statements

What is if, else-if and else Statements?

if, else-if and else Statements
Conditional statements allow a program to make decisions. They execute different blocks of code depending on whether a condition is true or false.
Level
🟡 Control Flow and Core Programming
Example File
if-else.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
conditionCondition used in if, else-if and else Statements programming.
ifIf used in if, else-if and else Statements programming.
else ifElse if used in if, else-if and else Statements programming.
elseElse used in if, else-if and else Statements programming.
blockBlock used in if, else-if and else Statements programming.

Syntax / Pattern

if (condition) { } else { }

Example Program

#include <iostream>
using namespace std;
int main(){
    int marks = 72;
    if (marks >= 90) cout << "A+";
    else if (marks >= 60) cout << "Pass";
    else cout << "Need Improvement";
    return 0;
}

Expected Output

Pass

Program Explanation

  • First condition is false, second is true.
  • Only the matching block is executed.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • grade systems
  • eligibility checks
  • login validation

Common Mistakes

  • Using = instead of ==.
  • Missing braces in multi-line blocks.

Practice Tasks

  1. Check pass/fail from marks.
  2. Find greatest of three numbers.

Summary

if, else-if and else Statements is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is if, else-if and else Statements?

if, else-if and else Statements
Conditional statements allow a program to make decisions. They execute different blocks of code depending on whether a condition is true or false.
Level
🟡 Control Flow and Core Programming
Example File
if-else.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
conditionCondition used in if, else-if and else Statements programming.
ifIf used in if, else-if and else Statements programming.
else ifElse if used in if, else-if and else Statements programming.
elseElse used in if, else-if and else Statements programming.
blockBlock used in if, else-if and else Statements programming.

Syntax / Pattern

if (condition) { } else { }

Example Program

#include <iostream>
using namespace std;
int main(){
    int marks = 72;
    if (marks >= 90) cout << "A+";
    else if (marks >= 60) cout << "Pass";
    else cout << "Need Improvement";
    return 0;
}

Expected Output

Pass

Program Explanation

  • First condition is false, second is true.
  • Only the matching block is executed.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • grade systems
  • eligibility checks
  • login validation

Common Mistakes

  • Using = instead of ==.
  • Missing braces in multi-line blocks.

Practice Tasks

  1. Check pass/fail from marks.
  2. Find greatest of three numbers.

Summary

if, else-if and else Statements is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial