Home Java JavaBase What are the formats of if statements?

What are the formats of if statements?

Jul 01, 2020 am 09:52 AM
if Format

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}].

What are the formats of if statements?

if statements have three formats, namely:

(recommended learning: java entry program)

1. If statement

if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}
Copy after login

2. Combined with else statement

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}
Copy after login

3. If statement with else if...else statement

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}
else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}
else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}
else {
   //如果以上布尔表达式都不为true执行代码
}
Copy after login

(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(比较表达式)
语句
Copy after login

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(比较表达式);
Copy after login

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");
		}
	}
}
Copy after login

The output result is as shown in the figure:

What are the formats of if statements?

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write if in c language to judge multiple conditions How to write if in c language to judge multiple conditions Mar 25, 2024 pm 03:24 PM

In C language, if statement is usually used to execute a specific block of code based on a single condition. However, multiple conditions can be combined to make a determination using logical operators such as &amp;&amp;, ||, and !. Including using logical AND (&amp;&amp;) to judge multiple conditions, using logical OR (||) to judge at least one condition, using logical NOT (!) to judge the negation of a single condition, as well as nesting if statements and using parentheses to clarify priority.

How to write the qq mailbox format? What is the qq mailbox format? How to write the qq mailbox format? What is the qq mailbox format? Feb 22, 2024 pm 03:40 PM

QQ email: QQ number@qq.com, English QQ email: English or numbers@qq.com, foxmail email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Tutorial Applicable Model: iPhone13 System: IOS15.3 Version: QQ Mailbox 6.3.3 Analysis 1QQ mailbox has four formats, commonly used QQ mailbox: QQ number@qq.com, English QQ mailbox: English or numbers@qq.com, foxmail Email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Supplement: What is qq mailbox? 1 The earliest QQ mailbox was only between QQ users

How to use block quotes in Apple Notes How to use block quotes in Apple Notes Oct 12, 2023 pm 11:49 PM

In iOS 17 and macOS Sonoma, Apple has added new formatting options for Apple Notes, including block quotes and a new Monostyle style. Here's how to use them. With additional formatting options in Apple Notes, you can now add block quotes to your notes. The block quote format makes it easy to visually offset sections of writing using the quote bar to the left of the text. Just tap/click the "Aa" format button and select the block quote option before typing or when you are on the line you want to convert to a block quote. This option applies to all text types, style options, and lists, including checklists. In the same Format menu you can find the new Single Style option. This is a revision of the previous "equal-width"

What is the file format of ink? What is the file format of ink? Feb 03, 2023 pm 02:32 PM

ink is the file format of a shortcut; a shortcut is a file object that is similar in function to a symbolic link, but is essentially different from a symbolic link; a shortcut is an ordinary file, not a symbol, and its extension is ".lnk" , so the shortcut can be copied, moved, changed or deleted; the shortcut can point to a file, folder or any other legal location in the system.

What is the format of pptm? What is the format of pptm? Jan 11, 2021 pm 02:46 PM

pptm is a file format of PowerPoint in the office office suite. Its full name is "macro-enabled PowerPoint presentation". pptm files can only be opened with office software of version 2007 and above. If opened with other versions of software, problems such as inability to edit and incomplete images will occur.

What format is m3u8? What format is m3u8? Feb 20, 2023 pm 01:38 PM

m3u8 is UTF-8 encoding format. M3U8 refers to the UTF-8 encoded M3U file, and the M3U file records an index plain text file. When you open it, the playback software does not play it, but finds the network address of the corresponding audio and video file based on its index for online processing. Play. M3U8 is a common streaming media format that mainly exists in the form of a file list. It supports both live broadcast and on-demand playback. It is especially commonly used on platforms such as Android and iOS.

What format file is cr3? What format file is cr3? Jun 23, 2021 pm 03:22 PM

CR3 files are original raw files recorded by Canon's new cameras using the Digic8 image processor. cr3 files can be opened through Canon ddp, which is software for performing image adjustments such as "RAW development" on RAW images.

What format is rtf What format is rtf Jan 30, 2023 pm 02:35 PM

The full name of RTF is "Rich Text Format", which means "Rich Text Format" or "Multiple Text Format" in Chinese. It is a cross-platform document format developed by Microsoft; RTF is a document format that Word can save in order to be compatible with other word processing software. , is a file similar to DOC format (Word document) and has good compatibility. For ordinary users, the RTF format is a good file format conversion tool for transferring formatted text documents between different applications.

See all articles