C language_branch and loop statements (2)


Preface

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


1. for loop

  • Guidance
    is too scattered

1.1 Grammar

Use a for loop to print 1~10

By default, the loop body can only control one statement.
If you want to control multiple statements, you need to add braces.

Example

1.2 Loop control variables of for statement

suggestion:

  • 1. Do not modify loop variables in the for loop body to prevent the for loop from losing control.
  • 2. It is recommended that the value of the loop control variable of the for statement be written in the "front closed and then open range" writing method

Error example (modification in loop body)

1.3 Some variations of for loops

(The judgment part is omitted, which means the judgment is always true)

2. do...while() loop

2.1 Syntax of do statement

2.2 break and continue in do… while loop

2.3 Exercise

1 - Calculate the factorial of n


2. -Find a specific number n in an ordered array

Binary search algorithm; half search algorithm (premise, orderly)


3. - **Write code to demonstrate that multiple characters move from both ends and converge toward the middle

Effect:**


4. -Write code to simulate user login scenarios, and can only log in three times (only three passwords are allowed to be entered. If the password is correct, the login will be successful. If the password is entered incorrectly three times, the program will exit.)


5. -Comparison of two strings

You cannot use == to compare two strings for equality. You should use the strcmp library function to compare.
strcmp returns 0 to indicate that the two strings are equal.
strcmp returns a number >0, indicating that the first string is greater than the second string.
strcmp returns a number <0, indicating that the first string is smaller than the second string.


Guess you like

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