Nested decisions · Décisions imbriquées
One mark, three outcomes
- A mark of 80 or more is a distinction, 50 or more is a pass, and anything lower is a fail.
- One
IFhas two paths. The second decision goes inside theELSE, as its ownIF. - Each
IFhas its ownENDIF. Indent the inner one so you can see whichELSEit belongs to.
Une note, trois résultats
- Une note de 80 ou plus est une distinction, 50 ou plus est une réussite, et tout le reste est un échec.
- Un
IFa deux chemins. La seconde décision va à l'intérieur duELSE, en tant que son propreIF. - Chaque
IFa son propreENDIF. Indentez celui du dedans pour voir auquelELSEil appartient.
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
Count the marks that count
- A validation tally walks the list and adds 1 only when the value is in range.
- The range test is the
ANDfrom the previous lesson, inside the loop.
Comptez les notes qui comptent
- Un décompte de validation parcourt la liste et ajoute 1 uniquement lorsque la valeur est dans la plage.
- Le test de plage est le
ANDde la leçon précédente, placé à l'intérieur de la boucle.
DECLARE Mark : ARRAY[1:4] OF INTEGER
DECLARE I, Count : INTEGER
Mark[1] ← 80
Mark[2] ← -1
Mark[3] ← 100
Mark[4] ← 40
Count ← 0
FOR I ← 1 TO 4
IF Mark[I] >= 0 AND Mark[I] <= 100
THEN
Count ← Count + 1
ENDIF
NEXT I
OUTPUT Count // → 3
Watch out
- The inner
IFstarts on the line afterELSE. It is not written on theELSEline. - Close the inner
ENDIFbefore the outer one. The lastENDIFbelongs to the firstIF.
Attention
- Le
IFinterne commence sur la ligne aprèsELSE. Il n'est pas écrit sur la ligneELSE. - Fermez l'interne
ENDIFavant l'externe. Le dernierENDIFappartient au premierIF.
Common mistakes
- An
ENDIFin the wrong place closes the outer test and leaves the inner one open. - A mark of exactly 50 is a pass, and exactly 80 is a distinction. The tests use
>=.
Erreurs courantes
- Un
ENDIFau mauvais endroit ferme le test externe et laisse l'interne ouvert. - Une note exactement de 50 est une réussite, et exactement 80 est une distinction. Les tests utilisent
>=.
Now you try
- The first two tasks are nested decisions. The third counts with
AND. - Press Check answer to test it.
À vous maintenant
- Les deux premières tâches sont des décisions imbriquées. La troisième compte avec
AND. - Appuyez sur Vérifier la réponse pour tester.
Read an INTEGER Mark. Output Distinction for 80 or more, otherwise Pass for 50 or more, otherwise Fail. Nest the second IF under · sous ELSE. · Lire un INTEGER Mark. Afficher Distinction pour 80 ou plus, sinon Pass pour 50 ou plus, sinon Fail. Imbriquer la deuxième IF sous ELSE.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Same three outcomes as the previous task. This input should take the inner ELSE. · Mêmes trois résultats que la tâche précédente. Cette entrée devrait prendre la ELSE interne.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
The array Mark holds · contient 80, -1, 100, 40. Count how many values are from 0 to · à 100 inclusive and output the count (it is 3). · Le tableau Mark contient 80, -1, 100, 40. Compter combien de valeurs sont comprises entre 0 et 100 inclusif et afficher le compteur (c'est 3).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.