C语言 while

C语言 while

while 语句

流程图

 

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    // 格式:while (表达式){}
    // 循环:每次循环将变量i加1直到10停止
    int i = 0;
    while (i < 10)
    {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
while 使用案例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// 敲7程序
int main(void)
{
    int i = 1;
    while (i <= 100)
    {
        if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
        {
            printf("敲桌子\n", i);
        }
        else
        {
            printf("%d\n", i);
        }
        i++;
    }
    return 0;
}
while 使用案例:敲七

do…while 语句

流程图

dowhile 与 while 区别

int i = 0;

// dowhile会先执行语句在判断表达式
    do 
    {
        printf("%d\n", i);
        i++;
    } while (i < 10);


// while会先判断表达式在执行语句
    while (i)
    {
        printf("%d\n", i);
        i++;
    }

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    int i = 0;

    // do{} while (表达式);
    // 限制性语句、在判断表达式
    do 
    {
        printf("%d\n", i);
        i++;
    } while (i < 10);

    return 0;
}
while..do 使用案例

 

posted @ 2020-02-27 17:24  kevin.Xiang  阅读(405)  评论(0编辑  收藏  举报