C language_branch and loop statements (1)


Preface

C language: structured programming language
sequential structure, selection structure, loop structure

branch statement

  • if
  • switch

loop statement

  • while
  • for
  • do while

1. What is a statement?

1.C statements can be divided into the following five categories:

1. Expression statement, 2. Function call statement, 3. Control statement, 4. Compound statement, 5. Empty statement (a semicolon; that is)

2. Control statements

It is used to control the execution flow of the program to realize various structural methods of the program (C language supports three structures: sequential structure, selection structure, loop structure). They are composed of specific statement definitions. C language has nine types of control statements.

3. The following three categories:

1. Conditional judgment statements are also called branch statements: if statement, Switch statement;
2. Loop execution statements: do while statement, while statement, for statement;
3. Turning statements: break statement, go to statement, continue statement, return statement.

2. Branch statement (selection structure)

2.1.1 Grammatical structure of if statement

By default, it can only be followed by one statement; if you want to follow multiple statements, add braces { }
  • Single branch - syntax structure:
    if (expression)
    statement;

  • Double branch - Grammar structure:
    if (expression)
    statement 1;
    else
    statement 2;

  • Multi-branch
    if (expression 1)
    statement 1;
    else if (expression 2)
    statement 2;
    else
    statement 3;



2.1.2 Comparison of if writing forms


2.1.3 Exercise

  • 1. Determine whether a number is odd.
    Features: divide 2 into 1.

  • 2. Print odd numbers between 1 and 100

2.2 switch statement

The switch statement is also a branch statement. Often used in multi-branch situations

!!! Add a break statement after the last case statement. !!!

switch statement syntax
switch (integer expression)
{ statement item; } statement item - // are some case statements; // as follows: case integer constant expression: statement;







2.2.1 break in switch statement

In the switch statement, we cannot implement branches. Only by using break can we achieve real branches.


2.2.2 default clause (default - default meaning)

If the value of the expression does not match the values ​​of all case tags, the structure - that is, all statements will be skipped, the program will not terminate, and no error will be reported, because this situation is not considered an error in C.
But what if you don’t want to ignore the value of an expression that doesn’t match all tags?

practise:
  • 1.switch statements can be nested
  • 2.break will only jump out of one layer of switch

3. Loop statement

  • while
  • for
  • do while

3.1 while loop

Syntax structure: while (expression) loop statement;

3.1.1 break and continue in while statement

  1. The role of break in the while loop:

If break is encountered in a loop, all subsequent loops will be stopped and the loop will be terminated directly.
So: break in while is used to terminate the loop permanently.

  1. continue introduction:

continue is used to terminate this loop, that is, the code after continue in this loop will not be executed,
but jumps directly to the judgment part of the while statement to judge the entry of the next loop.

四.getchar putchar EOF

  1. getchar
    reads a character and gets a character

EOF — end of file is essentially a sign of the end of the file.


When the function fails to read , it returns EOF. The scanf function reads successfully and returns the number of data read. If the reading fails, it returns EOF. getchar. If the reading succeeds, it returns the ASCII code value of the character. If the reading fails, it returns EOF. Ctrl + Z Will cause scanf or getchar to return EOF
  1. putcher
    print characters

Guess you like

Origin blog.csdn.net/Ghr_C99/article/details/132511378