The if statement encloses some code which is executed only if a condition is true. The general syntax of the if statement is:

    if (condition) {
body-code
}

The if statement has two parts. The condition is a boolean expression which resolves to either true or false. The body-code is code that will be executed only if the condition is true. Here is an example of the if statement.

    // Returns 0 if s is null
public int getLength(String s) {
if (s == null) {
// This line of code is execute only if s is null
return 0;
}
return s.length();
}

The if statement also has an optional else part which encloses some code that is executed only if the condition is false. Here is the general syntax of the if statement with the optional else part:

    if (condition) {
body-code
} else {
else-code
}

The else-code is executed only if condition resolves to false. Here is an example of the if statement with the optional else part.

    // Generates a random number and prints whether it is even or odd
if ((int)(Math.random()*100)%2 == 0) {
System.out.println("The number is even");
} else {
System.out.println("The number is odd");
}

The else-code part can also be another if statement. An if statement written this way sets up a sequence of condition's, each tested one after the other in top-down order. If one of the conditions is found to be true, the body-code for that if statement is executed. Once executed, no other conditions are tested and execution continues after the sequence. Here is an example of a sequence of if/else/if statements

    // Converts the time, which is in milliseconds, into a
// more readable format with higher units
public String getTimeUnit(long time) {
String unit = "ms";
if (time >= 365*24*60*60*1000L) {
unit = "years";
time /= 365*24*60*60*1000L;
} else if (time >= 24*60*60*1000) {
unit = "days";
time /= 24*60*60*1000;
} else if (time >= 60*60*1000) {
unit = "hr";
time /= 60*60*1000;
} else if (time >= 60*1000) {
unit = "min";
time /= 60*1000;
} else if (time >= 1000) {
unit = "sec";
time /= 1000;
}
// Execution continues here if any of the conditions match
return time + " " + unit;
}

Here's some sample output:

    getTimeUnit(123);                  // 123 ms
getTimeUnit(1234)); // 1 sec
getTimeUnit(12345)); // 12 sec
getTimeUnit(123456)); // 2 min
getTimeUnit(1234567)); // 20 min
getTimeUnit(12345678)); // 3 hr
getTimeUnit(123456789)); // 1 days
getTimeUnit(1234567890)); // 14 days
getTimeUnit(12345678900L)); // 142 days
getTimeUnit(123456789000L)); // 3 years
Related Examples

e1086. if/else语句的更多相关文章

  1. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  2. whdxlib

    1 数据库系统实现 实 验 指 导 书 齐心 彭彬 计算机工程与软件实验中心 2016 年 3 月2目 录实验一.JDBC 应用程序设计(2 学时) ......................... ...

  3. 【.net 深呼吸】细说CodeDom(2):表达式、语句

    在上一篇文章中,老周厚着脸皮给大伙介绍了代码文档的基本结构,以及一些代码对象与CodeDom类型的对应关系. 在评论中老周看到有朋友提到了 Emit,那老周就顺便提一下.严格上说,Emit并不是针对代 ...

  4. 将表里的数据批量生成INSERT语句的存储过程 增强版

    将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件 ...

  5. mysql学习之 sql语句的技巧及优化

    一.sql中使用正则表达式 select name,email from user where email Regexp "@163[.,]com$"; sql语句中使用Regex ...

  6. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...

  7. MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...

  8. Oracle 数据库语句大全

    Oracle数据库语句大全 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CH ...

  9. MyBatis源码分析(二)语句处理器

    StatementHandler 语句处理器,主要负责语句的创建.参数的设置.语句的执行.不负责结果集的处理. Statement prepare(Connection connection, Int ...

随机推荐

  1. bzoj 1860: [Zjoi2006]Mahjong麻将 题解

    [原题] 1860: [Zjoi2006]Mahjong麻将 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 211  Solved: 122 [Subm ...

  2. gulp#4.0

    gitbook教程: https://dragon8github.gitbooks.io/gulp-webpack/content/an-zhuang-gulp-4-0.html gulpfile.j ...

  3. NodeJS写日志_Log4js使用详解

    今天和大家分享一下NodeJS中写日志的一个常用第三方包:Log4js. 跟随主流Blog特色,先简单介绍下Log4js的基本信息.介绍Log4js之前,需要先说一下Log4***,Log4***是由 ...

  4. vmware esxi 过期,激活

    首先我们打开vSphere Client,登录esxi主机 他会提示你,说你的esxi主机的评估期还剩多长时间 我们现在去激活,我们下载esxi的注册机 然后点击配置--->已获许可的功能--- ...

  5. opencv源代码之中的一个:cvboost.cpp

    我使用的是opencv2.4.9.安装后.我的cvboost..cpp文件的路径是........\opencv\sources\apps\haartraining\cvboost.cpp.研究源代码 ...

  6. Android Things专题2 硬件介绍

    文| 谷歌开发人员技术专家, 物联网方向 (IOT GDE) 王玉成(York Wang) 经过2016年Brillo首批开发人员的反馈,以及市场调研,为了照应广大Android开发人员的习惯,形成了 ...

  7. ModelSim之tcl自动化仿真

    摘要: ModelSim的tcl最大的优势就在于它可以让整个仿真自动运行,免除每次进行各种用户界面控制操作的麻烦.用tcl就可以自动完成建库.映射库到物理目录.编译源代码.启动仿真器.运行仿真等一系列 ...

  8. [gj]HK一行所见闻

    香港一行 20多年来,未未去过HK,前段时间由于工作关系去了趟HK.感触良多. 一清早,福田过关,做火车,做地铁,一通到了目的地. 总结对那边的印象: 1,所有人都是粤语,包括工作交流.而且他们不怎么 ...

  9. android笔记一 控件属性

    <?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android= ...

  10. feginclient demo

    1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...