In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the structure of the C language cycle, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand.
Break statement
Break statements are used for switch branch statements (as described in previous articles) and for various loop statements. In the switch statement, break represents the meaning of termination, and when you encounter break, it means to terminate the current switch statement; in the loop statement, the function of break is to jump out of the current loop.
Continue statement
Continue statements are used only in loop statements. In the execution of the loop body statement, continue is used to end this loop and no longer execute the program after the loop, but directly proceed to the next loop. It is always subject to conditional judgment and execution.
C language loop structure 1, goto statement (now generally rarely used)
1. Statement introduction:
The C language provides goto statements and tag jump symbols that can be misused at will. Generally speaking, it is easy to write code without goto statements in practice, but it can be used in some situations. The most commonly used method is to terminate the program in some deeply nested structures, such as using it to jump out of a multi-layer loop at one time. (in general, if break fails to achieve its purpose, it will be realized by using goto statements.)
two。 Grammatical structure:
The following is the syntax structure of the goto statement, which contains the tag jump symbol and the goto statement; when the code runs from top to bottom, it will jump to ko when it encounters goto ko, which is the goto statement, which plays the role of a direct jump.
Int main () {ko: goto ko; return 0;} 3.goto statement program example:
Computer shutdown instruction program: this program uses goto statements to jump to achieve the correct input of instructions.
# include#include//system function call header file # include//strcmp function call header file int main () {char arr [] = {0}; system ("shutdown-s-t 60"); / / computer shutdown command ko: printf ("your computer is about to shut down, please enter:" stop operation ", computer shutdown will be terminated\ n"); scanf ("% s", arr) If (strcmp (arr, "stop operation")) / / determine the input instruction {system ("shutdown-a"); / / cancel the computer shutdown command} else {goto ko;//goto jump statement} return 0;} II. Do-while statement
1. Statement introduction:
The characteristic of do-while loop statement is to execute first and then judge, first execute the loop body unconditionally, and then judge according to the control expression, if the judgment is true, then continue to execute the loop; otherwise, the loop ends. So the do-while statement must execute a loop at least once.
two。 Grammatical structure:
The following is the syntax structure of the do-while loop statement, execute first and then loop, at least once.
Int main () {do {loop statement;} while (expression); / / the semicolon here must not be less, pay attention! Return 0;} 3.do-while flowchart:
Example of a 4.do-while statement program:
Simple guessing number game program
# includeint main () {int n = 0; int a = 8; do {printf ("Please enter 0-10 guessing numbers\ n"); scanf ("% d", & n);} while (n! = a); printf ("guessed!\ n"); return 0;} III, while statement
1. Statement introduction:
The while statement is judged before it is executed. First, it determines the control expression, and if it is true, it continues to execute downwards; if it is false, the loop ends.
two。 Grammatical structure:
If there is only one loop statement in the loop body, it can be enclosed without parentheses {}; if the loop statement is multiple, it must be enclosed in parentheses {}.
Int main () {while (expression) {Loop statement;} return 0;} 3.while Loop flowchart:
Example of a 4.while statement program:
Calculating the minimum common multiple and the maximum common divisor by toss and turn division
# includeint main () / / toss and turn division method to find the minimum common multiple and the maximum common divisor {int m = 12, n = 16; int x = 0, y = 0; int z = 0; x = m, y = n; while (m! = 0) / / while cycle {z = n% m; n = m M = z;} printf ("least common multiple:% d\ nmaximum common divisor:% d", n, x * y / n); return 0;} 4. For loop statement:
1. Statement introduction:
For loop is the most commonly used loop statement in C language, and its format is: for (expression 1; expression 2; expression 3). Expression 1 is the initial assignment of cyclic variable, expression 2 is cyclic condition, and expression 3 is cyclic variable increment. First of all, assign a value to the variable, and then judge the variable: if the judgment is true, continue the cycle; otherwise, the cycle stops, and finally the variable increases or decreases.
two。 Grammatical structure:
It is written in the same way as while statements. If there are multiple statements in the body of the loop, they are enclosed in parentheses to form a code block.
Int main () {for (expression 1; expression 2; expression 3) {Loop statement;} return 0;} 3.for Loop statement flowchart
Example of a 4.for loop program:
An odd program that outputs 1-10
# includeint main () {int I = 1; for (I = 1; I
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: 0
*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.