Loop structure of C language introductory notes

Cyclic structure

The function of the loop structure is to make the computer perform the same operation repeatedly

Various loop structures in C language

  • for loop

The for loop is a more commonly used loop, the general form isfor(一般表达式; 条件表达式; 末尾循环体){循环体}

Execution logic:

Created with Raphaël 2.1.2 开始 一般语句 条件语句 循环体 末尾循环体 其他操作 结束 yes no

Example: Print 1 2 3 4 5 …. 99 100 on the screen

#include <stdio.h>

int main()
{
    int i;
    //在屏幕上打印1-100
    for(i=1; i<=100; i++)
    {
        printf("%d ",i);
    }
    return 0
}
  • while loop

while(条件语句){循环体}

Execution logic

Created with Raphaël 2.1.2 开始 条件语句 主循环体 游标语句(比如i++等会改变判断结果的语句) 其他操作 结束 yes no

Example: Print 1 2 3 4 5 …. 99 100 on the screen

#include <stdio.h>

int main()
{
    int i = 1;
    while(i <= 100)
    {
        printf("%d ",i);
        i++;
    }
    return 0;
}

End

The most basic structure of the cycle will stop here, the same branch structure and the loop structure can be 嵌套used, cyclic structure typically used with a branched structure and to achieve complex functionality. Here is just a brief statement of the two branch structure

Guess you like

Origin blog.csdn.net/weixin_36382492/article/details/81456882