Control with Conditions - JSS 2 Note

Control with Conditions - JSS 2 Digital Technologies

CONTROL WITH CONDITIONS

Subject: Digital Technologies
Class: JSS 2
Term: Second Term
Week: 6
Topic: Control with Conditions
Duration: 80 Minutes (Double Period)

1. Learning Objectives

By the end of the lesson, students should be able to:

  • Explain the meaning of conditional statements.
  • Identify IF and ELSE statements in programming.
  • Explain how conditions help programs make decisions.
  • Identify examples of conditions in everyday life.
  • Use Scratch conditional blocks to make a simple program respond to a condition.

2. Introduction

In everyday life, people make decisions based on different conditions. For example, a student may decide to carry an umbrella if it is raining. A teacher may award a mark if a student answers a question correctly.

Computer programs also need to make decisions. A game may check whether a player has touched an obstacle, while a quiz program may check whether an answer is correct. Conditional statements enable a program to respond differently depending on whether a condition is satisfied.

3. Meaning of Control with Conditions

Definition: Control with conditions is a programming technique in which a program checks whether a condition is true or false and chooses an action based on the result.

A condition is a test that produces a true or false result. The program uses the result to determine what should happen next.

Examples of Conditions

  • Is the player's score greater than 10?
  • Is the answer correct?
  • Has the sprite touched the edge of the Stage?
  • Is the number greater than zero?
  • Is the mouse pointer touching a particular sprite?
Example from everyday life: If it is raining, carry an umbrella. The condition is whether it is raining. If the condition is true, the person carries an umbrella.

4. The IF Statement

Definition: An IF statement checks a condition and performs an action when that condition is true.

If the condition is false, the action inside the IF statement is skipped, and the program continues with the next instruction.

Example

A program checks whether a player's score is greater than 10. If it is, the program displays a message saying that the player has won.

IF Score > 10 THEN DISPLAY "You have won!"

How It Works

  1. The program checks the value of Score.
  2. It compares the score with 10.
  3. If Score is greater than 10, the message is displayed.
  4. If Score is 10 or less, the message is not displayed by this IF statement.

5. The IF-ELSE Statement

Definition: An IF-ELSE statement checks a condition and selects one of two actions. One action is performed if the condition is true, and the other is performed if the condition is false.

Example

A quiz program checks whether a student's answer is correct.

IF Answer is correct THEN DISPLAY "Correct answer!" ELSE DISPLAY "Try again!"

If the answer is correct, the program displays “Correct answer!” If the answer is incorrect, it displays “Try again!”

Difference Between IF and IF-ELSE

IF Statement IF-ELSE Statement
Checks a condition and performs an action if it is true. Checks a condition and selects between two alternative actions.
If the condition is false, the specified action is skipped. If the condition is false, the ELSE action is performed.
Example: If the score is above 10, display “Winner”. Example: If the answer is correct, display “Correct”; otherwise, display “Incorrect”.

6. Conditions in Scratch

Scratch is a block-based programming environment that provides conditional blocks in the Control category. These blocks allow a program to make decisions.

The IF Block

The IF block runs the instructions placed inside it only when its condition is true.

IF <condition> THEN [instructions] END IF

The IF-ELSE Block

The IF-ELSE block provides two possible paths. Scratch executes the first set of instructions if the condition is true and the second set if it is false.

IF <condition> THEN [instructions for TRUE] ELSE [instructions for FALSE] END IF

The examples above describe the logic. In Scratch, students use the actual puzzle-shaped blocks and place instructions inside them.

7. Creating a Simple Conditional Program in Scratch

In this activity, students will create a program that checks whether a sprite is touching the mouse pointer.

Steps

  1. Open Scratch and create a new project.
  2. Select a sprite to use for the activity.
  3. Go to the Events category and add a when green flag clicked block.
  4. From the Control category, add a forever block beneath the event block.
  5. Place an if-else block inside the forever block.
  6. From the Sensing category, use the touching mouse-pointer? condition in the IF-ELSE block.
  7. Inside the IF section, add a Looks block that makes the sprite say “Hello!” for a short time.
  8. Inside the ELSE section, add a Looks block that makes the sprite say “Move the mouse to me!” for a short time.
  9. Click the green flag and move the mouse pointer over and away from the sprite.
  10. Observe how the sprite responds to the condition.

Expected Result

  • When the mouse pointer touches the sprite, the sprite displays “Hello!”
  • When the mouse pointer is not touching the sprite, the sprite displays “Move the mouse to me!”
Learning point: The program repeatedly checks the condition inside the forever loop. The IF-ELSE block determines which message is displayed based on the result.

8. Other Applications of Conditional Statements

Application Condition Action
Quiz application Is the answer correct? Display the appropriate feedback.
Computer game Has the player reached the target score? Display a winning message.
Traffic control simulation Is the signal red? Display a stop instruction.
Student grading program Is the score at or above the required mark? Display a suitable result message.
Interactive animation Is the sprite touching an object? Change the sprite's appearance or behaviour.

9. Advantages of Conditional Statements

  • They allow programs to make decisions.
  • They help programs respond to user actions and events.
  • They make games and applications interactive.
  • They allow different actions to occur under different circumstances.
  • They help programmers create solutions to problems that require choices.

10. Summary

Control with conditions allows a computer program to make decisions by checking whether a condition is true or false. An IF statement performs an action when its condition is true. An IF-ELSE statement selects between two alternative actions depending on the result of the condition.

In Scratch, conditional blocks are found in the Control category. They can be combined with sensing blocks, operators and other instructions to create interactive games, quizzes and animations.

11. Evaluation Questions

  1. What is control with conditions in programming?
  2. What is a condition?
  3. Explain the function of an IF statement.
  4. Explain the function of an IF-ELSE statement.
  5. State two differences between IF and IF-ELSE statements.
  6. In which Scratch category can you find the IF and IF-ELSE blocks?
  7. Give two examples of situations where conditional statements can be used.
  8. Describe how to create a Scratch program that responds when a sprite touches the mouse pointer.

12. Assignment

  1. Define a conditional statement in programming.
  2. Explain the difference between IF and IF-ELSE statements.
  3. Write a simple pseudocode example of an IF statement.
  4. Write a simple pseudocode example of an IF-ELSE statement.
  5. Describe a Scratch project that displays one message when a condition is true and another message when it is false.

13. Interactive Quiz

Choose the correct option for each question, then click Submit Quiz to see your score.

1. What is a conditional statement used for?

2. What does an IF statement do?

3. What happens when an IF condition is false and there is no ELSE section?

4. What does IF-ELSE allow a program to do?

5. Where are IF and IF-ELSE blocks found in Scratch?

6. Which of these is an example of a condition?

7. A quiz program checks whether an answer is correct. Which programming feature is most suitable?

8. In an IF-ELSE statement, which action is selected when the condition is false?

9. Which Scratch category contains the “touching mouse-pointer?” condition?

10. Why are conditional statements useful in games?

Comments

Popular posts from this blog

Complete Computer Studies/ICT Curriculum for JSS 1 to SSS 3

90 Objective Examination Questions in Major Subjects

2027 UTME and SSCE Free CBT Practice Portal