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的更多相关文章

  1. 30行代码搞定WCF并发性能测试

    [以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main()         {               List< ...

  2. 30天代码day2 Operators

    Operators These allow you to perform certain operations on your data. There are 3 basic types: Unary ...

  3. 30行代码让你理解angular依赖注入:angular 依赖注入原理

    依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...

  4. 30行代码实现Javascript中的MVC

    从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...

  5. Tensorflow快餐教程(1) - 30行代码搞定手写识别

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/details ...

  6. 10分钟教你用python 30行代码搞定简单手写识别!

    欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 手写笔记还是电子笔记好呢? 毕业季刚结束,眼瞅着2018级小萌新马上就要来了,老腊肉小编为了咱学弟学妹们的学习,绞尽脑汁准备编一套大学秘籍, ...

  7. JAVA_SE基础——30.构造代码块

    黑马程序员入学blog...构造代码块作用:给所有的对象进行统一的初始化. 问题:要求每个小孩出生都会哭,这份代码有两个构造函数,如果需要每个小孩出生都要哭的话,那么就需要在不同的构造函数中都调用cr ...

  8. 30 行代码实现 JS 中的 MVC

    一连串的名字走马观花式的出现和更迭,它们中一些已经渐渐淡出了大家的视野,一些还在迅速茁壮成长,一些则已经在特定的生态环境中独当一面舍我其谁.但不论如何,MVC已经并将持续深刻地影响前端工程师们的思维方 ...

  9. 30行代码消费腾讯人工智能开放平台提供的自然语言处理API

    腾讯人工智能AI开放平台上提供了很多免费的人工智能API,开发人员只需要一个QQ号就可以登录进去使用. 腾讯人工智能AI开放平台的地址:https://ai.qq.com/ 里面的好东西很多,以自然语 ...

随机推荐

  1. 给listview加动画,让动画执行结束后再刷新

    问题:当给listview的条目加动画时,例如添加一个条目或者移除一个条目,动画效果会和添加删除条目的逻辑同时进行,因为动画并不是阻塞式的,这样会造成动画还没有结束,条目已经添加或者移除,从而动画作用 ...

  2. Saiku数据库迁移后的刷新脚本-Shell脚本读取数据库中的数据(二十三)

    Saiku数据库迁移后的刷新脚本 之前有谈过对saiku中的数据进行刷新,因为saiku默认会从缓存中查询数据,但是配置不使用缓存又会效率低下... 所以这里就需要做一个数据刷新,每次ETL之后都需要 ...

  3. 编译安装LAMP

    编译安装MariaDB 创建MariaDB安装目录.数据库存放目录.建立用户和目录 先创建一个名为mysql且没有登录权限的用户和一个名为mysql的用户组,然后安装mysql所需的依赖库和依赖包,最 ...

  4. 简单的Java ee思维导图

  5. 比较爬虫用的语言Python与Go

    Python是我比较喜欢的语言,莫名的喜欢,对Python的学习可能起初是敲错了网址开始的,哈哈哈~ 工作的任务从一个网站后台做登录.爬取数据,写入服务器Redis中,同事认为我会用PHP来写,哼!让 ...

  6. js问题解决——cannot read property style of undefind

    当遇到cannot read property style of undefine的问题如下: 那就说明你设定的这个变量名为空 比如我在文档里写的属性名为list_box 但是我获取的属性名称写错了 ...

  7. 实训任务04 MapReduce编程入门

    实训任务04 MapReduce编程入门 1.实训1:画图mapReduce处理过程 使用有短句“A friend in need is a friend in deed”,画出使用MapReduce ...

  8. git clone 带用户名密码

    git使用用户名密码clone的方式: git clone http://username:password@remote eg: username:  abc@qq.com, pwd: test, ...

  9. Git通过密钥对远程仓库上传和更新详细操作

    1,先到ssh中ls查看之前本地生成的公钥和私钥,然后将别人的密钥替换掉自己的密钥,这里我把别人的密钥放在d:/desktop/id_rsa 中,利用cp /D/Desktop/id_rsa  id_ ...

  10. Java语法基础学习DayTwentyOne(网络编程)

    一.IP地址和端口号 1.作用 通过IP地址,唯一的定位互联网上一台主机. 端口号标识正在计算机上运行的进程,不同进程有不同的端口号,被规定为一个16位的整数0~65535,其中0~1023被预先定义 ...