45 min

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.

Two runs of the same decision. Step through it: the value goes in, the comparison gives 1 or 0, and only then does the program act.Open full screen

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;
}
55

48 + 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.

KindExpressionResultThe result is
arithmeticscore + bonus55a number
relationaltotal >= 551true or false
logicalpassed && submitted0 or 1true 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, 27 are 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
0

The six relational operators

A relational operator (אופרטור יחס) compares two values. There are six:

OperatorMeaningExampleValue when score is 55
==equal toscore == 551
!=not equal toscore != 550
<less thanscore < 550
<=less than or equal toscore <= 551
>greater thanscore > 550
>=greater than or equal toscore >= 551

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 =<.

Click score = 40, 55 and 70 and predict each tile first. At 55, the boundary, < and <= give different answers, and so do > and >=.Open full screen

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.

CQuestion
temperature < 0Is temperature below zero?
score >= 55Is score at least 55?
count != 0Is 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.

Write 0 or 1 for each card on paper, then click a card to check it. The answers and the reason for each are in practice question 1.Open full screen

= 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 in score.
  • Equality (שוויון) score == 55 asks whether score equals 55. It gives 1 or 0 and does not change score.

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: 55

Line by line:

  1. score starts at 40.
  2. score = 55 does not compare. It stores 55 in score.
  3. The value of the whole expression score = 55 is the value that was stored, 55.
  4. 55 is not zero, so the condition is true and Exactly 55 is printed.
  5. 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.

Step through the bug and watch the score change inside the if.Open full screen

Key words

EnglishHebrewMeaning
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).