cint university / 101 / ced, the cint editor
101-06: true or false
This is lesson 101-06 of CU-101.2, checked against cint 0.2.0.
You will ask questions whose answer is true or false, and make a program do one thing or another depending on the answer.
the program
I64 year = 2024;
Bool leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
if (leap) {
"{year} is a leap year\n";
} else {
"{year} is not a leap year\n";
}
"leap = {leap}\n";
Run it:
2024 is a leap year
leap = true
what happened
A comparison is a question with a yes or no answer. year % 4 == 0 asks whether the remainder of year divided by 4 is 0. For 2024 the answer is yes, and cint writes yes as true and no as false. These are the comparisons:
| Written | Asks |
|---|---|
a == b |
is a equal to b? |
a != b |
is a different from b? |
a < b and a <= b |
is a less than b, or less than or equal to it? |
a > b and a >= b |
is a greater than b, or greater than or equal to it? |
The test for equality is two equals signs, ==. One = puts a number into a variable, as in lesson 101-03.
Bool is the type of true and false. A Bool variable holds one of the two answers. leap holds the answer to the whole question on the second line, and {leap} prints it as true.
&&, || and ! combine answers. a && b is true when both are true. a || b is true when at least one is. !a is true when a is false. The parentheses work as they do in arithmetic: the part inside them is worked out first. The second line reads "divisible by 4, and either not divisible by 100 or divisible by 400". That is the calendar's rule: 2024 and 2000 are leap years, and 1900 is not.
if chooses. The lines in the braces after if (leap) run when leap is true. The lines after else run when it is false. Exactly one of the two prints. The question in the parentheses after if is called the condition.
else if asks another question. When there are more than two choices, ask them one after another:
I64 t = 25;
if (t < 10) {
"cold\n";
} else if (t < 20) {
"mild\n";
} else {
"warm\n";
}
This prints warm. cint checks the conditions from the top and runs the first one that is true. If none is, it runs the else.
change it
- Set
yearto 1900. What do the two lines say? - Set
yearto 2000 and run it again. - Add an
else ifto the temperature example that printshotfrom 30 up. Where can it go, and why?
what 1900 prints
1900 is not a leap year
leap = false
where the else if for hot can go
It can go anywhere between the `if` and the `else`:I64 t = 35;
if (t < 10) {
"cold\n";
} else if (t < 20) {
"mild\n";
} else if (t >= 30) {
"hot\n";
} else {
"warm\n";
}
This prints `hot`. Nothing can come after the `else`, because the `else` takes every value the questions above it left.
when it goes wrong
One = where two were meant is a common slip. cint stops it before the program runs:
I64 x = 5;
if (x = 3) { "three\n"; }
error[C1041]: assignment in a condition
--> assign.ci:2:5
|
2 | if (x = 3) { "three\n"; }
| ^
|
A condition must be a Bool, too. A number on its own is not a question:
I64 year = 2024;
if (year) {
"yes\n";
}
error[C2002]: condition is not `Bool`
--> ifnum.ci:2:5
|
2 | if (year) {
| ^
|
Some other languages, such as C and Python, take any number that is not 0 to mean true. In cint, a condition must be true or false, so a test on a number is written out, such as year != 0.