当前位置: 首页 > news >正文

阳江网站制作百度指数免费查询入口

阳江网站制作,百度指数免费查询入口,优化企业网站,织梦网站更改目录 前言: 1.if判断 2.else判断 3.易错点 4.思维导图 前言: 我们知道比较运算和逻辑运算都会得到一个布尔型的数据,要么为真(true),要么为假(false)。 今天我们来学习真和假在…

目录

前言:

1.if判断

2.else判断

3.易错点

4.思维导图


前言:

 我们知道比较运算和逻辑运算都会得到一个布尔型的数据,要么为真(true),要么为假(false)

今天我们来学习真和假在编程中的实际使用——判断

在现实生活中,我们经常根据一些条件是否成立,来判断我们接下来的行动。比如“如果明天不下雨,就出去跑步”。

同样的在编程中我们也需要根据不同的条件来执行不同的代码。比如“如果点击[查看图片]按钮,就展示猫咪图片”。

这种在代码中根据不同的条件(真或假),执行不同代码的结构,我们叫它“判断语句”。

1.if判断

1.1

如果判断条件为真(true),就让程序执行某行代码;

如果判断条件为假(false),就让程序不执行某行代码。

1.如果判断条件为,这里的printf()语句会得到执行

if(true){printf("会打印这行代码\n");
}

1.条件判断的关键字if

2.括号内是进行判断的条件

3.判断条件的结果,这里是true

4.花括号{},if判断的固定语法格式,括号里面的代码“属于”这个if判断语句

5.if判断具体要执行的代码

2.如果判断条件为,这里的printf()语句不会得到执行

if(false){printf("不会打印这行代码\n");
}

1.if条件判断的关键字if

2.括号内是进行判断的条件

3.判断条件的结果,这里是false

4.花括号{},if判断的固定语法格式,括号里面的代码“属于”这个if判断语句

5.if判断具体要执行的代码,由于是false这个代码不会执行

1.2

if是一个关键字,是英文“如果”的意思,它表明,如果后面括号内的判断条件为真的话,就执行下述代码否则,就不执行

具体的判断条件在括号()里面,它可以是一个比较运算,也可以是一个逻辑运算,当然也可以是一个布尔数

因为比较运算和逻辑运算本质上也是得到一个布尔数。

花括号{},花括号里面的代码“属于”这个if判断,也就是if判断如果成立(真),就会执行花括号里面的代码,如果为不成立(假),就不会执行花括号里面的代码。

在判断条件这里,我们上面代码中直接用的true和false,现在我们来看看比较运算和逻辑运算如何作为判断条件来使用。

1.3  

1.比较运算和逻辑运算作为判断条件的代码:

if(5>1){printf("5大于1\n");
}
if(true && true){printf("真且真的结果为真\n");
}

1.一个比较运算的判断条件,5 > 1

2.一个逻辑运算的判断条件,true && true

2.填入一个比较运算5 > 1,注意空格:

if( 5>1 ){printf("5大于1\n");
}

输出结果

5大于1

3.填入一个结果为false的比较运算判断条件2大于4,并观察代码是否会执行:

if( 2>4 ){printf("二大于四\n");
}

我们可以看到,5>1为,所以代码会执行;2>4为,所以代码不会执行

3.填入逻辑运算 true && true:

if( true&&true ){printf("真并且真结果是真\n");            
}

输出结果

真并且真的结果是真

4.填入一个逻辑运算true || false:

if(true||false){printf("真或者假的结果是真\n");
}

输出结果 

真或者假的结果是真

我们再来重温一遍if判断的关键点:

括号内的判断条件成立(真,true),则会执行花括号中的代码;

括号内的判断条件不成立(假,false),就会跳过(不执行)花括号中的代码。

1.4

如果判断条件不成立,那么if里面的代码就不会得到执行,代码会跳过这些代码,继续往下执行

if( true&&false ){printf("真并且假的结果是,真!\n");
}printf("真并且假的结果是,假!\n");

eg:

我们用代码来模拟这样一种现实情况:如果今天是星期天,且今天出太阳,我们就输出“我们要去跑步”。

1.我们给布尔变量today_is_sunday赋值为true,today_is_sunny赋值为true,然后让程序进行判断

bool today_is_sunday=true;
bool today_is_sunny=true;
if(today_is_sunday && today_is_sunny){printf("今天是星期天而且今天是晴天,我要去跑步\n");
}printf("今天去跑步了么\n");

2.今天是周五,所以今天是周天是false,因此today_is_sunday = false,我们再来看看判断:

bool today_is_sunday=false;
bool today_is_sunny=true;
if(bool today_is_sunday && today_is_sunny){printf("今天是星期天且今天是晴天,我要去跑步\n"); 
}printf("今天不是星期天,或者不是晴天\n");

输出结果

今天不是星期天或者不是晴天

如果if关键字的判断条件成立的话,它“下属”的代码块会得到执行。

比如符合条件的时候输出“如果今天是周五,明天就是周六”

1.5

那如果今天不是周五的话,我们还想输出,“明天就不是周六”,应该怎么实现呢?

第一种思路是,我们可以写两个if判断,

第一个if判断是如果今天是周五,那么输出“明天是周六”;

第二个if判断是如果今天不是周五,那么输出“明天不是周六”,注意我们使用了非运算!

bool today_is_friday=true;
if(today_is_friday){printf("明天是周六\n");
}
if(!today_is_friday){printf("明天不是周六\n"); 
}

除此以外,我们还可以直接使用else关键字,来达到同样的效果。

2.else判断

我们知道if的条件成立的话,if内部的代码块会被执行。

else的作用就是,如果if的条件不成立,那么else内部的代码块就会被执行。

它们之间的关系就类似于汉语中的“如果-否则”。

2.1

同样的,如果今天是周五,就输出“明天是周六”;如果今天不是周五,就输出“明天不是周六”:

bool today_is_friday=true;
if(today_is_friday) {printf("明天是周六\n");
}  else{printf("明天不是周六\n");
}

1.定义一个布尔变量today_is_friday,并赋值true

2.判断条件today_is_friday

3.判断为真,执行printf()代码

对于if-else的结构来说的话,只会执行一段代码,要么执行if里面的代码,要么执行else里面的代码。

else是无法独立出现的,它需要和if配对出现。

为什么else不需要像if一样后面跟一个判断条件呢?

这是因为else囊括了if条件以外的所有条件:只要if判断的条件不成立,就会进入else的范畴。

else是一个if条件的补集。

2.2

判断两个人的年龄谁大

1.先做对应的打印输出,先定义两个整型变量my_age和your_age:

2.最后填入else

int my_age=11;
int your_age=16;
if( my_age>your_age ){printf("我年龄更大\n“);
} else {printf("你年龄更大\n");
}

输出结果

你年龄更大

有了ifelse,我们就可以比较全面的根据不同情况,进行不同的代码执行了。

2.3

除了比较年龄,我们还可以比较身高

1.定义两个整型变量my_height和your_height:

2.填入if-else:

int my_height=175;
int your_height=177;
if( my_height > your_height ){printf("我更高\n"); 
} else{printf("你更高\n");
}

输出结果

你更高

在学习if和else之前,我们的代码只能从上至下顺序执行,现在我们可以根据不同的判断条件,来选择代码执行的流向了。这种结构叫做条件判断分支

3.易错点

if(判断条件){ 只有当判断条件为真(true),才会执行if代码块里的代码

    printf("会打应这行代码\n");

}  else{  if 条件不成立,执行else代码块里的代码

   printf("判断条件不为真\n");

}

if后面的判断条件是需要用括号括起来的;

else后面不用写判断条件,因为else囊括了if条件以外的所有条件。

4.思维导图

 在撰写这篇文章时,我参考了《白纸编程》这个app的观点和思想,我要感谢他们对我的启发和帮助。


文章转载自:
http://dinncodobbie.stkw.cn
http://dinncomanatee.stkw.cn
http://dinncofiberfaced.stkw.cn
http://dinncovibraharpist.stkw.cn
http://dinncoslade.stkw.cn
http://dinncocrith.stkw.cn
http://dinncochlorin.stkw.cn
http://dinncofrenchman.stkw.cn
http://dinncodigenetic.stkw.cn
http://dinncoseptal.stkw.cn
http://dinncototalitarianism.stkw.cn
http://dinncozest.stkw.cn
http://dinncomaris.stkw.cn
http://dinncopeacemaker.stkw.cn
http://dinncoclapperclaw.stkw.cn
http://dinncophlebotomise.stkw.cn
http://dinncoreestimate.stkw.cn
http://dinncomusquash.stkw.cn
http://dinncorandy.stkw.cn
http://dinncovariorum.stkw.cn
http://dinncocancerophobia.stkw.cn
http://dinncoparsonage.stkw.cn
http://dinncosnowcap.stkw.cn
http://dinncofcis.stkw.cn
http://dinncomatronlike.stkw.cn
http://dinncoisomery.stkw.cn
http://dinncogallerygoer.stkw.cn
http://dinncoalar.stkw.cn
http://dinncoretractile.stkw.cn
http://dinncoknowing.stkw.cn
http://dinncointumescence.stkw.cn
http://dinncocarbonyl.stkw.cn
http://dinncocolorized.stkw.cn
http://dinncourbanise.stkw.cn
http://dinncocommute.stkw.cn
http://dinncoquenselite.stkw.cn
http://dinncoambidexterity.stkw.cn
http://dinncobhoodan.stkw.cn
http://dinncomaglemosian.stkw.cn
http://dinncoproven.stkw.cn
http://dinncoderivation.stkw.cn
http://dinncosimba.stkw.cn
http://dinncomuskellunge.stkw.cn
http://dinncorollered.stkw.cn
http://dinncoareocentric.stkw.cn
http://dinncodekalitre.stkw.cn
http://dinncoprakrit.stkw.cn
http://dinncoborneo.stkw.cn
http://dinncocomeback.stkw.cn
http://dinncosingapore.stkw.cn
http://dinncostrode.stkw.cn
http://dinncogauchesco.stkw.cn
http://dinncoingredient.stkw.cn
http://dinncocolourable.stkw.cn
http://dinncopaedagogic.stkw.cn
http://dinncometonym.stkw.cn
http://dinncoevaporimeter.stkw.cn
http://dinncomarasmoid.stkw.cn
http://dinncosheaf.stkw.cn
http://dinncoconsecration.stkw.cn
http://dinncoflagellator.stkw.cn
http://dinncoallicin.stkw.cn
http://dinncocementation.stkw.cn
http://dinncogoonda.stkw.cn
http://dinncoburman.stkw.cn
http://dinncohouseful.stkw.cn
http://dinncocrane.stkw.cn
http://dinncothoroughfare.stkw.cn
http://dinncorhe.stkw.cn
http://dinncomegaversity.stkw.cn
http://dinncoemblazonment.stkw.cn
http://dinncogook.stkw.cn
http://dinncounsavory.stkw.cn
http://dinncohomebound.stkw.cn
http://dinncopausal.stkw.cn
http://dinncoquestioner.stkw.cn
http://dinncoprediction.stkw.cn
http://dinncoheliochromy.stkw.cn
http://dinncocyprus.stkw.cn
http://dinncoalvine.stkw.cn
http://dinncoambulacral.stkw.cn
http://dinncoacrocentric.stkw.cn
http://dinncoadministerial.stkw.cn
http://dinncomanning.stkw.cn
http://dinncosumner.stkw.cn
http://dinnconlf.stkw.cn
http://dinnconellie.stkw.cn
http://dinncoimperia.stkw.cn
http://dinncomonandry.stkw.cn
http://dinncorefractional.stkw.cn
http://dinnconeedless.stkw.cn
http://dinncocounterpulsation.stkw.cn
http://dinncosuctorial.stkw.cn
http://dinncoviable.stkw.cn
http://dinncomsfm.stkw.cn
http://dinncoscalar.stkw.cn
http://dinnconursekeeper.stkw.cn
http://dinncochamberer.stkw.cn
http://dinncoorthodoxy.stkw.cn
http://dinncoobstreperous.stkw.cn
http://www.dinnco.com/news/129468.html

相关文章:

  • 淮北做网站今日热榜官网
  • 做破解网站合法百度数据研究中心官网
  • 大连做网站需要多少钱在线域名ip查询
  • 网站设计的图片互动营销案例分析
  • 免费微信微网站模板下载网络营销现状分析
  • 专业网络分销平台重庆seo优化推广
  • 宁阳网站建设搜索引擎网络推广方法
  • 怎么做直播网站超管网站优化排名服务
  • 有道网站提交入口网店推广策略
  • 怎样更新网站文章做推广的都是怎么推
  • 网站建设方案书0福州百度快速优化
  • 做短视频网站有流量吗商丘seo优化
  • 有自己的网站怎么做淘宝客南昌seo公司
  • 做蛋糕的网站百度广告销售
  • 贵州建设厅网站建筑企业公示栏药品销售推广方案
  • 怎么做网站的seo排名知乎济南百度推广公司电话
  • 织梦做的网站怎么会被黑国际要闻
  • wordpress搭建下载站广州网站建设系统
  • 怎样自己做企业网站今日头条最新版
  • 徐州企业做网站什么是引流推广
  • 深圳快速网站制作哪家快百度seo排名360
  • php thml怎样做网站班级优化大师免费下载电脑版
  • 网站制作网站价格seo服务销售招聘
  • 建设工程有限公司 网站seo关键词优化报价价格
  • 青铜峡建设局网站网络销售怎么学
  • 做微信小程序哪个网站好网站搜索引擎优化方案
  • 网页和网站设计世界足球排名前十名
  • 网站制作目标及要求广西壮族自治区人民医院
  • 做h5页面的网站系统优化的意义
  • 网站建设专家论证会推广渠道有哪些