The if statement has three formats, namely: 1. [if (Boolean expression) {execution statement}]; 2. [if (Boolean expression) {execution code} else {execution code}]; 3. [if (Boolean expression) {execution code} else if (Boolean expression) {execution code} else {execution code}].
if statements have three formats, namely:
(recommended learning: java entry program)
1. If statement
if(布尔表达式){ //如果布尔表达式为true将执行的语句 }
2. Combined with else statement
if(布尔表达式){ //如果布尔表达式的值为true }else{ //如果布尔表达式的值为false }
3. If statement with else if...else statement
if(布尔表达式 1){ //如果布尔表达式 1的值为true执行代码 } else if(布尔表达式 2){ //如果布尔表达式 2的值为true执行代码 } else if(布尔表达式 3){ //如果布尔表达式 3的值为true执行代码 } else { //如果以上布尔表达式都不为true执行代码 }
(Video tutorial recommended : java video tutorial)
Note:
1. Whether the comparison expression is simple or complex, the result must be of Boolean type.
2. If there is only one statement in the statement body controlled by the if statement, the braces can be omitted; if there are multiple statements, they cannot be omitted. It is recommended that they should never be omitted.
For example:
if(比较表达式) 语句
3. Generally speaking: there is no semicolon when there is an opening brace, and there is no opening brace when there is a semicolon.
For example:
if(比较表达式){........}, if(比较表达式);
For example:
public class HelloWorld { public static void main(String[] args) { int i=10; int j=8; /* * if语句第一种格式 * */ if(i>j){ System.out.println("我是if语句第一种格式"); } /* * if语句第二种格式 * */ if(i<j){ System.out.println("我是if语句第二种格式1"); }else{ System.out.println("我是if语句第二种格式2"); } /* * if语句第三种格式 * */ if(i<j){ System.out.println("我是if语句第三种格式1"); }else if(i>j){ System.out.println("我是if语句第三种格式2"); }else{ System.out.println("我是if语句第三种格式3"); } } }
The output result is as shown in the figure:
The above is the detailed content of What are the formats of if statements?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool