In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the C++ For cycle execution sequence process". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the C++ For cycle execution sequence flow".
The for loop allows you to write a repetitive control structure that executes a specific number of loops.
Grammar
The syntax of the for loop in C++:
For (init; condition; increment) {statement (s);}
The following is the control flow of the for loop:
Init will be executed first and only once. This step allows you to declare and initialize any loop control variables. You can also not write any statements here, as long as a semicolon appears.
Next, the condition is judged. If true, the loop body is executed. If false, the loop body is not executed, and the control flow jumps to the next statement immediately following the for loop.
After the body of the for loop is executed, the control flow jumps back to the above increment statement. This statement allows you to update loop control variables. This statement can be left blank as long as a semicolon appears after the condition.
The conditions are judged again. If true, the loop is executed, and the process is repeated over and over again (loop body, then increase the step value, and then re-determine the condition). The for loop terminates when the condition becomes false.
Flow chart
Case analysis one
The following for loop executes the code
It's more basic, so draw a picture to explain the steps of for loop execution.
1. Execute the first parameter in the body of the for loop, that is, define an int integer variable named I and initialize it to 0
two。 After executing the code in the first parameter, the second parameter is executed to determine whether I is less than 99
3. The code in the body of the loop is executed when the second expression is True, that is, when I is not equal to 99
4. After executing the code in the body of the loop, the third parameter of the for loop is + + I
When the above process is completed, the first parameter of the for loop will not be executed when entering the non-first execution loop.
Instead, directly execute the judgment expression in the second parameter.
And then execute the code in the loop body.
Then execute the third parameter of the for loop
The loop will not end until parameter 2: I > 99
IMagn98 is the last cycle, (iMagnum pedigree has no difference in the for cycle, I don't know why, but the structure is the same anyway), this is the second step iLead 99 will perform, do not meet the conditions, jump out of the loop body, if the final result of printing iQuery in the loop is 98 (99 times since 0, 98 times since 1,), if the final result of printing iQuery outside the loop is 99, it's about like this
The execution order of the for loop (detailed explanation of examples)
Instance code
# include # include int main (void) {char ctens for (c=getchar (); getchar ()! ='#'; c=getchar ()) putchar (c);}
The running results are as follows:
How does this work?
The first thing to be clear about is the order in which for loop statements are executed
Let's start with a simple example.
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 207
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.