Java Process Control Statements

Source: Internet
Author: User
Tags switch case

first, sequential statements

Statement: A semicolon-delimited code is called as a statement

Note: No code is written just a semicolon, but also a statement, called an empty statement


Second, Judgment (If...else)

Note: If there is only one statement in the curly braces, the curly braces can be omitted

Format one:

if (judging condition) {

Code block 1 for execution;

Code block 2 for execution;

... ;

code block n executed;

}


Format two:

if (judging condition) {

Code block 1 for execution;

... ;

code block n executed;

}else{

Code block 1 for execution;

... ;

code block n executed;

}

Format three:

if (judging condition 1) {

Code block 1 for execution;

}else if (judging condition 2) {

Execute the statement;

}else if (judging condition 3) {

Execute the statement;

}

Example:

import java.util.scanner;//Import Package Import java.util.scanner, use Scanner This class, fixed usage scanner sc=new  Scanner (system.in), Int nextint = sc.nextint () Gets the number entered by the user  public class demo1 {     public static void main (String[] args)  {     scanner sc = new scanner (system.in);     system.out.println (" Please enter a number: ");     int nextint = sc.nextint ();    if  ( nextint %2 == 0)  {        system.out.println ( Nextint + ": is an even number");    } else {         system.out.println (nextint + ": is an odd number");    }     System.out.println ("over");      }}  //run result [[Email protected] java ]# javac demo1.java[[email Protected] java]# java demo1 Please enter a number: 33: It's an odd over. 

 

[[Email protected] java]# cat demo2.javaimport java.util.scanner;public class  demo2 {    public static void main (String[] args)  {         scanner sc = new scanner (System.in);         system.out.println ("Please enter a number:");         double score = sc.nextdouble ();         char  grade;        if  (score >=90)  {             grade =  ' A ';         }        else if  (score >=  80)  {            grade =  ' B ';         }        else if  (score >=  60)  {            grade =  ' C ';           }         else {            grade =  ' D ';         }         System.out.println (grade);     } } //run result [[email protected] java]#  java demo2 Please enter a number: 78.4C

 

Import java.util.scanner;public class demo3 {    public static  void main (String[] args)  {    Scanner sc = new  Scanner (system.in);     system.out.println ("Please enter Year:")     int year  = sc.nextint ();    boolean isleapyear =  (year %4 ==  0);    isleapyear = isleapyear &&  (year %100 ! = 0);     isleapyear = isleapyear | |   (year %400 ==0);    if  (isleapyear)  {         system.out.println (year + ": Is run Year");    } else {         system.out.println (year + ": Common Year");     }    } } //run Results [[email protected] JAVA]# JAVA DEMO3 Please enter the year: 20182018: Yes common year 


III. Choice of Judgment Statement (switch)

Format:

switch (expression)

{

Case takes value 1:

Execute the statement;

Break

Case takes value 2:

Execute the statement;

Break

......

Default

Execute the statement;

Break

}

Switch statement features:

1) The switch statement chooses only four types: Byte,short,int,char;

2) between case and default no order, first judge all case, no matching case execution default;

3) The switch statement stops with the following conditions: The break keyword is encountered or the curly brace of the end switch statement;

4) If the matching case or default does not have a corresponding break, then the program will continue to execute downward until a break is encountered or the end of the switch ends;

5) The value in the switch case must have the same data type as the value of the switch expression

Example:

[[Email protected] java]# cat demo4.javaimport java.util.scanner;public class  demo4 {    public static void main (String[] args)  {     scanner sc = new scanner (system.in);     double  x = sc.nextdouble ();     string z = sc.next ();     double y = sc.nextdouble ();    switch  (z)  {     case  "+":         system.out.println ("x+y=")  + (x+y));        break;    case  "-":         system.out.println ("x-y="  + (x-y));         break;    case  "*":         system.out.println ("x*y=" + (x*y));        break;    case "/":         system.out.println ("x/y="  + (x/y));         break;    default:         SYSTEM.OUT.PRINTLN ("not reliable");        break;     }       }} //Running Results [[email protected] java]# java  demo44+5x+y=9.0

Four, while loop

Description: The condition is judged first, the loop body is executed only if the condition satisfies.

Format:

while (conditional expression)

{

Execute the statement;

}

Example

[[Email protected] java]# cat demo5.javaimport java.util.scanner;public class  demo5 {public static void main (String[] args)  {int num =  ( int) (Math.random () *100) +1; Scanner sc = new scanner (system.in);int guessnum = -1; while  ( Guessnum != num)  {    system.out.println ("Please enter the number between 1-100:");     guessnum = sc.nextint ();    if  ( guessNum == num )  {       system.out.println ("in the!");      } else if  ( guessnum > num)  {        SYSTEM.OUT.PRINTLN ("Big value!");      } else {       system.out.println (" The value is small! ");      }} }} //run Results [[EMAIL PROTECTED]&Nbsp;java]# javac demo5.java[[email protected] java]# java  Demo5 Please enter the number between 1-100:12 value is small! Please enter a number between 1-100:

v. Do and loop

Note: The loop body is executed first, then the condition is satisfied, and then the loop body is resumed.

Features: The loop body is executed at least once, whether or not the condition is satisfied

Format

Do

{

Execute the statement;

}while (conditional expression);

Example

[[Email protected] java]# cat demo6.javaimport java.util.scanner;public class  demo6 {public static void main (String[] args)  {int num =  ( int) (Math.random () *100) +1; Scanner sc = new scanner (system.in); int guessnum = -1;int count =  0; do {    system.out.println ("Please enter a number between 1-100:");     Guessnum = sc.nextint ();    if  ( guessnum == num)  {        SYSTEM.OUT.PRINTLN ("In!");      } else if  ( guessnum > num)  {        SYSTEM.OUT.PRINTLN ("Big value!");      } else {       system.out.println (" The value is small! ");      }    count++;}  while  (&NBsp;guessnum != num);  system.out.println ("The number to be guessed is:"  +num + "  you guessed it altogether:"  +count  + "Times");  }} //Run result [[email protected] java]# javac demo6.java[[email  PROTECTED] JAVA]# JAVA DEMO6 Please enter the number between 1-100:10 value is big! Please enter the number between 1-100:5 value is big! Please enter the number between 1-100:4 value is big! Please enter the number between 1-100:2! The number to guess is:2  you guessed it: 4 times.

Six, for Loop

Format:

for (initialize expression; loop condition expression; post-loop action expression) {

Execute the statement;

}

Example:

[[email protected] java]# cat demo7.javapublic class demo7 {     public static void main (String[] args)  throws interruptedexception  {    for  (int i=1;i<=9;i++)  {         for  (int j=1;j<=i;j++)  {             system.out.print (i + "*"  +j + "="  + (i*j)  + "\ T");             thread.sleep (;      )       }            System.out.println (" ");        }    }}   //run Results [[email protected] java]# javac demo7.java[[email protected] java]#  java demo71*1=1   2*1=2  2*2=4   3*1=3  3*2=6  3*3=9    4*1=4  4*2=8  4*3=12 4*4=16  5*1=5  5*2=10 5*3=15  5*4=20 5*5=25  6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36   7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49  8*1 =8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64  9*1=9   9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

Seven, break, continue keyword

Break: Statement following the termination statement

Continue: Jump out of this loop and perform the Next loop

Note: If continue appears at the end of the loop (the last statement), you can omit the

Java Process Control Statements

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.