7
for (int i = 0; i < palavra.Length; i++)
What does each word mean? And how each part of that code works?
7
for (int i = 0; i < palavra.Length; i++)
What does each word mean? And how each part of that code works?
15
This is the beginning of a repeat loop with an initialization, a condition that indicates the ending of the loop and a step that must be executed in each interaction.
for ()
It is the keyword that indicates to the compiler that it is a repeat loop in this format described above. It must have the 3 parts in parentheses
int i = 0;
Here I imagine you already know, you are declaring a variable of the whole type called i
and assigning 0 to it. This occurs only once at the beginning. This variable will only exist within the for
.
i < palavra.Length;
Here is the condition, like I would put in a if
. As long as that expression results in true
the loop will continue executing your code block in successive repetitions. In this case the condition checks whether i
is worth less than the variable size palavra
which should be a data collection (array, or List
, or string
, most likely, or something like that). When you hit the value of i
with the size, it means it went through the entire data collection.
i++
I put in the Github for future reference.
Here is incrementing the variable by 1 i
, which is the most common action of a for
, You can use any action that makes sense in there. Then at each step of the loop the variable will have its value changed with the next unit. Probably within it the variable will be used to access the specific element of the data collection. The scan will be complete as it starts at 0 and ends at its size.
Read more:
Thank you very much helped thank you.
Browser other questions tagged c# syntax for
You are not signed in. Login or sign up in order to post.
for
: repeat loop, used to repeat a code snippet more than 1 time.int i = 0
: statement of an entire variable.i < palavra.legth
: will go from 0 to word size.i++
: increment 1 to i– Francisco
https://www.dotnetperls.com/for
– Rovann Linhalis
https://answall.com/q/158379/64969
– Jefferson Quesado
For the record, I voted "broad" despite the closing as "unclear". For those who do not know the system of the site, the closing vote is "generalized" in the end.
– Bacco
@Bacco Why "broad"?
– ramaral
@ramaral for dealing with several concepts in the same code snippet (repetition structure, variable declaration, scope, increment, string method, just to start). Note that I am not against the author receiving the proper explanation, but I do not understand that it should be the type of "open" question on the site, due to lack of specificity and focus. I understand that it has already been resolved with the "summary" of the bigown (for the author to understand more deeply would have to detail a thousand more things). What could be the case of a closure for custom reason instead of a "broad", but I went the economic way.
– Bacco
@ramaral note that the title was altered afterward of closing , and I don’t even know if it really represents the author’s original intention. Including pq there I think it can even be duplicated
– Bacco
@Bacco I don’t totally agree with you, but this issue of editing, really it does not present the author’s intention. It should be reversed.
– Francisco
@Bacco Thanks, I hadn’t noticed the edition.
– ramaral