while() function

Non-platform specific questions.

while() function

Postby Hblade » Wed Apr 04, 2012 8:17 pm

How does this function work?

Code: Select all
while(var)
{
?
}
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: while() function

Postby skydereign » Wed Apr 04, 2012 8:27 pm

It's a loop mechanism.
Code: Select all
while(condition is true)
{
    // do something
}

So if you wanted to simulate a for loop you could do it like this.
Code: Select all
int i=0;
while(i<10)
{
    // do something
    i++;
}

Just make sure the while loop is escapable. There is also do while, which runs the code first, and then checks if it needs to loop.
Code: Select all
do
{
    // do something
}while(condition);
// notice, you need the semicolon after this while (but not the other)
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: while() function

Postby Game A Gogo » Wed Apr 04, 2012 8:30 pm

there are two varients. The normal while

Code: Select all
while(Conditional Statement)
{
    do things here while the Conditional Statement is true
}


and the do-while

Code: Select all
do
{
    do things here
}while(Conditional Statement)


The first while loop will ONLY run if Conditional Statement is true, but if the condition is not met, the code won't get run at all.
If the Conditional Statement is true, then the code will keep on running until the Condition Statement returns false.

The do-while loop is the same thing, except that the code will get run at least once!

EDIT:
Aww, sky beat me to it xP
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: while() function

Postby Hblade » Wed Apr 04, 2012 8:37 pm

Thanks guys :) now does this happen all in 1 frame, like using for(i=0;ect..)?
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: while() function

Postby skydereign » Wed Apr 04, 2012 8:40 pm

Yeah. Any code in any event must finish before the game can move on. So, if you had an infinite while loop, the game would freeze.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: while() function

Postby Hblade » Thu Apr 05, 2012 4:10 am

Oh I see. Thanks :3
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest