search
HomeJavajavaTutorialJava--program flow control
Java--program flow controlJun 25, 2017 am 10:56 AM
javaBasecontrolprocessprogram

Java program flow control (Part 1)

Java programs are generally divided into three process control structures: sequential structure, branch structure, and loop structure

  • Sequential structure

The program is executed line by line from top to bottom, without any jumps or judgment statements in the middle.

The sample code is as follows:

 1 public class TestSortStruc { 2     public static void main(String[] args) { 3         //流程控制:顺序结构 4         int i = 15; 5         int j = i + 1; 6         System.out.println(j); 7          8         /*错误示例,因为n的赋值语句使用到了m,所以不能将m在n后面定义 9          * int n = m + 1;10            int m = 10;11            System.out.println(n);*/12     }13 }

  • Branch structure

Selectively execute a certain code block based on conditions.

It is divided into two types of branch statements: if...else and switch..case.

1. Three structures of if statements:

1. if(true){

Execution code block;}

The sample code is as follows:

1 public class TestIf1 {2     public static void main(String[] args) {3         if(true){4             System.out.println("Hello World!!");5         }6     }7 }

 2. if (conditional judgment statement){

##  Execution code block;}

else{

Execution code block;}

The sample code is as follows:

 1 public class TestIf2 { 2     public static void main(String[] args) { 3         int age = 21; 4         if(age>18){ 5             System.out.println("你已经成年了!!"); 6         }else{ 7             System.out.println("你还没有成年!!"); 8         } 9     }10 }

##  3. if (conditional judgment statement) {

  Execution code block;}

## else if (conditional judgment statement) {

Execution code block;}

##  ……

##   else{Execute code block;}

 1 public class TestIf3{ 2     public static void main(String[] args) { 3         int age = 26; 4         
 5         if(age > 130 || age <strong><strong></strong> Obtain the value through the keyboard, then use the if judgment statement to judge the student's score, and use the Scanner object to allow the user to enter the value on the console</strong><div class="cnblogs_code"></div> <p>The sample code is as follows:<strong></strong> </p><p></p><pre class="brush:php;toolbar:false"> 1 /*题目: 2  * 从键盘输入自己的考试成绩 3  * 当成绩为100分时,奖励一台外星人电脑 4  * 当成绩在80~99时,奖励一部iPhone7 plus 5  * 当成绩在60~80时,奖励一本考试科目的习题册 6  * 成绩低于60时,没有奖励,需要连续三个月不许玩游戏看电视。*/ 7 //1.导入Scanner包,记住一定要是java.util下的Scanner包 8 import java.util.Scanner; 9 10 public class TestScanner {11     public static void main(String[] args) {12         //2.new 一个Scanner对象sc13         Scanner sc = new Scanner(System.in);14         /*3.从键盘获取用户输入的值,因为要判断成绩,15             所以规定输入的值是int类型的数字,使用nextInt()方法*/16         System.out.println("请输入你的成绩:");17         int grade = sc.nextInt();18         //使用if判断语句进行成绩奖励判断19         if(grade == 100){20             System.out.println("恭喜你,获得一台外星人电脑!!");21         }else if(grade = 80){22             System.out.println("恭喜你,获得一部iPhone7 plus!!");23         }else if(grade =60 ){24             System.out.println("恭喜你,你需要完成一本本学科的习题册!!");25         }else{26             System.out.println("很遗憾,你在未来的三个月不能玩游戏,看电视!!");27         }28     }29 }
Note: 1.if condition judgments can be nested;

  2. 1) If there is a "mutually exclusive" relationship between multiple conditions, then the order of the conditional statements is free;

    2) If There is an "inclusive" relationship between multiple conditions, so the conditions requiring a smaller range should be written above the conditions with a larger range.

 

2. switch...case statement:

 switch(

expression

){ case constant 1:

Statement 1;

Break;

## case Constant 2 :

          

       

 ……

Case constant n:

  Statement n;

  break;

 default:

        

##        

 }

Based on the value of the expression, select the corresponding case to judge. Once the case condition is met, execute the corresponding case statement.

The data types of the expression value in parentheses after switch include: char, byte, short, int, enumeration, String (String can only be used with JDK1.7 or above type).

The constant followed by case can only be an exact value, not a range of values.

The sample code is as follows:

 1 public class TestSwitch { 2     public static void main(String[] args) { 3         int i=1; 4         switch(i){ 5         case 0: 6             System.out.println("zero"); 7             break; 8         case 1: 9             System.out.println("one");10             break;11         case 2:12             System.out.println("one");13             break;14         case 3:15             System.out.println("one");16             break;17         case 4:18             System.out.println("one");19             break;20         default:21             System.out.println("ending");22             break;23         }24     }25 }

Note: If there is no break or the end has been reached, other case statements will continue to be executed. If you only want to get a certain value, you need to add the break keyword; although default is the end statement, it can It is written before or after any case in the switch, but it cannot be written inside the case, but it is habitually placed at the end. Default does not need to be written.

  • Loop structure

Through the loop condition, a certain code block is repeatedly executed until the The condition is not met.

It is divided into three types of loop statements: while, do..while, and for loop.

 Note: The foreach loop is provided in JDK1.5, which is more convenient for convenience collections and array elements.

The loop structure is the most commonly used and the most important, so it will be sorted out separately in the second chapter.

The above is the detailed content of Java--program flow control. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

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),