True, false and comparisons
How C answers a question: 0 is false and any other value is true. The six relational operators, reading a comparison as a question, and the bug of writing = instead of ==.
Why programs need decisions
Until now every line of a program ran, one after the other. An assignment like this one runs every time the program reaches it:
power = voltage * current;A decision is different. It runs a piece of code only when something is true:
if (voltage > 5.0)
{
printf("High");
}With voltage equal to 7.2, the question 7.2 > 5.0 is true, so High is printed. With voltage equal to 3.3, the question is false, and the printf is skipped. The question in the parentheses is called a condition (תנאי). This lesson is about conditions: what C does with them and what their answer looks like. The next lesson is about if itself.
Calculate first, then compare
A condition often compares the result of a calculation. The program does the calculation first and stores the result:
#include <stdio.h>
int main(void)
{
int score = 48;
int bonus = 7;
int total;
total = score + bonus;
printf("%d\n", total);
return 0;
}5548 + 7 gives 55. Now we can ask a question about it: the pass mark is 55, so is a total of 55 a pass? That question is a condition, and C needs a comparison to answer it.
Expressions give numbers or truth values
An expression (ביטוי) is a piece of code that produces a value. Some expressions produce a number, and some produce a truth value (ערך אמת): true or false.
| Kind | Expression | Result | The result is |
|---|---|---|---|
| arithmetic | score + bonus | 55 | a number |
| relational | total >= 55 | 1 | true or false |
| logical | passed && submitted | 0 or 1 | true or false |
A condition is an expression that C reads as true or false. Logical expressions with && come in lesson 3.
In C, zero is false
C has no special words for true and false in a condition. It uses numbers:
- false (שקר) is the value
0. - true (אמת) is any value that is not zero:
1,-3,27are all true.
Comparisons always answer with exactly 1 (true) or 0 (false). You can see this by printing a comparison with %d:
#include <stdio.h>
int main(void)
{
double voltage = 7.2;
printf("%d\n", voltage > 5.0);
printf("%d\n", voltage < 5.0);
printf("%d\n", 8 == 3);
return 0;
}1
0
0The six relational operators
A relational operator (אופרטור יחס) compares two values. There are six:
| Operator | Meaning | Example | Value when score is 55 |
|---|---|---|---|
== | equal to | score == 55 | 1 |
!= | not equal to | score != 55 | 0 |
< | less than | score < 55 | 0 |
<= | less than or equal to | score <= 55 | 1 |
> | greater than | score > 55 | 0 |
>= | greater than or equal to | score >= 55 | 1 |
The result is a truth value, 1 or 0, not one of the values being compared. The two-character operators are written with no space and in this order: >= and <=, never => or =<.
Read a comparison as a question
Every comparison is a yes-or-no question. Read it in full English before you write it, and read it back when you check code.
| C | Question |
|---|---|
temperature < 0 | Is temperature below zero? |
score >= 55 | Is score at least 55? |
count != 0 | Is count different from zero? |
command == 'R' | Is command exactly the character R? |
For each one, name the left value, the operator, the right value, and the result. In the last row, 'R' in single quotes is one character. 'R' and 'r' are different characters.
Exercise 1: predict 0 or 1
Do the arithmetic first, then compare. Characters are stored as numbers: 'A' is 65 and 'a' is 97.
= stores a value, == compares two values
These two look alike and do completely different jobs.
- Assignment (השמה)
score = 55;works out the right side and stores it inscore. - Equality (שוויון)
score == 55asks whetherscoreequals 55. It gives1or0and does not changescore.
Read them differently when you say them aloud: "score gets 55" and "is score equal to 55?".
Here is what happens with the bug:
#include <stdio.h>
int main(void)
{
int score = 40;
if (score = 55)
{
printf("Exactly 55\n");
}
printf("Score: %d\n", score);
return 0;
}Exactly 55
Score: 55Line by line:
scorestarts at 40.score = 55does not compare. It stores 55 inscore.- The value of the whole expression
score = 55is the value that was stored, 55. - 55 is not zero, so the condition is true and
Exactly 55is printed. - The last line prints the changed score, 55.
The repair is one character: if (score == 55). Then the condition is 40 == 55, which is 0, the block is skipped, and the program prints only Score: 40.
Key words
| English | Hebrew | Meaning |
|---|---|---|
| condition | תנאי | an expression that C reads as true or false |
| expression | ביטוי | code that produces a value |
| truth value | ערך אמת | 1 (true) or 0 (false) |
| true | אמת | any value that is not 0 |
| false | שקר | the value 0 |
| relational operator | אופרטור יחס | == != < <= > >= |
assignment = | השמה | stores a value in a variable |
equality == | שוויון | compares two values, gives 1 or 0 |
Practice
Answers to these and more questions are on the practice page (questions 1 to 6).