Summary of C Language: 3 Essential Loop Structures

Click the blue text
Summary of C Language: 3 Essential Loop Structures
Follow us

Due to changes in the public account’s push rules, please click “View” and add “Star” to get exciting technical shares at the first time

Source from the internet, please delete if infringing

The loop structure can be seen as a combination of a conditional statement and a backward turning statement.

Additionally, the three elements of the loop structure: loop variable, loop body, and loop termination condition, are represented in the program flowchart using a decision box, where the condition is written inside the decision box, and the two exits correspond to the different instructions executed when the condition is satisfied or not satisfied, with one pointing to the loop body, which then returns to the entry point of the decision box.

Summary of C Language: 3 Essential Loop Structures

1. While Loop

The while loop first checks the expression, then executes the loop body. When the condition is met, it enters the loop, and once the condition is not met, it exits the loop. The general expression for the while statement is: while (expression) {loop body}.

2. Do-While Loop

The do…while loop is a variant of the while loop. This loop executes the statements inside do{} once before checking if the while() condition is true, and if the condition is true, it repeats the do…while loop until the while() is false.

The difference between the two loops: the while loop checks the condition before executing, while the do-while executes once before checking.

Comprehensive example: Narcissistic number

#include<Stdio.h>int main(void) {// n=3   153  1^3  5^3   3^3     int n;    scanf("%d", &n);       int number=1;    int t = 1;    int i;      while (t < n) {        number *= 10;        t++;  }        i = number;    while (number < i * 10) {// Traverse numbers from 100 to 1000        int j = number;        int sum = 0;                do {            int d = j % 10;                        j /= 10;            int b = 1;            int c = 0;            while (c < t){   b *= d;     c++;    }            sum += b;        } while (j > 0);        if (number == sum) {            printf("%d ", sum);        }    number++;    }   return 0;}

3. For Loop

for(initialization; condition; action per iteration) { (loop body) }

Each of the expressions can be omitted, but the semicolon cannot be omitted, as “;” can represent an empty statement. Omitting it reduces the number of statements, changing the format of the statements, making it unrecognizable to the compiler and thus unable to compile.

Example: White Chicken Hundred Money

#include<stdio.h>int main(void) {    int x, y, z;    int sum = 0;    for (x = 0; x < 20; x++) {        for (y = 0; y < 33; y++) {            z = 100 - x - y;   if ((x * 5 + y * 3 + z / 3.0) == 100 && x > 0 && y > 0 && z > 0) {          printf("%d %d %d\n", x, y, z);                }     }  }  return 0;}//Result://4 18 78//8 11 81//12 4 84

4. Goto Loop

The goto statement, also known as an unconditional transfer statement, has the following general format: goto label; where the label is a symbol written according to the identifier specification, placed before a certain statement line, with a colon (:) after the label. The label serves to identify the statement and is used in conjunction with the goto statement. For example: goto out; out:

The goto statement is usually used in conjunction with conditional statements. It can be used to implement conditional transfers, form loops, exit loop bodies, etc. However, in structured programming design, the use of goto statements is generally not advocated to avoid confusion in program flow, making it difficult to understand and debug the program.

Example: Input -1 to end the loop

#include<stdio.h>int main(void) {    int n;    scanf("%d", &n);    int i;    // Jump here    out: if (n != -1) {// If -1 is not entered, it will loop continuously           printf("*\n");        scanf("%d", &n);            goto out;// Define a goto statement named out here  }      return 0;}

Tips:

1. For loop statements, some auxiliary statements can be used for jumps or exits, such as: break (directly end the loop), continue (do not execute the subsequent statements, continue to return to the loop condition), goto (unconditional jump to a specified location)

2. When to use loop statements: use for when there is a fixed number, use do-while if it must execute once, otherwise use while.

Extracurricular: Decomposition of integers

1. For an integer, performing %10 operation gives its unit digit

2. For an integer, performing /10 operation gives the integer without its unit digit

Then performing %10 on the result of step 2 gives the original number’s ten’s digit.

Other digits follow this pattern…

If you are over 18 years old and find learning 【C language】 too difficult? Want to try other programming languages? Then I recommend you learn Python, where a Python zero-based course worth 499 yuan is available for free for a limited time, with only 10 spots available!


▲ Scan the QR code - Get it for free

Summary of C Language: 3 Essential Loop Structures
Click to read the original text to learn more

Leave a Comment