True or false
Vrai ou faux
A comparison gives one of two answers: true or false.
Une comparaison donne une réponse parmi deux : true (vrai) ou false (faux).
| Sign | Signe | Meaning | Veut dire |
|---|---|---|---|
=== | is equal to | est égal à | |
!== | is different from | est différent de | |
< > | is smaller than, is bigger than | est plus petit que, est plus grand que | |
<= >= | is smaller than or equal to, bigger than or equal to | est plus petit ou égal à, plus grand ou égal à |
Don’t mix them up: one = puts a value in a variable; three, ===, compare two values.
Ne les mélange pas : un seul = range une valeur dans une variable; trois, ===, comparent deux valeurs.
If…, otherwise…
Si…, sinon…
With if, the program makes a decision. The condition goes between brackets. If it is true, the computer runs the block between braces { }; otherwise, it runs the else block. Then the program carries on.
Avec if (si), le programme prend une décision. La condition se met entre parenthèses. Si elle est vraie, l’ordinateur exécute le bloc entre accolades { }; sinon, il exécute le bloc else (sinon). Ensuite, le programme continue.
Change the temperature and run it again. The lines inside a block are shifted to the right: it does not change anything for the computer, but it helps humans see the blocks. The editor does it for you.
Change la température et exécute de nouveau. Les lignes d’un bloc sont décalées vers la droite : ça ne change rien pour l’ordinateur, mais ça aide les humains à voir les blocs. L’éditeur le fait pour toi.
Several cases
Plusieurs cas
With else if, you can test one case after another. The computer stops at the first condition that is true.
Avec else if (sinon, si), on teste un cas après l’autre. L’ordinateur s’arrête à la première condition vraie.
Two conditions can be joined: && means and (both must be true), || means or (one is enough).
On peut aussi relier deux conditions : && veut dire et (les deux doivent être vraies), || veut dire ou (une seule suffit).
Exercises
Exercices
As in the last lesson, the variables listed after Given: already exist, and your program is tested with several values.
Comme à la leçon précédente, les variables indiquées après Valeurs données existent déjà, et ton programme est testé avec plusieurs valeurs.
Even or odd
Pair ou impair
Print even if the number n is even, and odd otherwise. A number is even when the remainder of its division by 2 is 0.
Affiche pair si le nombre n est pair, et impair sinon. Un nombre est pair quand le reste de sa division par 2 vaut 0.
The bigger one
Le plus grand
Print the bigger of the two numbers a and b. If they are equal, print either one.
Affiche le plus grand des deux nombres a et b. S’ils sont égaux, affiche l’un des deux.
At the cinema
Au cinéma
At the cinema, it is free under 3, $8 from 3 to 12, $12 from 13 to 64, and $9 from 65 on. Print the price for the age age: the word free, or just the number, without the $.
Au cinéma, c’est gratuit avant 3 ans, 8 $ de 3 à 12 ans, 12 $ de 13 à 64 ans, et 9 $ à partir de 65 ans. Affiche le prix pour l’âge age : le mot gratuit, ou juste le nombre, sans le $.
Leap years
Les années bissextiles
A year is a leap year (it has a 29 February) if it can be divided by 4, except if it can be divided by 100… except if it can also be divided by 400! Print leap year or not a leap year for the year year.
Une année est bissextile (elle a un 29 février) si elle est divisible par 4, sauf si elle est divisible par 100… sauf si elle est aussi divisible par 400! Affiche bissextile ou pas bissextile pour l’année annee.