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. 从github上下载项目到eclipse

    第一步:把代码下载到本地的仓库中 到github后选择自己想下载的项目,拷贝它的URL,图示如下: 进入eclipse中  点击后如下:  继续 按照图片指示继续(大白菜next教程)     fin ...

  2. VS2012/13中即将增加InstallShield升级版

    对于Visual Studio 2012去掉了前作中的安装程序(Installer)项目模板,许多开发者都感到非常失望.这个流行的项目类型为开发者们提供了若干选项:除了InstallShield LE ...

  3. 国际化模块 angular-translate 简单方便快捷翻译中英文等多语言环境

    很多web服务面对的不仅仅是当地用户,多语言环境不仅能提升逼格,更重要是一种用户体验. angular.js 作为前后端拆分的解决方案之一,当然离不开前端框架处理国际化的问题,angular.js 官 ...

  4. CSS选择器、层叠相关的基础知识

    CSS是Cascading Style Sheets的英文缩写,即层叠样式表.CSS2.1是W3C于2007年发布,现在推荐使用的.CSS3现在还处于开发中,有部分浏览器的新版本支持. 1. CSS ...

  5. 算法练习:最小生成树 (Minimum Spanning Tree)

    (注:此贴是为了回答同事提出的一个问题而匆匆写就,算法代码只求得出答案为目的,效率方面还有很大的改进空间) 最小生成树是指对于给定的带权无向图,需要生成一个总权重最小的连通图.其问题描述及算法可以详见 ...

  6. RhinoMock学习-Stub方法

    // Arrange var stub = MockRepository.GenerateStub<IDemo>(); stub.Stub(x => x.StringArgStrin ...

  7. 【Android】9.2 内置行视图的分类和呈现效果

    分类:C#.Android.VS2015: 创建日期:2016-02-18 一.简介 Android内置了很多行视图模板,在应用程序中可直接使用这些内置的视图来呈现列表项. 要在ListView中使用 ...

  8. linux系统资源网站

    http://upstream.rosalinux.ru/    API/ABI changes analysis for C/C++ libraries

  9. ny269 VF

    VF 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Vasya is the beginning mathematician. He decided to make ...

  10. Spark性能优化指南——基础篇转

    前言 在大数据计算领域,Spark已经成为了越来越流行.越来越受欢迎的计算平台之一.Spark的功能涵盖了大数据领域的离线批处理.SQL类处理.流式/实时计算.机器学习.图计算等各种不同类型的计算操作 ...