codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

14 / 87 topics
14if, if...else, and Nested if...else15switch..case Statement16Loops17Jump Statements: break, continue, goto
Tutorials/C++ Programming/if, if...else, and Nested if...else
⚡C++ Programming

if, if...else, and Nested if...else

Updated 2026-05-12
30 min read

if, if...else, and Nested if...else

Conditional statements are fundamental building blocks in programming that allow you to control the flow of your program based on certain conditions. In C++, these conditions can be checked using if, if-else, else-if ladders, and nested if statements. Understanding how to use these statements effectively is crucial for writing efficient and logical programs.

Introduction

Conditional statements enable your program to make decisions based on specific criteria. For example, you might want a program to execute different code blocks depending on whether the user input is valid or not. In this tutorial, we will explore various ways to implement conditional logic in C++ using if, if-else, else-if ladders, and nested if statements.

Core Content

if Statement

The simplest form of a conditional statement is the if statement. It checks a condition and executes a block of code only if the condition is true.

Syntax

C++
1if (condition) {
2 // Code to execute if condition is true
3}

Example

Let's write a simple program that checks if a number is positive.

positive_check.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num;
6 cout << "Enter an integer: ";
7 cin >> num;
8
9 if (num > 0) {
10 cout << "The number is positive." << endl;
11 }
12
13 return 0;
14}
Output
Enter an integer: 5
The number is positive.

if-else Statement

The if-else statement allows you to execute one block of code if a condition is true, and another block of code if the condition is false.

Syntax

C++
1if (condition) {
2 // Code to execute if condition is true
3} else {
4 // Code to execute if condition is false
5}

Example

Let's modify the previous example to also check for negative numbers.

positive_negative_check.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num;
6 cout << "Enter an integer: ";
7 cin >> num;
8
9 if (num > 0) {
10 cout << "The number is positive." << endl;
11 } else {
12 cout << "The number is negative or zero." << endl;
13 }
14
15 return 0;
16}
Output
Enter an integer: -3
The number is negative or zero.

else-if Ladder

An else-if ladder allows you to check multiple conditions sequentially. The first true condition's block of code will be executed, and the rest will be skipped.

Syntax

C++
1if (condition1) {
2 // Code to execute if condition1 is true
3} else if (condition2) {
4 // Code to execute if condition2 is true
5} else {
6 // Code to execute if none of the above conditions are true
7}

Example

Let's write a program that checks whether a number is positive, negative, or zero.

positive_negative_zero_check.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num;
6 cout << "Enter an integer: ";
7 cin >> num;
8
9 if (num > 0) {
10 cout << "The number is positive." << endl;
11 } else if (num < 0) {
12 cout << "The number is negative." << endl;
13 } else {
14 cout << "The number is zero." << endl;
15 }
16
17 return 0;
18}
Output
Enter an integer: 0
The number is zero.

Nested if Statements

Nested if statements are used when you need to check multiple conditions within another condition. This can be useful for more complex decision-making processes.

Syntax

C++
1if (condition1) {
2 // Code block 1
3 if (condition2) {
4 // Code block 2
5 }
6}

Example

Let's write a program that checks whether a number is positive and even.

positive_even_check.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num;
6 cout << "Enter an integer: ";
7 cin >> num;
8
9 if (num > 0) {
10 cout << "The number is positive. ";
11 if (num % 2 == 0) {
12 cout << "It is also even." << endl;
13 } else {
14 cout << "It is not even." << endl;
15 }
16 }
17
18 return 0;
19}
Output
Enter an integer: 4
The number is positive. It is also even.

Dangling Else Problem

A dangling else problem occurs when the nesting of if-else statements is ambiguous, leading to unexpected behavior.

Example

Consider the following code:

dangling_else.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 10;
6
7 if (num > 5)
8 if (num < 20)
9 cout << "Number is between 5 and 20." << endl;
10 else
11 cout << "Number is not between 5 and 20." << endl;
12
13 return 0;
14}

In this case, the else statement is associated with the innermost if statement (if (num < 20)), not the outer one (if (num > 5)). This can lead to confusion and bugs. To avoid this, it's best to use braces {} even for single-line statements.

dangling_else_fixed.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 10;
6
7 if (num > 5) {
8 if (num < 20) {
9 cout << "Number is between 5 and 20." << endl;
10 }
11 } else {
12 cout << "Number is not between 5 and 20." << endl;
13 }
14
15 return 0;
16}

Practical Example

Let's create a complete program that checks whether a given year is a leap year. A leap year is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400.

leap_year.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int year;
6 cout << "Enter a year: ";
7 cin >> year;
8
9 if (year % 4 == 0) {
10 if (year % 100 != 0 || year % 400 == 0) {
11 cout << year << " is a leap year." << endl;
12 } else {
13 cout << year << " is not a leap year." << endl;
14 }
15 } else {
16 cout << year << " is not a leap year." << endl;
17 }
18
19 return 0;
20}
Output
Enter a year: 2000
2000 is a leap year.

Summary

ConceptDescription
if statementExecutes code if a condition is true.
if-elseExecutes one block of code if a condition is true, and another if it's false.
else-if ladderChecks multiple conditions sequentially.
Nested ifsChecks multiple conditions within another condition.
Dangling elseAmbiguity in associating an else statement with the correct if.

What's Next?

Now that you have learned about conditional statements, the next topic is the switch..case statement. This allows for more efficient handling of multiple discrete values compared to a long series of if-else statements. Continue your journey in control flow by exploring how to use switch..case in C++.

Stay tuned for more tutorials on codingstuff.io!


PreviousDate and TimeNext switch..case Statement

Recommended Gear

Date and Timeswitch..case Statement