C++ Tutorial

C++ Booleans

bool is true or false. Comparisons produce booleans. Conditions consume them.

true or false

A bool stores one of two values: true or false. That is the whole type. You use it to remember a yes-or-no fact: a door is open, a score is high enough, a loop should keep going.

Write the literals in lowercase. True and False are Python. C++ will not compile them.

A complete program

Declare a bool, print it, then print a comparison. By default cout prints booleans as1 and 0, not the words true and false.

Example

#include <iostream>
using namespace std;

int main() {
  bool ready = true;
  bool empty = false;
  cout << ready << endl;
  cout << empty << endl;
  cout << (7 > 3) << endl;
  cout << (7 == 3) << endl;
  return 0;
}

Run this in /cpp/try. You should see 1, 0, 1,0.

Comparisons produce booleans

Every comparison expression has type bool. You can store it, print it, or pass it straight intoif. The operators are the same ones you saw in Operators.

ExpressionTrue when
a == ba equals b
a != ba is not equal to b
a < ba is less than b
a <= ba is less than or equal to b
a > ba is greater than b
a >= ba is greater than or equal to b

Use == to compare. A single = assigns. Writing if (x = 1) setsx to 1 and then treats that as true. The compiler may warn; the program is still wrong.

Combine with and, or, not

C++ uses && (and), || (or), and ! (not). &&is true only if both sides are true. || is true if at least one side is true. ! flips a value.

Example

#include <iostream>
using namespace std;

int main() {
  int age = 20;
  bool member = true;
  cout << (age >= 18 && member) << endl;
  cout << (age < 18 || member) << endl;
  cout << (!member) << endl;
  return 0;
}

Print the words true and false

Insert boolalpha into the stream if you want text instead of 1 and 0. It stays on until you switch back with noboolalpha.

Example

#include <iostream>
using namespace std;

int main() {
  bool ok = true;
  cout << boolalpha;
  cout << ok << endl;
  cout << (2 == 3) << endl;
  return 0;
}

Conditions consume them

An if, a while, and the middle part of a for all expect something that converts to bool. You will write that in the next chapters. The value itself is still just true or false.

Integers convert too: 0 is false, any other number is true. That is why a forgotten== can look like a working condition. Prefer an actual bool or a comparison.

What to remember

  • bool holds true or false.
  • Comparisons such as == and < produce a bool.
  • cout prints 1 and 0 unless you use boolalpha.
  • Combine tests with &&, ||, and !.

Next: if and else — the first place those booleans get used.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Chemistry

Acidic or not

On the pH scale, 7 is neutral at 25 °C. Below 7 is acidic, above 7 is alkaline. 4.2 is lemon-juice territory, not a strong mineral acid, but it is still acidic.

bool stores that yes/no. boolalpha prints true and false as words instead of 1 and 0.

acidic ⇔ pH < 7

pH 7 is exactly neutral only at 25 °C. Kw changes with temperature, so the neutral point drifts a little.

Example

#include <iostream>
using namespace std;

int main() {
  double ph = 4.2;
  bool acidic = ph < 7.0;
  cout << boolalpha << acidic << endl;
  return 0;
}

Physics

Ice or liquid water

At standard pressure, ice melts at 0 °C. A freezer at −3 °C keeps water solid. The boolean is the whole scientific question for this check: is the sample above that point?

liquid if t > 0 °C (at 1 atm)

Example

#include <iostream>
using namespace std;

int main() {
  double celsius = -3.0;
  bool liquid_water = celsius > 0.0;
  cout << (liquid_water ? "yes" : "no — ice") << endl;
  return 0;
}

FAQ: C++ Booleans

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ boolean examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ boolean in this C++ C++ lesson (C++ Booleans).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.