Review: If statements

This material reviews 'If statements,' a fundamental programming concept used to control the flow of a program based on specific conditions.

Swift28·17 fiches·11 questions
high schoolcomputer_scienceprogramming
0
Je sais
1 / 17
0
J'apprends
Recto

What is an if statement?

Appuyez pour retourner
Verso

An if statement allows a program to execute a block of code based on whether a specified condition is true.

Appuyez pour retourner
Je sais
J'apprends

Quiz(11 questions)

Question 1 sur 11

1. What does an if statement do?

Termes dans ce set(17)

What is an if statement?

An if statement allows a program to execute a block of code based on whether a specified condition is true.

Syntax of an if statement

The basic syntax is: if (condition) { // code to execute }

True or false: If statements can only check one condition.

False, because they can chain multiple conditions using else if.

Fill in the blank: 'If x > 10, then ___.'

execute the code block for true condition.

What happens if the condition is false?

If the condition is false, the code block inside the if statement will not execute.

Difference between if and else if

If checks the condition first; else if checks an additional condition if the first is false.

What is an else statement?

An else statement runs a block of code when the associated if condition is false.

Example of a simple if statement

if (temperature > 32) { console.log('It is above freezing.'); }

Nested if statements

You can place an if statement inside another if statement to check multiple conditions.

True or false: Else statements are optional.

True, because an if statement can work without an else.

What do we use comparison operators for?

To evaluate conditions in if statements, such as >, <, ==.

Example of chaining conditions

if (x < 10) { // code 1 } else if (x >= 10 && x < 20) { // code 2 }

True or false: If statements can execute multiple lines of code.

True, because you can include multiple statements inside the braces.

What is a boolean expression?

A boolean expression evaluates to either true or false and is used in if statements.

Comparison: If vs Switch statement

If is for conditions; switch is for multiple discrete values of a variable.

Fill in the blank: 'If it rains, then take an ___.'

umbrella.

What does the else if allow?

It allows checking additional conditions if the previous if is false.

Questions dans ce set(11)

1. What does an if statement do?

A.Executes code based on a condition
B.Always executes code
C.Ends the program
D.Loops code endlessly

2. Which of the following describes an else statement?

A.Checks a condition
B.Executes when the if condition is false
C.Always runs
D.Ends the program

3. Which of these is NOT a comparison operator?

A.==
B.>
C.++
D.<

4. What will happen if a condition in an if statement is false?

A.The code inside the if executes
B.The code inside the if does not execute
C.The program crashes
D.Nothing happens

5. What does the else if statement allow you to do?

A.Create loops
B.Check multiple conditions
C.End the program
D.Perform mathematical operations

6. In the statement 'if (x > 5)', what is 'x > 5' called?

A.A boolean expression
B.An integer
C.A variable
D.A string

7. Difference between if and switch statements?

A.Both are the same
B.If checks conditions, switch checks values
C.Switch checks boolean
D.If is faster than switch

8. True or false: You can nest if statements inside each other.

A.True
B.False
C.Sometimes
D.Only in loops

9. What will this code print: 'if (temperature < 32) { console.log('Freezing'); }' if temperature is 30?

A.Freezing
B.Nothing
C.Error
D.Warm

10. True or false: An if statement can execute multiple lines of code.

A.True
B.False
C.Only in functions
D.Only if it's short

11. Which of these is an appropriate use of if statements?

A.To compare text strings
B.To control program flow based on conditions
C.To create graphics
D.To store data

Sets associés

Créez votre propre set d'étude

Téléchargez un PDF, collez vos notes ou décrivez un sujet – l'IA génère des fiches, des quiz et plus en quelques secondes.

Mis en avant sur