🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #43

else


The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

In the example below, the program checks the value of time. If it is less than 18, it prints "Good day". Otherwise, it prints "Good evening":

Example

int time = 20;

if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}

// Outputs "Good evening."

Example explained

Because time is 20, the condition time < 18 is false, so the code inside the else block runs and prints "Good evening.". If time was less than 18, the program would print "Good day." instead.


Using a Boolean Variable

You can also store the condition in a boolean variable and use it in the if...else statement. This can make the code easier to read.

Example

int time = 20;

bool isDay = time < 18;

if (isDay) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}

// Outputs "Good evening."

Tip: A name like isDay makes it easy to understand what the condition means.

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

else – FAQs

Quick answers about learning else in C++.

This free note from CodingNow 2.0 explains else in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including else, is 100% free with no signup required.
With focused practice, most students grasp else in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now