Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Incrementing a value through a loop
Old
rahbee
Guest
 
Status:
Posts: n/a
Default Incrementing a value through a loop - 10-11-2009, 03:02 PM

.

Last edited by rahbee; 10-14-2009 at 09:29 AM.
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 10-11-2009, 04:59 PM

It's not so hard if you work with it for awhile. As for your question, you could make a code block that looks like this:

variables needed:
-userNum: the number of times the user wants to have the penguin jump (entered by user at prompt)
-jumpHeight: number variable that starts out as 1

Loop (userNum) times
penguin jump (jumpHeight) meters
increment jumpHeight by 1

The increment command can be found by dragging the variable from the properties window onto the script area of Alice (a popup menu opens up with increment as an option).
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 10-11-2009, 07:35 PM

Yes, that's the way to do it if you want it to increase by more than one. Good job figuring it out.
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 10-04-2011, 04:55 PM

A window opens and asks the user how many times they want to perform the next method, the amount is then stored in a variable.

How would I make my program, after the the next "method" has been run to repeat the same method the amount of times the user entered at the first window?
   
Reply With Quote
Old
Mr Kidnapper
Senior Member
 
Status: Offline
Posts: 442
Join Date: Feb 2011
Default 10-04-2011, 06:47 PM

You can insert a number variable into the loop # times section, and that variable can be changed when you use the "ask user for a number" function.
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 10-04-2011, 07:46 PM

Quote:
Originally Posted by Mr Kidnapper View Post
You can insert a number variable into the loop # times section, and that variable can be changed when you use the "ask user for a number" function.


Sorry, should have mentioned that it needs to go in a while loop and not a for loop. Pity because what you suggested would have solved my problem : (
   
Reply With Quote
Old
Mr Kidnapper
Senior Member
 
Status: Offline
Posts: 442
Join Date: Feb 2011
Default 10-04-2011, 10:09 PM

I have no idea why you would want to loop in a while when all you need to do is ask the user how many times they want to loop and store it in a variable, but this is how "while" loops are done.

While ( both ( world.booleanVariable == true ) and ( world.numberVariable <= 2 ) )
increment world.numberVariableby 1

And in Java,
boolean Vars1;
int Vars2=0;
int Vars3=3;
while ( Vars1 == false && Vars2 <= Vars3 ){
Vars2++;
};
Simply put, a while loop can ask if a variable is true or if it has not reached a certain number yet, in this case 2. It is used to make very complicated loops that require you satisfy many conditions first. At the end of a while function, or in any part of the function really, Vars2++ is used to increment the number, currently at 0, by 1. This in particular make the while loop a total of 3 times, assuming Vars1 is true. If Vars1 is not true, it will loop to infinity or until it becomes true.
I really don't see why you can't just use the regular loop for this.

Last edited by Mr Kidnapper; 10-04-2011 at 10:13 PM.
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 10-05-2011, 06:44 PM

Quote:
Originally Posted by Mr Kidnapper View Post
I have no idea why you would want to loop in a while when all you need to do is ask the user how many times they want to loop and store it in a variable, but this is how "while" loops are done.

While ( both ( world.booleanVariable == true ) and ( world.numberVariable <= 2 ) )
increment world.numberVariableby 1

And in Java,
boolean Vars1;
int Vars2=0;
int Vars3=3;
while ( Vars1 == false && Vars2 <= Vars3 ){
Vars2++;
};
Simply put, a while loop can ask if a variable is true or if it has not reached a certain number yet, in this case 2. It is used to make very complicated loops that require you satisfy many conditions first. At the end of a while function, or in any part of the function really, Vars2++ is used to increment the number, currently at 0, by 1. This in particular make the while loop a total of 3 times, assuming Vars1 is true. If Vars1 is not true, it will loop to infinity or until it becomes true.
I really don't see why you can't just use the regular loop for this.
Thanks for the help!

I had 2 or 3 conditions before the while loop that needed to be met to process the while loop i was talking about.

Didnt end up needing to increment, just got my variable to + 1 while the user number input was <=0 then added the next method. (this was added to an "else" part of an if statement that was part of the prior conditions that needed to be met.)

Works beautifully now : )
   
Reply With Quote
Old
gilhritrobin
Junior Member
 
Status: Offline
Posts: 5
Join Date: Oct 2011
Default 10-09-2011, 03:07 AM

Good job figuring it out.

   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2019, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.