For loop

For loop

For loops are iterations that will only ever go through the code inside the loop if the condition for the loop  is true(unlike do-while). There are four parts of a for loop. Those parts are setting a variable, a conditional statement(normally with the variable you set) and some change in the variable which is always an int at least from when I have used it and the code inside the loop (the code that is repeated).

A for loop is formatted as shown:

for(int i = somenumber; i  >somenum; i++)

{//if more than one line it needs brackets

<code in loop>

}

So the int doesn’t need to be there if you’ve declared i already and you want to declare i outside the for loop if you are going to use it at some point outside the for loop because otherwise it is out of its scope since the for loop counts as brackets, the conditional operator > can be replaced with any other conditional operator, and ++ can be replaced with anything like –, +2, -6, *2, /5(remember that it’s a int though) , or % 9.

One thought on “For loop

Comments are closed.