Post: [C# +] - The For Loop
09-03-2011, 07:12 PM #1
Correy
I'm the Original
(adsbygoogle = window.adsbygoogle || []).push({}); The for loop/ statement counts from one number to another number. You start a for loop with the for keyword which is followed by brackets. The first section inside the brackets is where you set the starting value of the loop counter variable. The second section is the loop end condition. The third is where the loop counter is incremented. The code to run for the loop goes after the brackets.

the explanation of the for statement
    
[COLOR="#0000CD"]for ([/COLOR] for-initializeropt [COLOR="#0000CD"];[/COLOR] for-conditionopt [COLOR="#0000CD"];[/COLOR] for-iteratoropt [COLOR="#0000CD"])[/COLOR]


a basic for loop looks something like this.
    
[COLOR="#0000CD"]for (int[/COLOR] i [COLOR="#0000CD"]=[/COLOR] 0; i [COLOR="#0000CD"]<=[/COLOR] 10; i[COLOR="#0000CD"]++[/COLOR] [COLOR="#0000CD"])[/COLOR]


this basically means, this.
you declare the int 'i' as a statement, this means you are inputting the value of 'i', as you can see we have set the value of that with the 'i = 0' so that declares the 'i' represents the value 0.

for the next statement
    
i [COLOR="#0000CD"]<=[/COLOR] 10[COLOR="#0000CD"];[/COLOR]

this statement means the when 'i' is lower or equal to '10' the statement will stop looping, which takes you to the next statement.

    
i[COLOR="#0000CD"]++;[/COLOR]

this part means, if the statement before is false.. other words 'i' Is Not higher or equal to '10' it will add '1' value to 'i'
so, for every loop take place if 'i' is smaller than '10' it will add '1' until it reaches '10'

so, here's the for loop put together to make a function.
    
[COLOR="#0000CD"]for (int[/COLOR] i [COLOR="#0000CD"]=[/COLOR] 0[COLOR="#0000CD"];[/COLOR] i [COLOR="#0000CD"]<=[/COLOR] 10[COLOR="#0000CD"];[/COLOR] i[COLOR="#0000CD"]++)[/COLOR]
[COLOR="#0000CD"]{[/COLOR]
console.Write(i);
[COLOR="#0000CD"]}[/COLOR]


this will then make a console application write separate lines, from '1' to '10', when it reaches '10' it will stop looping until that specific function is called again.
you can change the 'i<=10' to something else, use different symbols to make it loop if it's lower or if it's higher.
you can also set the value of 'int i' to anything you want, this is just the starting point of 'i'

but, if you wanted to make a continuous loop which does not stop you simple add no functions in the for statement.
like this
    
[COLOR="#0000CD"]for([/COLOR];;[COLOR="#0000CD"])[/COLOR]
{
[COLOR="#00cc00"]//Function Here[/COLOR]
}


this will not end until it is returned or ended.
Last edited by Correy ; 09-03-2011 at 09:24 PM.

The following 2 users say thank you to Correy for this useful post:

Docko412, Pichu
09-03-2011, 08:40 PM #2
Pichu
RIP PICHU.
Nicely written. Smile

Maybe add color as it would be in the program for those who are learning to get an idea of what should be what, to help enhance visual learning.
09-03-2011, 09:17 PM #3
Correy
I'm the Original
Originally posted by Sublimity View Post
Nicely written. Smile

Maybe add color as it would be in the program for those who are learning to get an idea of what should be what, to help enhance visual learning.


thanks for the feedback, i'll colour code the variables Smile
09-03-2011, 09:45 PM #4
Pichu
RIP PICHU.
Originally posted by Correy View Post
thanks for the feedback, i'll colour code the variables Smile


Much better and easier to see. You should really consider making a thread dedicated to little tutorials such as this. I'd foresee a sticky if that was so.
09-03-2011, 09:48 PM #5
Correy
I'm the Original
Originally posted by Sublimity View Post
Much better and easier to see. You should really consider making a thread dedicated to little tutorials such as this. I'd foresee a sticky if that was so.


i was thinking of making an ultimate C# thread but, it takes soo long :confused:
09-04-2011, 03:34 AM #6
Originally posted by Correy View Post
i was thinking of making an ultimate C# thread but, it takes soo long :confused:

well i hope you do. there aren't that many really good tutorials on c# and you seem like you could write a great one
Happy
09-04-2011, 05:53 AM #7
Pichu
RIP PICHU.
Originally posted by Correy View Post
i was thinking of making an ultimate C# thread but, it takes soo long :confused:


I think I'll do one for Message Boxes for if you choose Yes, No, or Maybe then allow for a command to take place if said is done.

Maybe tomorrow. Will help for those that don't know since it is quite so different in C# to VB.Net. Smile
09-04-2011, 01:23 PM #8
Correy
I'm the Original
Originally posted by Sublimity View Post
I think I'll do one for Message Boxes for if you choose Yes, No, or Maybe then allow for a command to take place if said is done.

Maybe tomorrow. Will help for those that don't know since it is quite so different in C# to VB.Net. Smile


oh, you mean dialog results Winky Winky ?

---------- Post added at 02:23 PM ---------- Previous post was at 02:22 PM ----------

Originally posted by D3ss3rtPr0ducts View Post
well i hope you do. there aren't that many really good tutorials on c# and you seem like you could write a great one
Happy


i might just do like a daft notepad .txt file and update it when i can be bothered Smile
09-04-2011, 06:10 PM #9
Pichu
RIP PICHU.
Originally posted by Correy View Post
oh, you mean dialog results Winky Winky ?

Well yea. :P
09-04-2011, 07:03 PM #10
Correy
I'm the Original
Originally posted by Sublimity View Post
Originally posted by Correy View Post
oh, you mean dialog results Winky Winky ?

Well yea. :P


they are easy ha, i think everyone knows that :confused:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo