Control with Conditions - JSS 2 Note
CONTROL WITH CONDITIONS
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
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?
4. The IF Statement
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.
How It Works
- The program checks the value of Score.
- It compares the score with 10.
- If Score is greater than 10, the message is displayed.
- If Score is 10 or less, the message is not displayed by this IF statement.
5. The IF-ELSE Statement
Example
A quiz program checks whether a student's answer is correct.
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.
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.
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
- Open Scratch and create a new project.
- Select a sprite to use for the activity.
- Go to the Events category and add a when green flag clicked block.
- From the Control category, add a forever block beneath the event block.
- Place an if-else block inside the forever block.
- From the Sensing category, use the touching mouse-pointer? condition in the IF-ELSE block.
- Inside the IF section, add a Looks block that makes the sprite say “Hello!” for a short time.
- Inside the ELSE section, add a Looks block that makes the sprite say “Move the mouse to me!” for a short time.
- Click the green flag and move the mouse pointer over and away from the sprite.
- 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!”
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
- What is control with conditions in programming?
- What is a condition?
- Explain the function of an IF statement.
- Explain the function of an IF-ELSE statement.
- State two differences between IF and IF-ELSE statements.
- In which Scratch category can you find the IF and IF-ELSE blocks?
- Give two examples of situations where conditional statements can be used.
- Describe how to create a Scratch program that responds when a sprite touches the mouse pointer.
12. Assignment
- Define a conditional statement in programming.
- Explain the difference between IF and IF-ELSE statements.
- Write a simple pseudocode example of an IF statement.
- Write a simple pseudocode example of an IF-ELSE statement.
- 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.
Comments
Post a Comment