🟡 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.
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
🟡 Control Flow and Core Programming
Example File
if-else.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| condition | Condition used in if, else-if and else Statements programming. |
| if | If used in if, else-if and else Statements programming. |
| else if | Else if used in if, else-if and else Statements programming. |
| else | Else used in if, else-if and else Statements programming. |
| block | Block 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
- Check pass/fail from marks.
- 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.
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
🟡 Control Flow and Core Programming
Example File
if-else.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| condition | Condition used in if, else-if and else Statements programming. |
| if | If used in if, else-if and else Statements programming. |
| else if | Else if used in if, else-if and else Statements programming. |
| else | Else used in if, else-if and else Statements programming. |
| block | Block 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
- Check pass/fail from marks.
- 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.