Get the App
SLTechnology News&Howtos  ›  Development  › 

Logical Control statements in Java basic Grammar

Shulou Source: shulou.com Published: 2022-06-03 19:09:44 09月23日 Update

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

Tags: Code input language loop character expression statement Boolean output control condition logic structure syntax content number foundation string that is form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Linux Xiaomi Huawei