Thinking about it, besides when reading
sometimes I also get lucky and have a few ideas
so I’ll open another category to record them~
I hope there will be as many posts under this category as possible -.-
There’s a personal contest tonight
I feel a bit nervous…
What if I score zero? -_-
I feel I’ll come back and edit this once tonight
Let me set a flag first
【Follow-up Discussion on Defining Variables in for Loops】
Last time I read in the book about the issue of variables defined in for loops1 [【Book Reading】Variables Defined in for Loops](/content/post/note/competitive-programming/6/算法竞赛入门经典(6) – for循环中的定义变量)
Combining recent practical experience and some tests I’ll add some supplementary notes here
Limitations of for(int i=0 ; i<n ; i++)
I mentioned earlier
Tip 2-4 suggests shortening the scope of variable definitions as much as possible. For example, define the loop variable in the initialization part of the for loop.
Together with my own habits, these days I’ve been defining loop variables as I use them whenever possible
PS: E-language’s count-loop header (count, n) is also this directly written pattern (though it actually still needs to be defined first—but I’ve developed the bad habit of letting it error out and then defining it directly)
But sometimes some problems appeared For example, the most memorable one involves break; inside a loop For example, there was a primality test method at the time:
| |
Then the for loop cannot be written asfor(int i=2;i<=sqrt(m);i++)
otherwise, after breaking out of the loop, there is no variable i
so if i is not defined before the for loopif(i>k)
it will report an error
and if it is defined
it is equivalent to not assigning a value to i
so you will get a wrong result
It can be seen that for(int i=0 ; i<n ; i++) also cannot be abused
Now for this kind of situation, I generally take the approach of
For cases that need break and then check the loop variable
still defining it before the for loop, and then
writing the for loop as for(i=0 ; i<n ; i++)
Or use a flag marker
For example, for the above primality test
it can be written as:
| |
This is relatively troublesome
but for some cases it is still quite easy to understand
After all, you still only need to define loop variables as you use them
before the loop you only need int flag
It should be noted that
flag must be defined before the loop
defining it inside the loop likewise makes it impossible to call after the loop ends
This leads to the next part of the discussion
namely the scope of variables defined in the loop body
Scope of int i in Braces Such as while
As already known
For a user-defined function
| |
(only to illustrate the issue of defining variables in functions)
(this function can be written directly as)
| |
To avoid OCD, I’ll mention it again
The variable c defined inside it
cannot be accessed by other functions
(including the main function)
And the function statements are enclosed by { }
Combining the above discussion
there is a guess:
Variables defined inside { } braces can only be used inside the braces
This is the case for a function’s { }
So let’s discuss another place that uses braces { }
while(){
}

In fact, I found that it does indeed report an error It can be seen that the above guess holds for while
Thinking about it there are actually other things with braces For example if(){ } So I did another test

The result proves that it also matches the guess
So we can tentatively conclude
Variables defined inside { } braces can only be used inside the braces
Algorithms Are a Way of Thinking
There are also some thoughts about algorithms… but regarding this Thinking about it “===Programming Design Soul==Algorithms===” there are no articles under the category yet so I’ll write this part there~ Hahahahaha
So if there is such a part, I’ll also start a new article for it-.-
This one is getting a bit long
Never mind, I’ll just write it directly here
Contest link: HPU Grade 15 Personal Contest - Virtual Judge
Uh… I don’t want to talk about the result
I just still need to firm up my own ideas
and refine my own ideas
In addition… seriously carry out my own ideas
Previous article: [【Book Reading】Variables Defined in for Loops](/content/post/note/competitive-programming/6/算法竞赛入门经典(6) – for循环中的定义变量) ↩︎