上嵌学习笔记

C概念及循环

2016-12-13  本文已影响9人  追風

一,类型转换运算

#include<stdio.h>
int main()
{
    int num = 4;
    int num1 = 3;
    float div = num / num1;           //隐式转换,数据类型向精度高的转化
    float div1 = (float)num / num1;   //显式转换即强制转换数据类型
    printf("隐式转换%f\n", div);
    printf("显式转换%f\n", div1);
    printf("%f, %d", (float)num, num);//强制转换只是临时转换
    return 0;
}

二,位运算


三,赋值操作符


四,三目运算符


五,if语句

#include<stdio.h>
int main()
{
    int grade = 0;
    printf("input grade: ");
    scanf("%d", &grade);
    printf(this grade is );
    if(grade > 100 || grade <0)  //if括号后没有“;”
        printf("not grade!\n");
        else if(grade >= 90)     //下面的else if括号后均不能有“;”
            printf("A\n");
            else if(grade >= 70)
                printf("B\n");
                    else if(grade >= 60)
                        printf("C\n");
                        else if(grade >= 0)
                            printf("D\n");
                            return 0;
}

六,switch语句

#include<stdio.h>
int main()
{
    int grade = 0;
    printf("input grade: ");
    scanf("%d", &grade);
    printf(this grade is );
    grade /=10;       //对成绩进行处理
    switch(grade)     //括号后不能有“;”
    {
        case 10:
        case 9:
            printf("A\n");
            break;   //若执行这语句跳出循坏
        case 8:
        case 7:
            printf("B\n");
            break;
        case 6:
            printf("C\n");
            break;
        case 5:
        case 4:
        case 3:
        case 2:
        case 1:
        case 0:
            printf("D\n"); //对于多事件输出同一结果的问题可以这样处理
            break;
        default:
            printf("not grade!\n");
            break;
    }
return 0;
}

七,while语句

#include<stdio.h>
int main()
{
    int num = 0;
    int sum = 0;
    while(num <= 100)   //while语句后不能有“;”
    {
       num++;
       if(num % 10 == 4)
       continue;        //如果if为真,结束本次循环 
       sum +=num;
    }
    printf("和为 %d", sum)
    return 0;
}

八,do while语句

#include<stdio.h>
int main()
{
    int num = 0;
    int sum = 0;
    do
    {
       num++;
       if(num % 10 == 4)
       continue;        //如果if为真,结束本次循环 
       sum +=num;
    }
    while(num <= 100);  //do while 括号后“;”不能少
    printf("和为 %d", sum)
    return 0;
}

九,for语句

#include<stdio.h>
int main()
{
    int num = 0;
    int sum = 0;
    for(num = 1; num <= 100; num++)  //括号后没有“;”,括号内“;”不能少
        sum +=num;
        printf("1 + 2 +...+ 99 +100 = %d", sum);
        return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读