Conditionals and If Statements — Fiche d'exercices de Intro to Programming
A conditional lets a program make decisions. An if statement runs a block of code only when its condition is True; otherwise the block is skipped. This is how programs react differently to different inputs.
Conditions are Boolean expressions — they evaluate to True or False. Comparison operators produce Booleans: == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal), >= (greater than or equal). Remember that == compares values while = assigns a value.
Boolean operators combine conditions: and is True only when both sides are True, or is True when at least one side is True, and not flips True to False. Parentheses can clarify the order of evaluation.
An if/elif/else chain checks conditions from top to bottom and runs only the first block whose condition is True. The else block is the catch-all that runs when no earlier condition matched. In Python, indentation defines which lines belong to each block.
Compétences travaillées
- Writing if, elif, and else statements
- Using comparison operators correctly
- Combining conditions with and, or, not
- Tracing code output through branches
- Distinguishing = (assignment) from == (comparison)
Fiche d'exercices : Conditionals and If Statements
Consignes : Résous chaque problème avec soin. Montre clairement tout ton travail. Écris ta réponse finale dans l'espace prévu ou sur une feuille séparée comme indiqué.
-
1.What is the purpose of an if statement in a program?
- A. To repeat a block of code multiple times
- B. To make a decision and execute code only when a condition is true
- C. To store multiple values in a single variable
- D. To display output to the user
Conditionals and If Statements — Fiche d'exercices (suite)
-
2.In the following code snippet, what will be printed to the screen? x = 10 if x > 5: print("Large") else: print("Small")
Conditionals and If Statements — Fiche d'exercices (suite)
-
3.Write an if statement that checks if a variable called age is greater than or equal to 18. If it is, print "Adult". Otherwise, print "Minor".
Conditionals and If Statements — Fiche d'exercices (suite)
-
4.Which of the following is the correct syntax for an if-else statement in Python?
- A. if condition { } else { }
- B. if condition: // code else: // code
- C. if (condition) then { } else { }
- D. if condition then code else code
Conditionals and If Statements — Fiche d'exercices (suite)
-
5.What is the output of the following code? num = 7 if num print("Even") else: print("Odd")
Conditionals and If Statements — Fiche d'exercices (suite)
-
6.Write a program segment that assigns the string "Positive" to a variable called result if a number stored in n is greater than 0. If n is less than 0, assign "Negative". If n equals 0, assign "Zero".
Conditionals and If Statements — Fiche d'exercices (suite)
-
7.What will the following code print? score = 75 if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") else: print("F")
- A. A
- B. B
- C. C
- D. F
Conditionals and If Statements — Fiche d'exercices (suite)
-
8.Explain the difference between if and elif in a conditional statement.
-
9.Write a conditional statement that checks if a variable temp is greater than 100. If it is, print "Hot". If temp is between 50 and 100 (inclusive), print "Warm". Otherwise, print "Cold".
Conditionals and If Statements — Fiche d'exercices (suite)
-
10.Which of the following conditions checks if x is NOT equal to 5?
- A. if x == 5:
- B. if x != 5:
- C. if x <> 5:
- D. if x =! 5:
Corrigé
-
1.
An if statement makes a decision and executes code only when a condition is true.Réponse finale : B
-
2.
Since x = 10 and 10 > 5 is true, the if branch executes and prints "Large".Réponse finale : Large
-
3.
Réponse finale : if age >= 18: print("Adult") else: print("Minor")
-
4.
In Python, an if statement uses a colon after the condition and indented code blocks.Réponse finale : B
-
5.
Since 7Réponse finale : Odd
Corrigé (suite)
-
6.
Réponse finale : if n > 0: result = "Positive" elif n < 0: result = "Negative" else: result = "Zero"
-
7.
Since 75 ≥ 70 is true and the earlier conditions (≥ 90, ≥ 80) are false, the elif score >= 70 branch executes and prints "C".Réponse finale : C
-
8.
Réponse finale : if checks a condition and executes its block if true. elif (short for "else if") is used after an if to check an additional condition only if the previous conditions were false. This allows multiple mutually exclusive conditions to be checked in sequence.
Corrigé (suite)
-
9.
Réponse finale : if temp > 100: print("Hot") elif temp >= 50: print("Warm") else: print("Cold")
-
10.
The != operator checks if two values are not equal in Python.Réponse finale : B
Erreurs fréquentes à éviter
- Using = instead of == in a condition
- Forgetting the colon after if, elif, or else
- Incorrect indentation of the block body
- Assuming every elif runs instead of only the first True branch
- Mixing up and with or when combining conditions
Générez la vôtre
Vous voulez un nouvel ensemble de problèmes de conditionals and if statements à la difficulté de votre choix ? Utilisez WorksheetSmith pour compiler un PDF personnalisé prêt à imprimer en quelques secondes.
Générer une fiche personnalisée sur Conditionals and If Statements
Dernière mise à jour : 2026