Scratch codes -- Find the value of a at the end of the program

  • Context: Comp Sci 
  • Thread starter Thread starter Tesla In Person
  • Start date Start date
  • Tags Tags
    Program scratch Value
Click For Summary

Discussion Overview

The discussion revolves around understanding a Scratch programming code snippet, specifically focusing on the behavior of a variable 'a' and the implications of the "repeat 10" block. Participants explore how the code modifies the value of 'a' through iterations and clarify the difference between "set" and "change by" commands.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants express confusion about the "repeat 10" block and its effect on the variable 'a'.
  • One participant suggests that the block indicates executing the following code 10 times in succession.
  • Another participant proposes that if 'a' is initially set to 1, and the loop doubles 'a' each time, they question how to handle the value of 'a' across iterations.
  • There is a clarification that the instruction "change a by 1" does not replace the value of 'a' but rather increments it.
  • Participants discuss the initial value of 'a' when it first reaches the "change a by 1" instruction, with some asserting it remains 5.
  • One participant concludes that repeating the increment results in 'a' reaching 15 after 10 iterations, while another emphasizes that the condition for 'a' being less than 3 is never met.
  • A participant mentions the existence of tutorials for Scratch that could help clarify these concepts.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of the "change a by 1" instruction and its implications for the variable 'a', but there is some confusion regarding the initial conditions and the interpretation of the code's behavior. Multiple interpretations of the code's execution remain present.

Contextual Notes

Some participants express uncertainty about the initial value of 'a' and how it interacts with the loop, highlighting the potential for misunderstanding Scratch's commands.

Who May Find This Useful

Individuals new to Scratch programming or those seeking clarification on variable manipulation and control structures in programming.

Tesla In Person
Messages
34
Reaction score
13
Homework Statement
Find the value of a at the end of the program
Relevant Equations
No need of equations
Hi, i am really new to scratch programming so I am having difficulties in interpreting how does this code work ? I understand the first block set a = 5. But i don't know what is that " Repeat 10 " block. What does it do? So really stuck at that "repeat 10" so haven't gone further.
 

Attachments

  • 1.PNG
    1.PNG
    24.9 KB · Views: 170
Physics news on Phys.org
I think it's saying do the following block of code 10 times in a row.
 
Office_Shredder said:
I think it's saying do the following block of code 10 times in a row.
Ok so if i set a=1. Then a <3 so a*2 = 1*2 = 2. If i repeat this process 10 times , what do i have to do with the a value each time. Add them up ? 2 *10= 20 which is option c) which is wrong. The correct answer is e) 15.
 
Tesla In Person said:
Ok so if i set a=1.
There is no instruction set a to 1, the instruction inside the loop is change a by 1. What is the value of a the first time it reaches this instruction?
 
pbuk said:
There is no instruction set a to 1, the instruction inside the loop is change a by 1. What is the value of a the first time it reaches this instruction?
I thought "change a by 1" meant replace the value of 5 by 1.
"What is the value of a the first time it reaches this instruction?"- 5?
 
Tesla In Person said:
I thought "change a by 1" meant replace the value of 5 by 1.
You are not the first person to have been caught out by that, it's a confusing "feature" of Scratch.
Tesla In Person said:
"What is the value of a the first time it reaches this instruction?"- 5?
Yes.
 
pbuk said:
You are not the first person to have been caught out by that, it's a confusing "feature" of Scratch.

Yes.
So we add 1 ? It becomes 6 and 6 is not smaller then 3 so we go back and keep adding 1 ? Repeating this 10 times gives a=15 which is correct.
 
  • Like
Likes   Reactions: DrClaude, FactChecker and pbuk
Tesla In Person said:
I thought "change a by 1" meant replace the value of 5 by 1.
No, as already mentioned. There's a difference between "set" and "change by". "Set" replaces the old value of the variable by some new value. "Change by" adds or subtracts some value to a variable. In the program "change a by 1" adds 1 each time in the loop. If it had said "change a by -2" it would have subtracted 2 each time in the loop.

Tesla In Person said:
what do i have to do with the a value each time. Add them up ? 2 *10= 20 which is option c) which is wrong. The correct answer is e) 15.
In the program, a's value is always >= 5, so the if condition is never executed. Essentially what the code does is to increase a (which starts off at 5) by 1 ten times, which results in a's final value being 15.
 
  • Informative
Likes   Reactions: Tesla In Person
I believe there are tutorials for Scratch that are easy and would introduce these basic Scratch statements.
I Googled it and this looks like one.
 
  • Like
Likes   Reactions: Tesla In Person

Similar threads

Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
7
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K