当前位置:网站首页>C language loop structure program

C language loop structure program

2022-04-23 17:55:00 Chshyz

while loop 、do while loop 、for loop 、break End 、continue end

Environmental Science :CentOS7;
The loop condition :① The loop body ;② Cycle end condition ;
characteristic : The computer only deals with conditional loops ( effectiveness 、 Certainty and finiteness ).

  • while loop ( First judge recycling , Embedded statement )
    Example 1:1+2+3+…+100
     Insert picture description here
#include <stdio.h>
int main() {
    
    int i, sum = 0;
    i = 1;
    while (i <= 100) {
    
          sum += i;
          i++;
    }
    printf ("%d\n",  sum);
    return 0;
}
[[email protected] c]# gcc -o while while.c
[[email protected] c]# ./while
5050
  • do while( First cycle, then judge )
    Example 1:1+2+3+…+100
     Insert picture description here
#include <stdio.h>
int main() {
    
    int i,  sum = 0;
    i = 1;
    do {
    
          sum += i;
          i++;
    }
    while (i <= 100);
    printf ("%d\n",  sum);
    return 0;
}
[[email protected] c]# gcc -o dowhile1 dowhile1.c
[[email protected] c]# ./dowhile1
5050
  • for loop ( Assign an initial value to a loop variable ; The loop condition ; Loop variable increment )
#include <stdio.h>
int main() {
    
    int i, sum = 0;
    for (i = 1; i <= 100; i++)
        {
    
        sum += i;
        }
    printf("%d\n", sum);
    return 0;
}

[[email protected] c]# gcc -o for for.c
[[email protected] c]# ./for
5050


  • break sentence ( End the cycle ahead of time , Terminate the entire )
    Example : The current shift does not exceed 30 people , Make the program count the average score of the class , End the loop when a negative number is received .
[[email protected] c]# cat break.c
#include <stdio.h>
int main() {
    
    float score, sum = 0, average;
    int i, n;
    for (i = 1; i < 31; i ++) {
    
        scanf ("%f", &score);
        if (score < 0) break;
        sum = sum + score;
        }
     n = i - 1;
     average = sum/n;
     printf ("n = %d, average = %7.2f\n", n, average);
     return 0;
}
[[email protected] c]# ./break
77
88
99
12
33
44
55
66
-1
n = 8, average =   59.25

#flloat score Define a floating point variable score
#%7.2f Print before the decimal point 7 position ( Less than seven places are filled in 0); Two decimal places ( Less than two people make up for it 0).
  • continue sentence ( End this cycle ahead of time , Just terminate this cycle, not the whole )
    PS: End this cycle , That is, skip the following unexecuted statements in the loop body , Then proceed to the next time whether 1 Execute loop judgment .
    Example : Enter the grade of all the students in a class , Output the results of failed students , And ask for the average score of passing students .
#include <stdio.h>
int main() {
    
    float score, sum = 0, average;
    int i, n = 0;
    for (i = 1; i < 6; i ++) {
      # Suppose that those who fail have 5 individual 
        printf ("please enter score:");
        scanf ("%f", &score);
        if (score < 60) {
       # If you fail 
            printf ("Fail:%7.2f\n", score); # End this cycle , Output failing grades 
            continue;
            }
         sum = sum + score;
         n = n + 1;   # Count the number of people who failed 
         }
     average = sum/n;  # A passing grade average 
     printf ("\n n = %d, average = %7.2f\n", n,  average);  # Output the number of passing students and average score 
     return 0;
}
[[email protected] c]# gcc -o continue continue.c
[[email protected] c]# ./continue
please enter score:44
Fail:  44.00
please enter score:55
Fail:  55.00
please enter score:60
please enter score:70
please enter score:90

 n = 3, average =   73.33

版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547414047.html