"A Basic C Language Introductory Course"-(7) The loop of C language is started in minutes

1. Learning objectives

  1. Know how to use loops

table of Contents

First: (a) from the Learning Myth
Two: (b) C language development is not so difficult to simply take you through the process
Title III: (c) easily understand the first C language program
Part IV: ( 4) The basic data types and variables of the language
Chapter 5: (5) Variables, constants and operations in the C language
Chapter 6: (6) Easy to understand the logic operations of the C language

recommend

I’m participating in the 1024 activity. Welcome everyone to like, bookmark, and comment on my dry goods article "An article takes you from 0 to 1 to understand the site construction and complete the CMS system writing".
Welcome everyone to pay attention to the official account. The official account is 1024 and 1024 times multiples. A lottery will give a mechanical keyboard + 2 IT books~
Insert picture description here

Second, understand the use of circulation

Loop refers to the realization of repeated execution of the program, including loop condition judgment. The loop in C language is divided into for loop, while loop, do...while loop. The loop can be jumped out by the default judgment condition, or you can write control statements yourself to realize the jump out or ignore the loop.

2.1 Understanding the use of for loops
Why do we need loops? What does the loop do? Loop is the repeated execution of a certain piece of code. In fact, a very simple example can explain the role of loops. For example, when you need to control a variable, let it increase from 0 each time by adding 1, adding 2, adding 3... until it is added to 100. At this time, writing code can be implemented simply by using a loop; you may feel that this example cannot be understood. Then I said that if you want to output the words "I want to go to heaven" 100 times, you write a program based on the current knowledge you have learned. You think this is a tedious process. When you learn the loop, this tedious problem will be solved easily.

Let's start with a simple example. Now I need to output "I want to go to heaven" 10 times, using a loop, the code is as follows:

#include<stdio.h>

void main() {
    
    
	int i=0; 
	for(;i<10;i++){
    
    
		printf("我想上天\n");
	}
}

In the above code, a variable i is first defined and initialized to 0. Use a for loop afterwards for(;i<10;i++). The format of the for loop is the for keyword, followed by a pair of parentheses. Inside the parentheses ;i<10;i++, there are two semicolons. The first semicolon can be used to write the initialization of the variable used in the loop. Here is i, because I have It is initialized to 0 when it is created. I will not repeat the assignment here; after separated by a semicolon, it is i<10this judgment, which means that each cycle needs to judge whether the expression is correct, if i is not less than 10, no more Execute the loop, and then use a semicolon to indicate the end; after that, i++ indicates that the variable i is incremented by 1 each time the loop is looped. The value of the variable i is 1 for the first time 0+1, and the value of i is 1, 1+1 for the second time. It is 2, and so on, if it is not less than 10, it will jump out. i++ can actually be written as i=i+1, i++ is a simple way of writing. In the curly braces that follow are loop statements.

In order to facilitate the understanding of novices, the above code is posted here that is not initialized when creating i:

#include<stdio.h>

void main() {
    
    
	int i; 
	for(i=0;i<10;i++){
    
    
		printf("我想上天\n");
	}
}

The result is as follows:
Insert picture description here
We can change the code to output the value of variable i every time to see if i increases. code show as below:

#include<stdio.h>

void main() {
    
    
	int i=0; 
	for(;i<10;i++){
    
    
		printf("我想上天\n");
		printf("i的值为%d\n",i);
	}
}

The results are as follows:
Insert picture description here
Why is the value of i not 10? Because the first time is 0, starting from 0 and increasing up, when i is 9, it is the 10th cycle, and the next time the value of i is 10, the cycle will not be repeated.

If you want to ignore the fifth loop, you can add an if statement in the loop body (referring to all the content in the loop curly braces) to determine whether the value of i is 5, and use the continue keyword to skip the current loop for 5. code show as below:

#include<stdio.h>

void main() {
    
    
	int i=0; 
	for(;i<10;i++){
    
    
		if(i==5){
    
    
			continue;
		}
		printf("我想上天\n");
		printf("i的值为%d\n",i);
	}
}

The above code uses continue to ignore this loop when i is equal to 5. It will not execute further, just ignore it and start the next loop.

The running result is as follows, and there is no output where i is equal to 5.
Insert picture description here
If you want to jump out of the loop when it is equal to 5, you can use break to jump out of the loop. Modify the code as follows:

#include<stdio.h>

void main() {
    
    
	int i=0; 
	for(;i<10;i++){
    
    
		if(i==5){
    
    
			break;
		}
		printf("我想上天\n");
		printf("i的值为%d\n",i);
	}
}

After using break, it will jump out directly, and no longer loop the next content.

The running results are as follows:
Insert picture description here
2.2 Understand the usage of
while loop While loop is similar to for loop, but the loop variable value needs to be set in the loop body (loop variable refers to the variable that can control the number of loops, and the loop traverse is i in the for loop).

Check out the while loop example:

#include<stdio.h>
int main() {
    
    
    int i=0;
    while(i<10){
    
    
            printf("我想上天\n");  
            i++;
	} 
}

The loop uses while in the above code. The expression in parentheses after while is a loop judgment. When i is not less than 10, it jumps out of the loop; the content in the curly brackets is the loop body, and i++ in the loop body increases each time, thus making the loop Controllable.

The results of the operation are as follows:
Insert picture description here
2.3 Understand the use of
do...while loop do...while loop is similar to while loop, and the difference with while loop is that the condition is first judged and then the loop is executed in the while loop, and the do...while loop will loop first and then judge Whether the conditions are met.
View example:

#include<stdio.h>
void main() {
    
    
    int i=10;
    do{
    
    
        printf("我想上天\n");  
        i++;
      } while(i<10);
}

The do...while loop can be known from the code example. First, it starts with a do, and then uses a pair of curly braces. Inside the curly braces is the loop body. Write the while keyword after the curly braces and the circle after the while. Fill in the loop conditions in parentheses. Even if the condition is not met, do...while will execute the loop once, and then jump out after judging that the condition is not established. Note that you need to add a semicolon after the while condition to indicate the end.
The results are as follows:

Insert picture description here
Please follow the official account for IT original animation, learning materials, and original tutorials. Get 2 copies of IT books and 1 copy of mechanical keyboard for official account with 1024 fans
Insert picture description here

Five, summary

Through the above description and explanation, we have learned the following points:

  1. Understand the use of for loop
  2. Understand the use of while loop
  3. Understand the use of do...while loop
  4. Understand how to use break and continue

Guess you like

Origin blog.csdn.net/A757291228/article/details/108899175