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

• Comp Sci
• Tesla In Person
In summary, the code in the "repeat 10" block is doing the following 10 times:1. Change a by 12. Add 1 to a3. Repeat the previous two4. Change a by -15. Subtract 1 from a6. Repeat the previous five7. Change a by -28. Subtract 2 from a9. Repeat the previous eight10. Change a by -3
Tesla In Person
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
24.9 KB · Views: 99
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.

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.

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.

Tesla In Person

## 1. What is a Scratch code?

A Scratch code is a block-based coding language used in the Scratch programming environment. It allows users to create interactive stories, games, and animations by snapping together different code blocks.

## 2. How do I find the value of "a" at the end of a Scratch program?

To find the value of "a" at the end of a Scratch program, you can use the "set a to" block. This block allows you to assign a value to the variable "a" and you can then use the "show" block to display the value of "a" at the end of your program.

## 3. Can I change the value of "a" during the program?

Yes, you can change the value of "a" during the program by using the "change a by" block. This block allows you to add or subtract a specific amount from the current value of "a". You can also use other mathematical operations, such as multiplication or division, to change the value of "a".

## 4. Why is it important to know the value of "a" at the end of a program?

Knowing the value of "a" at the end of a program is important because it allows you to keep track of and manipulate the value of this variable throughout your program. This can be useful in creating more complex and dynamic programs.

## 5. Can I use "a" in other parts of my program?

Yes, you can use "a" in other parts of your program by using the "set a to" block or the "change a by" block. These blocks allow you to change the value of "a" and use it in different parts of your program. You can also use "a" in conditional statements, loops, and other code blocks to control the flow of your program.

• Engineering and Comp Sci Homework Help
Replies
17
Views
651
• Engineering and Comp Sci Homework Help
Replies
15
Views
1K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Replies
20
Views
2K
• Engineering and Comp Sci Homework Help
Replies
8
Views
2K
• Engineering and Comp Sci Homework Help
Replies
8
Views
2K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K
• Engineering and Comp Sci Homework Help
Replies
10
Views
2K
• Programming and Computer Science
Replies
16
Views
1K