What are the forms and usages of if statements in java
Form 1: if statement
The if statement means that if a certain condition is met, a certain process will be performed. For example, Xiao Ming’s mother said to Xiao Ming, “If you get 100 points in the exam, I will take you to the amusement park on Sunday.” This sentence can be described by the following pseudocode.
If Xiao Ming got 100 points in the exam
Mom took Xiao Ming to the amusement park on Sunday
In the above pseudocode, "if" is equivalent to the keyword if in Java, "Xiao Ming took the exam "Scored 100 points" is the judgment condition and needs to be enclosed in (). "Mom took Xiao Ming to the amusement park on Sunday" is the execution statement and needs to be placed in {}. The modified pseudocode is as follows:
if(小明考试得了100分){ 妈妈星期日带小明去游乐场 }
The above example describes the usage of the statement. In Java, the specific syntax format of the if statement is as follows:
if(条件语句){ 代码块 }
Above In the format, the judgment condition is a Boolean value. When the judgment condition is true, the execution statement in {} will be executed.
Form 2: if...else statement
The if...else statement means that if a certain condition is met, a certain process will be performed, otherwise another process will be performed. . For example, to determine the parity of a positive integer, if the number is divisible by 2, it is an even number, otherwise the number is an odd number. The specific syntax format of the if...else statement is as follows:
if(判断条件){ 执行语句1 }else{ 执行语句2 }
In the above format, the judgment condition is a Boolean value. When the judgment condition is true, the execution statement 1 below will be executed. When the judgment condition is false, execution statement 2 in {} after else will be executed.
Form 3: if…else if…else statement
if…else The if…else statement is used to judge multiple conditions and perform a variety of different processes. . For example, a student's test scores are graded. If the score is greater than 80 points, the grade is excellent. Otherwise, if the score is greater than 70 points, the grade is good. Otherwise, if the score is greater than 60 points, the grade is medium. Otherwise, the grade is poor. . if...else The specific syntax format of the if...else statement is as follows:
if(判断条件1){ 执行语句1 } else if(判断条件2){ 执行语句2 } … else if(判断条件n){ 执行语句n }else{ 执行语句n+1 }
In the above format, the judgment condition is a Boolean value. When the judgment condition 1 is true, the execution statement 1 in the following {} will be executed. When judgment condition 1 is false, judgment condition 2 will continue to be executed. If it is true, sentence 2 will be executed, and so on. If all judgment conditions are false, it means that all conditions are not satisfied. The { in else follows Execution statement n 1 will be executed.
Recommended tutorial: java introductory tutorial
The above is the detailed content of What are the forms and usages of if statements in java. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
