您的位置:首页 > 编程语言 > C语言/C++

1. if 语句

2014-05-12 08:26 197 查看


1. if 语句

1.1 现实生活中有许多需要做出判断的情况,例如:
如果今天天气好,那么就做公交去上班,否则就开车去上班。
如果累了就休息一会儿,否则继续工作。
1.2 if语句的语法结构如下:
if(表达式)语句
表达式为真,执行语句,否则不执行语句
1.3 if语句的其他变种
1.3.1 if else
if(表达式)
{
语句
}else
{
语句
}
1.3.2 if elseif else
if(表达式)
{
语句
}else if(表达式)
{
语句
}else if(表达式)
{
语句
}else
{
语句
}
 
#include <stdio.h>
// 测试if
void test_if() {
            // 如果生病了就呆在家里休息
            int sick = 1;
            if (sick) {
                        printf("呆在家里休息!");
            }
}
// 测试if else
void test_ifelse(){
            // 如果生病了就呆在家里休息,否则去上班
            int sick = 0;
            if(sick){
                        printf("呆在家里休息!");
            }else{
                        printf("去上班!");
            }
}
// 测试 if elseif
void test_if_elseif(){
            int score = 50;
            if(score<60){
                        printf("不及格!");
            }else if(score>=60&&score<70){
                        printf("及格!");
            }else if(score>=70&&score<80){
                        printf("优!");
            }else{
                        printf("良!");
            }
}
// 测试嵌套if
void test_nest_if(){
            int a = 200,b = 1,c = 5;
            if(a>b){
                        if(a>c){
                                    printf("a最大!");
                        }else{
                                    printf("c最大!");
                        }
            }else{
                        //b>a
                        if(b>c){
                                    printf("b最大!");
                        // a<b<c
                        }else{
<
a826
span style="color:rgb(51,51,51);font-family:sans-serif, Arial, Verdana, 'Trebuchet MS';font-size:13px;line-height:20.799999237060547px;">                                    printf("c最大!");
                        }
            }
}
int main(void) {
            //test_if();
            //test_ifelse();
            //test_if_elseif();
            test_nest_if();
            return 0;
}

该博客教程视频地址:http://geek99.com/node/977

原文出处:http://geek99.com/node/834#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息