Type of Loops what they do?

Status
Not open for further replies.

XinChao

Veteran
Veteran
Joined
Jun 11, 2012
Messages
117
Reaction score
3
Primarily Uses
For some reasons, I still not so clear with each type of loops. There are more than one type of loops and they all seem can perform the same thing. What I don't understand is the distinctive of each type?
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
for loops: when you know how many times you want to iterate. eg: for i = 1 to 5.

While loops: when you can loop infinitely cause you don't know how many times you're going to iterate.

Those are the basic loops.

Everything else is similar in logic but has their own purposes I guess (don't know their impact on low-level).

Do...While means you will do it at least once and then check if you want to continue looping.
 

Dr.Yami

。◕‿◕。
Developer
Joined
Mar 5, 2012
Messages
1,003
Reaction score
757
First Language
Vietnamese
Primarily Uses
Other
There is loop do too.
 

XinChao

Veteran
Veteran
Joined
Jun 11, 2012
Messages
117
Reaction score
3
Primarily Uses
for loops: when you know how many times you want to iterate. eg: for i = 1 to 5.

While loops: when you can loop infinitely cause you don't know how many times you're going to iterate.

Those are the basic loops.

Everything else is similar in logic but has their own purposes I guess (don't know their impact on low-level).

Do...While means you will do it at least once and then check if you want to continue looping.
A better explained with samples would be more helpful, pal.

I know For loop is to set specific values. For (i=0; i <=10; i ++)

what about For Each?

Do loops

While loops

any more of loop type?
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
http://www.tutorialspoint.com/ruby/ruby_loops.htm

You asked for the difference between different types of loops. Samples are available all over the internet, and there are people and organizations that spend days of their lives trying to write nice, simple articles to explain the same concepts.

I told you for loops and while loops are the essentials and if you understand how they're used then all other variations are clear when you go and read their descriptions.

Things like "until i < 3" are just, arguably, clearer ways to say "while not i < 3"

You'd find it easier to answer your own questions by picking up an IDE and actually looking at what the code does, based on some sample syntax, and learning ruby using ruby, not learning ruby based on some programming language you might have learned before (java, C, I don't know)

You learn how a tool works by using it and reading the reference manual.
 
Last edited by a moderator:

Dark Phoenix

Adventurer
Member
Joined
Mar 14, 2012
Messages
230
Reaction score
33
First Language
English
Primarily Uses
while - Repeats the code in the block until the condition passed to it becomes false. Probably the most common type of loop; just remember to actually change the condition in the loop somewhere, or the loop will run forever.

until - Opposite of while; more of a convenience option. Repeats the code until the condition passed to it becomes true.

for - If you're using something like C or Java, note that for loops in Ruby are more like foreaches; you use them to "iterate" over an array or a hash. For example...



Code:
numbers = (1...5).to_a
for num in numbers
    puts num
end
In this case, num would be set to 1, then 2, then 3, and so on, repeating for each entry in the array. Of course, you could always use each and a block instead.
 

IceDragon

Elder Cookie Dragon
Veteran
Joined
Mar 8, 2012
Messages
73
Reaction score
63
First Language
English
Primarily Uses
N/A
small correction:

loop isn't really a standard loop.

Its a hack in the Kernel, using a while loop:

NOTE: Not the actual code, but just the jist of it



Code:
module Kernel
  def loop
    while true do
      yield
    end
  end
end

loop do
  puts ":3 Herp derp"
  sleep 3.0
end
for invokes each on an object.

For example



Code:
module Woot
  def self.each
    10.times do yield rand(20) end
  end
end
for num in Woot
  puts num
end
:3
 

TheCastle

Veteran
Veteran
Joined
Apr 2, 2012
Messages
150
Reaction score
2
First Language
English
Primarily Uses
I have been trying to figure out how to make use of a looping function that would allow me to make it so tp generates in real time and monsters have individual timers during battle.

I attempted to make use of a while loop and it seems to make the game hang as though the loop never resolves. Not sure how to set this up to work like in the background rather than pause the entire battle script indefinitely...
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
A game loop is not the same as just running a while loop.

Each iteration you need to update the graphics (scene, sprites, etc), check for input, and other things. That is why you are given the illusion that things are actually happening. Otherwise how would you play an animation?

You will need to piggyback your code with the existing Update methods provided (or write your own update method, but I suspect you would just want to use what Scene_Battle already gives you)
 
Last edited by a moderator:

TheCastle

Veteran
Veteran
Joined
Apr 2, 2012
Messages
150
Reaction score
2
First Language
English
Primarily Uses
Hmmm do you mean call an event every second during battle or something to that effect? What existing update methods do I have at my disposal?
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Basically everything has an update method. You just need to figure out where you want to put the code that should be run everytime an update is called.
 

TheCastle

Veteran
Veteran
Joined
Apr 2, 2012
Messages
150
Reaction score
2
First Language
English
Primarily Uses
I understand that. My biggest challenge has been understanding the code that is currently in place so I know where to place my own loop that does everything I need while still satisfying the rest of the games functionality... If that makes sense...
 

BigEd781

undefined method 'stupid_title' found for nil:NilC
Veteran
Joined
Mar 1, 2012
Messages
940
Reaction score
304
First Language
Dothraki
Primarily Uses
N/A
Please do not hijack existing (and old) threads. Create a new thread with your question. Closed.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,072
Members
137,578
Latest member
JamesLightning
Top