Control flow statements in program languages are used to control the order in which each calculation operation executes. The control Flow structure is: (1) sequential structure (2) branch structure (3) loop structure
The If...else statement is a classic branching structure control statement, and the same "switch (expression) ... case constant expression" statement can also be used for branch processing. An expression can be any expression, and a constant expression can be any constant expression. The case branch generally contains multiple statements and does not have to be enclosed in curly braces, because each case branch gives only the starting position of the statement execution in the switch branch structure and does not determine the terminating position. The end of the switch branch structure relies on the switch branch structure ending closing brace or break statement.
1 /*file name: switch.c2 * Description: switch (expression) ... case (constant expression)3 * Medium:4 * An expression can be any expression5 * Constant expression must be a constant expression, as in this routine will cause compilation error6 *7 * switch.c: In function ' main ':8 * Switch.c:8:2: Error: Case label cannot be reverted to an integer constant9 * Case A + 1:printf ("A + 1 is%c\n", a + 1);Ten * ^ One * Switch.c:10:2: Error: Case label cannot be reverted to an integer constant A * Case A + 1:printf ("A + 2 is%c\n", A + 2); - * ^ - */ the#include <stdio.h> - - Main () - { + CharA =0; - + Switch(a) { A CaseA +1: printf ("A + 1 is%c\n", A +1);//error: ' A ' cannot appear in a constant expression at Break; - CaseA +1: printf ("A + 2 is%c\n", A +2);//error: ' A ' cannot appear in a constant expression - Break; - default: -printf"default\n"); - Break; in } - to +}
The For statement is a typical circular structure control statement. The For statement can be used to cycle through the process of changing a set of numbers, which at any time represents a value in the change.
Note:
In the loop we can use continue; The statement jumps directly to the loop body closing brace, so that she actually realizes the purpose of allowing the loop to go directly to the next round of loops.
A break can be used in a loop, and the statement directly smashes the loop and kills the loop.
1 /*2 * Control flow statements in programming languages are used to control the order in which each calculation operation executes. The control Flow structure is: (1) sequential structure (2) branch structure (3) loop structure3 4 * While and DO...WHILE loop structure control statements are more general than for loop structure control statements5 * The following is a while routine6 */7#include <stdio.h>8 Main ()9 {Ten intShu_zi =0, Ge_shu =0; Oneprintf"Input an integer:"); Ascanf"%d", &Shu_zi); - /* - While (Shu_zi) {//This loop involves the execution order of the statements (we use {} to represent all the statements of the loop body): [Shu_zi, { }] [Shu_zi, {}] [Shu_zi, {}] ... the Shu_zi/=; - ge_shu++; - } - + */ - + Do{//Compared to the while loop in the above comment, the loop avoids the embarrassment of a program calculation error when the user enters an integer 0 AShu_zi/=Ten; atge_shu++; -} while(Shu_zi);//the order of execution of the statements involved in this loop can be seen as (we use {} to represent all statements of the loop body): [{}, Shu_zi] [ {}, Shu_zi] [{}, Shu_zi] ... -printf"Integer%d%d bits \ n", Shu_zi, Ge_shu); -}
1 /*2 * Cycle of Death3 * */4 5#include <stdio.h>6 7 Main ()8 {9 intCounter =0;Ten for (;;) { Oneprintf"%d\n", counter++); A } - /* - The For statement is a typical circular structure control statement. The For statement can be used to cycle through the process of changing a set of numbers, which at any time represents a value in the change. the While and Do...while are more general circular structure control statements. The Loop condition test statement followed by the while is not nullable, but the for is allowed to be null, and the default is true when the loop condition test statement for the for loop is empty. - - While (1) { - printf ("%d\n", counter++); + } - */ + A}
Standard C Episode 3