-
For Loop Code
Construct a form with two buttons, labelled PLUS and MINUS respectively, which will display the number 1 to 10 with the + or - signs as appropriate using the for loop statement.
For Loop Statement Exercise:
Press PLUS to display +1 to +10: PLUS Button
Press MINUS to display -1 to -10: MINUS Button
(TIP) Specify two JavaScript functions and use the onClick event handler to call the appropriate function when a button is clicked. Display the results in a JavaScript alert box.
Yes I am really struggling with things like this and the mini course I'm on is just blowing my mind at the moment, any help would be greatly appreciated.
I also need the same code using the While loop function... however I don't know how I would change this, so any tips or pointers would be great!
-
-
Have you made any of this?
-
-
Not any of the coding that is needed because I am struggling to get the concept of the loop statements. I have only do one code with them before and it was much different to this :/
-
-
for loops work like this:
for (START VALUE; LIMIT; ACTION) { functions inside the loop }
You can copy the format of a for loop with a while loop like this:
START VALUE
while (LIMIT) {
functions inside the loop;
ACTION;
}
Because of this, for() loops are basically useless, except that they make your code a little cleaner if you want that type of loop.
While loops are good for vague general loops. For loops are good for when you are counting specifically (but while loops can also be used). For loops wouldn't work so well if you aren't using numbers/counting (in many cases).
(But I do believe that for loops can actually omit two of the three statements to work just like a while loop, so that would work out ok.)
START VALUE above describes how you set a variable to start the process, so you can track it. For example, if you are counting to five, you might set x=5, then subtract, or x=0 and add up to five.
LIMIT is when the loop stops. You could use x>0 or x<5, or another function like isacolor(x), or whatever you want. If the LIMIT returns FALSE then the loop will stop (or not start if it is the first time through). Be careful to set it correctly or you will end up with an infinite loop that never stops and crashes the program. LIMIT can be thought of as "if (LIMIT) go, else stop".
ACTION is what is done each time the loop goes, but specifically used for the tracking variable. So again with counting to 5, you will just say x=x+1 or x=x-1, along with the LIMIT and START VALUES given above. Technically this is no different than an action that takes place inside the loop (which is why you can substitute it in a while loop inside the loop), but it creates a convenient way to separate elements and then have cleaner code that is easier to manage.
Here's an example:
for(x=1;x<3;x=x+1) {
print 'hi';
}
That will print 'hihi'-- going through the loop twice, then stopping.
The syntax will vary slightly depending on the language you are using, but the general layout will be the same in most (all?) languages.
Daniel -
Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
-
The Following User Says Thank You to djr33 For This Useful Post:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks