answersLogoWhite

0

How does a FOR loop work in GML?

Updated: 9/13/2023
User Avatar

Wiki User

14y ago

Best Answer

FOR loops work as follows:

{

for( [initialize a variable]; [expression]; [increment the variable] ) {

//Do this code

}

}

Here as an example of a FOR loop:

{

for(i = 1; i < 10; i += 1) {

show_message(string(i));

}

}

What this will do is show a message 10 times displaying the value of "i" so you would get a message that says "1," another one after that saying "2," etc... The way this works is that you have the variable "i" initialized in the FOR loop. The FOR loop will keep looping until i >= 10, because the middle statement dictates that i must be smaller than 10 for the FOR loop activate. The third statement in the for loop is the statement that you increment the i variable with. If you change i += 1 to i -= 1 then the FOR loop would go on forever, freezing the game. This is a critical mistake to make when constructing a FOR loop (as is with any loop.)

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does a FOR loop work in GML?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Basic Math

How does a WHILE loop work in GML?

The while loop works as follows:{while( [expression is true] ) {//Do this code}}The while loop re-runs until the expression contained within the parentheses is false. Take a look at this example:{while(!place_meeting(x,y,obj_ground)) {y += 1;}}This while loop tells the object to move down one pixel until it collides with obj_ground. Unfortunately, nothing guarantees that this loop will not run forever. Always make sure that when you construct a while loop that you make sure that it does not run forever. Take a look at this whileloop:{while(obj_ball.y < y) {draw_sprite(sprite_index,0,x,y);}} This while loop will run for ever. Why? It does not have any statements that insure that the while loop aborts. Again, Always make sure that when you construct a loop that you put statements in the loop that will eventually abort the loop. y -= 1; is the statement in this new while loop that eventually aborts the loop:{while(obj_ball.y < y) {draw_sprite(sprite_index,0,x,y); y -= 1;}}


When a VBScript loop control variable is not altered during loop execution what kind of loop may result?

An infinite loop.


What are the differences between a condition-controlled loop and a count-controlled loop?

A Condition-Controlled loop keeps going until a certain condition is met, like say the user clicks a button, or the world ends or something. A Counter controlled loop keeps going until it has run a certain number of times. For example if you create a variable x=0. And then every time your look runs you increase x by 1 (x=x+1), you can tell your loop to keep running until x=5. That way the loop would run 5 times until the *COUNTER* reaches 5. This would be a counter controlled loop


What is the purpose of wend statement in while loop in gw-basic?

While--wend statement is used to execute a loop until a given condition is true.if the condition is false the loop ends and the program continous to the line following eend.


What are the purpose of test condition in a loop statement in qbasic?

The purpose of using a 'test condition' inside of a loop statement/which is also called a 'conditional loop'; is to make the loop STOP counting. Otherwise, you would have gone and created what is known as being called an 'endless loop'; which just keeps on running really quite endlessly, forevermore...! To give a quick example, let's compare... CONDITIONAL LOOP EXAMPLE num% = 0 DO num% = num% + 1 PRINT num%; LOOP UNTIL num% = 3 END Output... 1 2 3 Press any key to continue... Here, when the LOOP's test condition is met: (num%=3); then, the loop stops counting up any further; the DO/LOOP block structure is broken out of; and, the final END statement will get executed. UNCONDITIONAL LOOP EXAMPLE Next, let's try re-writing the same above program; by removing the conditional statement part of the LOOP which says: (UNTIL num%=3)... num% = 0 DO num% = num% + 1 PRINT num% LOOP END Output... 1 2 3 4 5 6 7 8 9 10 11 12 .... ...because there is no conditional test statement to make the loop stop repeating itself; therefore, it just keeps on endlessly counting upwards, instead; the program never breaks out of the DO/LOOP block; and, therefore never gets to reach the final END statement. To make an 'unconditional loop' stop repeating itself inside of QBASIC IDE/Integrated Development Environment; then, use combination key press: [CTRL] + [BREAK]; and, this should make an 'endless loop' stop repeating itself any further; and, return you straight back to the Editor Screen where you can futher change/edit your code.

Related questions

How is a while loop different from a do until loop in gml?

In any programming language, a "while" loop and a "do until" loop are the same except for 1 difference. In order to enter a while loop, the condition must always be true. But in a do until loop, if the condition was false, the block of code inside the loop will always be ran at least once. Example: while (false) { // code here } in this example, the code inside the while loop will never run, but in the following example: do { //code here } until(false) although the condition is false, the code will be run 1 single time and the exists the loop.


What is the percent error if a measured value is 24.59 gmL and the accepted value is 25.49 gmL SCH1.4.1?

3.531%


How does a WHILE loop work in GML?

The while loop works as follows:{while( [expression is true] ) {//Do this code}}The while loop re-runs until the expression contained within the parentheses is false. Take a look at this example:{while(!place_meeting(x,y,obj_ground)) {y += 1;}}This while loop tells the object to move down one pixel until it collides with obj_ground. Unfortunately, nothing guarantees that this loop will not run forever. Always make sure that when you construct a while loop that you make sure that it does not run forever. Take a look at this whileloop:{while(obj_ball.y < y) {draw_sprite(sprite_index,0,x,y);}} This while loop will run for ever. Why? It does not have any statements that insure that the while loop aborts. Again, Always make sure that when you construct a loop that you put statements in the loop that will eventually abort the loop. y -= 1; is the statement in this new while loop that eventually aborts the loop:{while(obj_ball.y < y) {draw_sprite(sprite_index,0,x,y); y -= 1;}}


How do you use gml?

Go to yoyogames.com, they have a bunch of tutorials. You could also go to, gmc.yoyogames.com. It's the forum of gml game makers.


How is the density of an object in kgl related to its density in gml?

If kgl is kg/L and gml is g/mL these densities are equal.


What is the difference between GML Geography mark-Up Language and G-XML?

GML is a standard of the OGC and ISO (ISO 19136). G-XML is a standard of the Japanese Standards Association (JSA). Both standards are based on XML for the encoding of geographic information, however, G-XML is focused on location based services. G-XML is written in GML as a GML application schema.


Gold has a density of 19.3 gml sand has a density of 2 gml which is heavier?

Depends on quantity. But assuming you have the same volume of both then Gold is definitely heavier.


Density of lead in gml?

11.34 g/mL


What is the density of copper gml?

8.92 g/mL


How do you abbreviate General McLean?

Gen. McLean


Is there a real programming language that resembles GML - the language in Game Maker?

GML IS a real programming language. But anyway, all programming languages are about the same, with different syntax.


What liquids will be the unkown water 9982gmL toluene 8669 gmL clorofomo 14832 gmL ethanol 0789 gmL?

the correct density numbers are the following water= 0.9982 g/mL Toluene= .8669 g/mL Clorofomo 1.4832 g/mL Ethanol= .789 g/mL