Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Understanding for loops

A topic by William created Jan 19, 2022 Views: 175 Replies: 2
Viewing posts 1 to 3
(1 edit)

i have a couple questions about for loops that i’m not quite sure is correct, this is using c# as well. 

for (int i =1; i<100; i+1){

Console.Write(i);

}

i is declared as 1, so does it check if the declared variable is less than 100 and if that’s true it will then add 1 until it gets to 100?

A for loop is executed like so:

  1. Execute the first expression. In this case that declares a variable called i and sets its value to one.
  2. Execute the second expression. If it evaluates to true, jump to the inside of the loop.
  3. After the body of the loop has completed, execute the final expression. In this case that adds one to i.
  4. Jump back to step 2 and rinse and repeat until the second expression evaluates to false.

In short, all a foo loop does is iterate until the second expression evaluates to false (in this case, if i is greater than or equal to 100), which is in this case caused by adding one onto i.

(+1)

ah thanks for the explanation, i understand how they work now. 

Moderator moved this topic to General Development