Hi guys,
I started learning programming in school and I got some tasks for my homework.
I solved the easier tasks, but I can't solve "harder" ones.
I hope you will give me some instructions.
Thanks
1. taskProgram which writes my name 10 times.
2. taskProgram which writes numbers 1-10
3. task Program which writes numbers 0-100 and only shows even or odd numbers.
Each of these exercises requires you to understand looping constructs. The first one asks you to repeat a task 10 times. The second asks you to count to 10 and the third asks you to ... well, I think you wrote the third one down wrong because showing only even or odd numbers will show them all
- unless you're supposed to prompt the user to enter whether or not he wishes to see evens or odds? That seems a bit more complicated than the other two which are simply asking you to understand loops.
Loops in programming are very similar to how you'd think of repetitive tasks in your life. For example, let's look at the first task. How would you do it on paper (without using computer code)?
1) Look to see how many times your name has been written
2) If your name has been written fewer than 10 times, write your name
3) Repeat
You could also express this as a sentence:
I will repeatedly write my name until I have written it 10 times.
By doing this, you have given yourself a task that must be repeated until a condition is met. Let's think of that sentence in another way:
While I have written my name fewer than 10 times, write my name.
So... you need a way to count how many times you've written your name, a way to write your name, a way to increment your counter, and a way to repeat those tasks.
We would start our counter at 0 because you haven't written your name yet. Now, you start your repeating loop. Is the counter less than 10? If yes, write your name and increment the counter. Do it again. Now the counter is 1. Is that less than 10? Yup. Write your name and increment the counter.
Now let's think of it in terms of code:
Initialize counter to 0.
While counter is less than 10:
* Write my name
* increment counter
I hope this helps, and allows you to follow along with your other 2 tasks.