<>for loop

stay while Print in loop 1-10, The code is as follows
int main() { int i = 1;// initialization while(i <= 10)// Judgment part { printf("%d",i); i++;// Adjustment part
} return 0; }
while The initialization part of the loop is too far away from the judgment part and the adjustment part , And in for The syntax in the loop is as follows :

grammar
for( expression 1; expression 2; expression 3) { Loop statement ; }
expression 1 Is the initialization part ; expression 2 For the condition judgment part ; expression 3 For the adjustment part

for Cycle to print on screen 1-10
int main() { int i=0; for(i=1;i<=10;i++) { // initialization ; judge ; adjustment printf("%d",i); } return
0; }

We can find that while There are still three necessary conditions for circulation , However, due to the problem of style, the three parts are likely to deviate a lot , In this way, the search and modification is not centralized and convenient enough . therefore ,for The style of circulation is better .
for Recycling is also the most frequent .

for In the cycle break
int main() { int i=0; for(i=1;i<=10;i++) { if(i==5) break; printf("%d",i);
//1234 } return 0; }

break The function and function of while It's the same cycle , Also jump out of the whole cycle .

for In the cycle continue
int main() { int i=0; for(i=1;i<=10;i++) { if(i==5) continue; printf("%d",i);
//1234 678910 } return 0; }

stay i=5 Time ,continue,continue Skip the role of continue The following statement , therefore continue Skip it i=5 Print when , Skip and continue i++, So there's the print at the back .

For writing for Circular suggestions

1. Do not modify the value of the loop variable in the tuning loop , Prevent circulatory body from losing control

2. proposal for The value of the loop control variable of the statement adopts the “ Front closing back opening section ” How to write it .
int i = 0; // The writing method of front closing and back opening for(i=0; i<10; i++) {} // There are closed intervals on both sides for(i=0; i<=9; i++) {}
int main() { int arr[10]={0}; int i=0; for(i=0;i<10;i++) { printf("%d",arr[i]);
} // There are closed intervals on both sides /*for(i=0;i<=9;i++) { printf("%d",arr[i]); }*/ return 0; }
some for A variant of the cycle

Omission of judgment part - The judgment part is always true
int main() { for(;;) { printf("haha");// Fall into a dead circle } return 0; }
This is the judgment part that is always true , So it's a dead cycle

Let's take another look at the code
int main() { int i=0; int j=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(
"haha\n");//9 individual haha } } return 0; }

Nested loop above , When we will i=0;j=0; The initialization section is omitted
int main() { int i=0; int j=0; for(;i<3;i++) { for(;j<3;j++) { printf("haha\n")
;//3 individual haha } } return 0; }

It's only implemented here i=0 Three of the best haha, When i=1 Time , here j=3,j Not less than 3, Can't get in and out of circulation , and so on , There's no circulation in the back

Written questions

How many times do I have to cycle ?
int main() { int i=0; int k=0; for(i=0,k=0;k=0;i++,k++) k++; return 0; }
The answer is 0 second , Note that the judgment area is the assignment number , The judgment is false , So the circulation can't go in .

<>do……while loop
do Loop statement ; whlie( expression );
do The characteristics of sentence

The loop is executed at least once , There are limited scenarios to use , So not often .

do while Circular printing 1-10:
int main() { int i=1; do { printf("%d ",i); i++; }while(i<=10); }
do while In the cycle break and continue
int main() { int i=1; do { if(i==5) { break; } printf("%d ",i); i++; }while(i<=
10); }
Printing 1 2 3 4,i==5 Time ,break Jump out of the loop

int main() { int i=1; do { if(i==5) { continue; } printf("%d ",i); i++; }while(
i<=10); }
continue Skip subsequent statements , Skip it i++, So it was printed 1 2 3 4 After that, he fell into a dead circle

<> practice

1. calculation n factorial
// calculation n factorial #include<stdio.h> int main() { int n = 0; scanf("%d", &n); int temp =
1; for (int i = 1; i <= n; i++) { temp = temp * i; } printf("%d", temp); return
0; }
2. calculation 1!+……+10!

Method 1 :
// calculation 1!+……+10! // Method 1 : #include<stdio.h> int main() { int n = 0; int temp = 1;
int sum = 0; for (n=1;n<=10; n++) { temp = 1;// calculation n Before the factorial of , hold temp Set as 1 for (int i = 1;
i<= n; i++) { temp = temp * i; } sum = sum + temp; } printf("%d", sum); return
0; }
Method 2 :
#include<stdio.h> int main() { int temp = 1; int sum = 0; for (int n = 1; n <=
3; n++) { temp = temp * n; //1*1 1*1*2 1*1*2*3// sum = sum + temp; } printf("%d"
, sum); return 0; }
* Half search algorithm #include<stdio.h> int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 7;// Number to find // stay arr This is an ordered array k(7) Value of int sz = sizeof(arr) / sizeof(arr[0]);
int left = 0; int right = sz - 1; while (left<=right) { int mid = (left + right)
/ 2; if (arr[mid] < k) { left = mid + 1; } else if (arr[mid] > k) { right = mid
- 1; } else { printf(" eureka , The subscript is :%d\n", arr[mid]); break; } } if (left > right) {
printf(" can't find \n"); } return 0;
*
Write the code , Demonstrates moving multiple characters from both ends , Converge in the middle .
#include<stdio.h> #include<windows.h> #include<string.h> int main() { char arr1
[] = "welcome to bit!!!!!!"; char arr2[] = "####################"; int left = 0;
int right = strlen(arr1) - 1; while (left <= right) { arr2[left] = arr1[left];
arr2[right] = arr1[right]; printf("%s\n",arr2); Sleep(1000);// Sleep for a second system("cls")
;// Clear screen left++; right--; } return 0; }
*
Code implementation , Simulate user login scenarios , And you can only log in three times .( Only three passwords are allowed , If the password is correct, the login is successful , If the input is wrong three times , Then exit the program .
#include<stdio.h> #include<string.h> int main() { int i = 0; // Assume the correct password "123456"
char password[20] = { 0 }; for (i = 0; i < 3; i++) { printf(" Please input a password :>"); scanf(
"%s", password); //if(password=="123456")//err- String comparison cannot be used ==, Need to use strcmp if (strcmp
(password, "123456") == 0)// Return successfully 0 { printf(" Successful login \n"); } else { printf(
" Wrong password , Reenter !\n"); } } if (i == 3) { printf(" The password is wrong three times , Exit the program \n"); } return 0; }
6. Guessing numbers
// Guessing numbers //1. Automatically generate a 1-100 Random number between //2. Guess the number // a. That's right , Congratulations , game over //
b. You guessed wrong , I'll tell you how big it is , I guess it's too small , Keep guessing , You know, guess right //3. The game is played all the time , Unless you quit the game #include<stdio.h> #include
<time.h> void menu() { printf("***************************\n"); printf(
"******** 1.play *********\n"); printf("******** 0.exit *********\n"); printf(
"***************************\n"); } void game() { // The realization of guessing numbers game //1. Generating random numbers
//rand Function returns a 0-32767 Number between // One time corresponds to one timestamp int ret = rand()%100+1;
//%100 What is the remainder of 0-99, then +1, namely 0-100 //printf("%d\n", ret); //2. Guess the number int guess = 0; while
(1) { printf(" Guess the number :>"); scanf("%d", &guess); if (guess < ret) { printf(" Guess it's too small \n");
} else if (guess > ret) { printf(" Guess big \n"); } else { printf(" Congratulations on your guess \n"); break; }
} } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu();
// Print menu printf(" Please select :>"); scanf("%d", &input); switch (input) { case 1: game();
break; case 0: printf(" Quit the game \n"); break; default: printf(" Wrong choice , Reselect "); break; } }
while(input); return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element