30天代码day3 Intro to Conditional Statements
Boolean
A logical statement that evaluates to true or false. In some languages, true is interchangeable(可互换的) with the number 1 and false is interchangeable with the number 0.
Conditional Statements
The basic syntax used by Java (and many other languages) is:
if(condition) {
// do this if 'condition' is true
}
else {
// do this if 'condition' is false
}
where condition is a boolean statement that evaluates to true or false. You can also use an if without an else, or follow an if(condition) with else if(secondCondition) if you have a second condition that only need be checked when condition is false. If the if (or else if) condition evaluates to true, any other sequential statements connected to it (i.e.: else or an additional else if) will not execute.
Logical Operators
Customize your condition checks by using logical operators. Here are the three to know:
||is the OR operator, also known as logical disjunction.&&is the AND operator, also known as logical conjunction.!is the NOT operator, also known as negation.
Another great operator is the ternary operator for conditional statements (? :). Let's say we have a variable, v, and a condition, c. If the condition is true, we want v to be assigned the value of a; if condition c is false, we want v to be assigned the value of b. We can write this with the following simple statement:
v = c ? a : b;
In other words, you can read c ? a : b as "if c is true, then a; otherwise, b". Whichever value is chosen by the statement is then assigned to v.
Switch Statement
This is a great control structure for when your control flow depends on a number of known values. Let's say we have a variable, condition, whose possible values are val0, val1, val2, and each value has an action to perform (which we will call some variant of behavior). We can switch between actions with the following code:
switch (condition) {
case val0: behavior0;
break;
case val1: behavior1;
break;
case val2: behavior2;
break;
default: behavior;
break;
}
Note: Unless you include break; at the end of each case statement, the statements will execute sequentially. Also, while it's good practice to include a default: case (even if it's just to print an error message), it's not strictly necessary.
Additional Language Resources
C++ Statements and Flow Control
Python Control Flow Tools
30天代码day3 Intro to Conditional Statements的更多相关文章
- 30行代码搞定WCF并发性能测试
[以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main() { List< ...
- 30天代码day2 Operators
Operators These allow you to perform certain operations on your data. There are 3 basic types: Unary ...
- 30行代码让你理解angular依赖注入:angular 依赖注入原理
依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...
- 30行代码实现Javascript中的MVC
从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...
- Tensorflow快餐教程(1) - 30行代码搞定手写识别
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/details ...
- 10分钟教你用python 30行代码搞定简单手写识别!
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 手写笔记还是电子笔记好呢? 毕业季刚结束,眼瞅着2018级小萌新马上就要来了,老腊肉小编为了咱学弟学妹们的学习,绞尽脑汁准备编一套大学秘籍, ...
- JAVA_SE基础——30.构造代码块
黑马程序员入学blog...构造代码块作用:给所有的对象进行统一的初始化. 问题:要求每个小孩出生都会哭,这份代码有两个构造函数,如果需要每个小孩出生都要哭的话,那么就需要在不同的构造函数中都调用cr ...
- 30 行代码实现 JS 中的 MVC
一连串的名字走马观花式的出现和更迭,它们中一些已经渐渐淡出了大家的视野,一些还在迅速茁壮成长,一些则已经在特定的生态环境中独当一面舍我其谁.但不论如何,MVC已经并将持续深刻地影响前端工程师们的思维方 ...
- 30行代码消费腾讯人工智能开放平台提供的自然语言处理API
腾讯人工智能AI开放平台上提供了很多免费的人工智能API,开发人员只需要一个QQ号就可以登录进去使用. 腾讯人工智能AI开放平台的地址:https://ai.qq.com/ 里面的好东西很多,以自然语 ...
随机推荐
- Delphi 带星期几的日期格式化
把日期按日期+星期几的格式输出 方法1:DatetoStr + DayOfWeek计算 ,这种办法灵活,但计算量大,不再祥叙. 方法2:FormatDateTime 具体代码如下://这里需要用For ...
- python操作samba
最近在部署完xxl-job后,陆续将一些日常性执行的python脚本迁移到上面去:其中部分脚本涉及到对samaba的操作,先后尝试了pysmb.fs.smbfs.pysmbclient pysmb 安 ...
- Python 字典删除元素clear、pop、popitem
同其它python内建数据类型一样,字典dict也是有一些实用的操作方法.这里我们要说的是字典删除方法:clear().pop()和popitem(),这三种方法的作用不同,操作方法及返回值都不相同. ...
- Python随笔--代理ip
- 正则表达式判断QQ号格式是否正确
#正在表达式匹配QQ号格式是否正确#QQ号假如长度为5-11位,纯为数字 import rewhile 1: qq=input("请输入QQ号:") result=re.finda ...
- 背景图片利用backgrond-posintion属性实现不同形式的分割
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Spring Cloud的应用程序—上下文服务
Spring Boot对于如何使用Spring构建应用程序有一个看法:例如它具有常规配置文件的常规位置,以及用于常见管理和监视任务的端点.Spring Cloud建立在此之上,并添加了一些可能系统中所 ...
- Mac下部署Ionic环境
1.下载安装nodejs,可以到官网http://nodejs.org/en/download/上去下载最新版本安装,比较无脑.如果官网打不开的话可以到中文网站去下载http://nodejs.cn/ ...
- 测试使用 Open Live Writer写博客
这里测试文字的显示: 第一段 第二段 第三段 这里测试图片的显示: 这里测试视频的显示: 测试不了,是不是版权问题?
- 总结vue中父向子,子向父以及兄弟之间通信的几种方式
子向父方式1:通过props,如例子中子组件test1.vue向父组件App.vue传值 App.vue代码 <template> <div id="app"&g ...