Java Learning (iv): Java Process Control statements (sequential structure, if condition statement, switch conditional statement, loop statement, and jump statement)

Source: Internet
Author: User
Tags java keywords

Java Process Control Statements


This blog post will focus on the control statements that make up the various structures in Java, the main content of which is the emphasis of the process control statements that differ from C/s.


1. Sequential statements


The statements in the sequential structure can be divided into three categories: expression statement , empty statement and compound statement .

Note:

(1), the empty statement is mainly used as an empty loop body, the syntax format is as follows:

;//is actually a semicolon

Executes an empty statement, which is to go to the end point of the statement. Thus, if an empty statement is reachable, the end point of the empty statement is also reachable.

(2), the compound statement is also called the statement block, if the statement block is empty, then the control goes to the end point of the statement block.


2. If condition statement


The conditional statements in Java are consistent with C + +, and there is not much difference.

Example code:

/******************************************************** * "If condition statement" Sample code * Function: Enter three number, output maximum value */import Java.util.Scanner ;p Ublic class Program {public static void main (String [] args) {int num1,num2,num3,max; Scanner input = new Scanner (system.in); System.out.println ("Please enter first number:"); Num1=input.nextint (); System.out.println ("Please enter a second number:"); Num2=input.nextint (); System.out.println ("Please enter a third number:"); Num3=input.nextint (); max=num1;if (Num2>max) max=num2;if (Num3>max) max=num3; System.out.println ("max=" +max); Input.close ();}}



3. Switch condition statement

The IF Else statement can be used to describe a "two-fork intersection" where switch can be used for "multiple fork junctions".

The switch statement is a multi-branch selection statement, which is commonly used to select the statement to execute based on the value of an expression, with the following basic syntax:

<pre name= "code" class= "Java" >switch (expression) {case value 1: statement block 1;break;case Value 2: statement block 2;break;case Value 3: statement block 3;break;......case Value N: statement block N;break;}

Where switch represents a toggle, case indicates, default means defaults, break means stop, and they are all Java keywords.

/******************************************************** * "switch statement Combat Walkthrough"--judging constellation */import Java.util.Scanner according to birth date; public class Program {public static void main (String [] args) {System.out.println ("Please enter your date of birth (if 0123 means January 23):"); Scanner sc=new Scanner (system.in); int monthday=sc.nextint (); int month=monthday/100;int day=monthday%100; String xingzuo= "";//Save String Switch (month) {case 1:xingzuo=day<21? ") Capricorn ": Aquarius"; Break;case 2:xingzuo=day<20? " Aquarius ": Pisces"; break;case 3:xingzuo=day<21? " Pisces ": Aries"; Break;case 4:xingzuo=day<21? " Aries ":" Taurus "; break;case 5:xingzuo=day<22?" Taurus ": Gemini"; break;case 6:xingzuo=day<22? " Gemini ":" Cancer "; Break;case 7:xingzuo=day<23?" Leo ":" Leo "; Break;case 8:xingzuo=day<24?" Capricorn ": Virgo"; break;case 9:xingzuo=day<24? " Virgo ":" Libra "; Break;case 10:xingzuo=day<24?" Libra ":" Scorpio "; Break;case 11:xingzuo=day<23?" Scorpio ": Sagittarius"; break;case 12:xingzuo=day<22? " Sagittarius ":" Capricorn "; break;default:break;} System.out.println ("Your constellation is" +xingzuo);}}



4. Circular statements

The looping statements in Java are similar to the loop statements in C, mainly while/do-while/for and foreach.


4.1. While statement

The syntax of the while is as follows:

<pre name= "code" class= "Java" >while (expression) {statement block;}

4.2. Do-while Statement

The Do-while statement first executes the loop body and then evaluates the conditional expression, which has the following grammatical form:

do {statement block;}while (conditional expression);

Note: Do it first and then judge


4.3. For statement

The For statement is suitable for repeating a predetermined number of iterations of a statement block with the following syntax:

for (expression 1; expression 2; expression 3) {statement block;}

Note: The For statement is divided into three cases where the expression 1 or 2 or 3 is empty, regardless of which part of the expression is missing, can be supplemented elsewhere in the program, thus maintaining the integrity of the FOR Loop statement, so that the loop is normal.



4.4. foreach statement

The foreach statement is a special simplified version of the for statement, but the foreach statement cannot completely replace the For statement, however, the foreach statement can be rewritten as a version of the for statement. It is important to note that foreach is not a keyword, but it is customary to refer to this special for statement as a foreach statement.

The foreach statement provides a great convenience to developers in iterating over arrays and collections, with syntax in the following format:

For (type variable name: Collection) {statement block;}
Sample code

/******************************************************** * "foreach Loop statement" Using example and code */public class Program {public static void Main (String [] args) {string[] fruits = {"Apple", "orange", "banana", "watermelon", "pear", "other"}; System.out.println ("Hot-selling fruit:"); for (String fruit:fruits) {System.out.println (fruit+ ",");}}}


4.5, actual combat--99 multiplication tables

The code examples are as follows:

/* * "combat-99 multiplication Table" */public class program {public static void main (String [] args) {System.out.println ("99 multiplication Table:"); for (int i=1;i<=9;i++) {for (int j=1;j<=i;j++) {System.out.print (j+ "*" +i+ "=" +j*i+ "\ T");} Note the difference between print and println System.out.print ("\ n");//system.out.println ();}}}



5. Jump Statement


Strictly speaking, jump statements are not part of a Process control statement, but they can help programmers control the entire process more precisely, such as using a jump structure to end a loop when encountering a dead loop.

Java provides the same jump structure control statements as other languages, including: Return,break and continue.


5.1, "return statement, break statement, continue statement" The difference and contact

The return statement terminates the execution or exit method of the program and returns control to the caller of the method. If this method has a return type, the return statement must return a value of this type, and if none, a return statement without an expression can be used.

The break statement is used in a loop statement or conditional statement to terminate a loop statement, causing the control flow to jump to the next statement in the Loop statement.

The Continue statement represents a loop that exits the current loop and executes the next time, for loop statements and switch statements. It differs from break in that the break statement exits the entire loop statement, and the continue statement simply exits the current loop and proceeds to the next loop.

/******************************************************** * "Break use method and sample Code" * Function: The user randomly enter a number, the sum is greater than 100 after terminating the exit */import Java.util.scanner;public class Program {public static void main (String [] args) {int sum=0,num=0;for (;;) {System.out.println ("Please enter a number:"); Scanner SC =new Scanner (system.in); Num=sc.nextint (); sum+=num;if (sum>100) break;} System.out.println ("sum=" +sum);} /* * "continue statement" use example and code * Function: Reject or disable one of the arrays, that is, do not want to display it in the result */public class Program{public static void Main (String [] args) {Stri ng[] Users ={"AAA", "BBB", "CCC", "DDD", "EEE"}; SYSTEM.OUT.PRINTLN ("Valid output:"); for (String user:users) {if (user== "CCC") continue; System.out.print (user+ ",");}}}


5.2, actual combat--break realization Goto function


The break statement can implement Goto functionality, and Java defines an extended form of break statements to handle the problem of exiting a deeply nested loop. At the same time, this extended break statement has a label that explicitly specifies where execution should start again. The general format of the label break statement is as follows:

Break label;

/* * "combat"--break implementation goto function */public class program{public static void Main (String [] args) {int i;outer:for (i=0;i<10;i++) { if (i<7) {System.out.println ("External goto function"); continue outer;} if (i==5 | | i==8) {System.out.println ("x=8"); break outer;}}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Learning (iv): Java Process Control statements (sequential structure, if condition statement, switch conditional statement, loop statement, and jump statement)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.