Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Logical Control statements in Java basic Grammar

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "logic control statements in Java basic syntax". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "logic control statements in Java basic syntax" together!

directory

Logic Control in Java Basic Syntax

I. Logic control statements

1. sequential structure

2. branching structure

3. loop structure

II. Input and output mode

1. printed to the console

2. from the key input

Third, guess the number game

Logic Control in Java Basic Syntax 1. Logic Control Statements

1. sequential structure

Like the code we write, it executes line by line from top to bottom. This z is the order structure, different order, the result may be different. as

System.out.println(1)System.out.println(2) System.out.println(3)

The code results in 1 (newline) 2 (newline) 3, but if you change the order, the result will change.

System.out.println(2)System.out.println(3) System.out.println(1)

the result of this code become 2 (newline) 3 (newline) 1

2. Branching structure 2.1 if statements

Like C, there are three basic forms of the if statement in Java.

the form

if(Boolean expression){ //Code executed when condition is met}

Form II

if(Boolean expression){ //code executed when condition is met}else{ //Code executed if condition is not met}

Form III

if(Boolean expression 1){ //code executed when condition 1 is satisfied}else if(Boolean expression 2){ //code executed when condition 2 is met}else{ //Code executed when none of the conditions are met}

But from the form we can see that there are still some different

Conditional expressions must be Boolean values and cannot use a numeric value directly like C language.

For example:

int a = 10;if(a){ System.out.println(a);}

This code is wrong, the condition must be Boolean

int a = 10;if(a > 0){ System.out.println(a);}

This code is correct.

Code style issues, such as when I write C language like to put a pair of braces juxtaposed at the beginning of the line. But in Java, it is recommended to put {if} or {else} in the same line.

Also note the dangling else problem, remembering that else matches the closest if.

For example:

int a = 2;if(a>0) if(a>10) System.out.println("a > 10");else System.out.println("a > 0 && a

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report