← C# from Scratch Lesson 12 of 39 · 10 XP

Boolean

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

A bool holds true or false. It usually comes from a comparison:

int age = 17;
bool adult = age >= 18;           // false
bool canPlay = adult || hasParent;
Console.WriteLine(adult);         // False — with a capital letter!

In C# a bool is not a number: if (1) won't compile (unlike C/C++). You need an explicit condition: if (n != 0).

It's useful to give boolean variables question-like names: isAlive, hasKey, canJump.

Task. Read a year. Print True if it's a leap year, otherwise False. A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. Print the boolean expression itself, without an if.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.