如果今天天气好,那么就做公交去上班,否则就开车去上班。
如果累了就休息一会儿,否则继续工作。
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{
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#