The IF statement of the Java Process Control

Source: Internet
Author: User

1. Process Control Statements

In the process of executing a program, the order of execution of each statement has a direct effect on the result of the program. In other words, the process of the program directly affects the results of the operation. So, we have to understand the execution flow of each statement. And sometimes we have to do what we want to do by controlling the order in which statements are executed.

Process Control Statement Classification: Sequential structure, select structure and branch structure.


2. Sequential structure

The sequential structure is the simplest and most basic process control, there is no specific grammatical structure, according to the Order of the code, executed sequentially, most of the code in the program is executed.

Overall: Written in front of the first execution, written in the back after the execution.



3. Select structure

Also known as the branching structure. The selection structure has a specific syntax rules, the code to perform specific logical operation to judge, the result of the logical operation has two, so the choice, according to different choices to execute different code.

The Java language provides two alternative structure statements: The IF statement and the switch statement.


4. Select the IF statement for the structure

Format one:

if (Boolean expression) {statement body;}

Execution process: 1) First determine whether the result of the Boolean expression is True or False 2) if true, executes the statement body 3) If false, the statement body is not executed.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/87/4C/wKioL1fb7rPxvwqAAABNm0wzCbc262.png "title=" Qq20160916210801.png "alt=" Wkiol1fb7rpxvwqaaabnm0wzcbc262.png "/>

import java.util.scanner;/** *  selection structure:  *      IF statement and switch statement The  * IF statement has three formats  *      1) if (Boolean expression) { *            Statement Body; *      } *        Execution Flow: Evaluates the value of the Boolean expression to see if the return value is true or false.  *               If true, executes the statement body;  *               If False, the statement body is not executed;  *      2)  */public class IfDemo1 {     public static void main (String[] args) {         scanner input = new scanner (system.in);         system.out.print ("Please enter the first number:");         int&Nbsp;num1 = input.nextint ();         system.out.print ("\ n Please enter a second number: ");         int num2 = input.nextint ();         if (num1 == num2) {             system.out.println (values equal to num1+ "and" +num2+ ");         }    }}

Note: 1. The Boolean expression is simple or complex, the result must be a Boolean type 2.if statement control of the body of the statement, if it is a statement, curly braces can be omitted, if it is more than one statement, it cannot be omitted. It is strongly recommended that you never omit it for readability.


Format two:

if (Boolean expression) {statement body 1;} else{statement body 2;}

Execution process: 1. First determine whether the Boolean expression evaluates to True or False 2. If true, executes the statement body 1 3. If false, the statement body 2 is executed.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/87/4C/wKioL1fb9KCTqwsNAABTGisXBWw965.png "title=" Qq20160916213318.png "alt=" Wkiol1fb9kctqwsnaabtgisxbww965.png "/>

import java.util.scanner;/** *  selection structure:  *      IF statement and switch statement The  * IF statement has three formats  *      1) if (Boolean expression) { *            Statement Body; *      } *        Execution Flow: Evaluates the value of the Boolean expression to see if the return value is true or false.  *               If true, executes the statement body;  *               If False, the statement body is not executed;  *      2) if (Boolean expression) { *            Statement Body 1; *      }else{ *            Statement Body 2; *      } *        Execution Flow: evaluates the value of the Boolean expression to see if the return value is true or false.  *               If true, the statement body is executed 1; *                If False, executes the statement body 2; */public class ifdemo1 {     public static void main (String[] args) {         scanner input = new scanner (system.in);         system.out.print ("Please enter the first number:");         int  num1 = input.nextint ();         system.out.print ("\ n Please enter a second number: ");         int num2 = input.nextint ();         if (num1 == num2) {             system.out.println (values equal to num1+ "and" +num2+ ");         }else{             system.out.println (num1+ "and" +num2+ "values are not equal");         }    }}

Note: Else there is no Boolean expression behind it, after the IF.


Format three:

if (Boolean expression 1) {statement body 1;} else if (Boolean expression 2) {statement body 2;} else{statement body N;}

Execution process: 1) The Boolean expression 1 is first judged to see if the result is true or false. 2) If true, the statement body 1 is executed. 3) If false, continue to judge the Boolean expression 2 to see if the result is true or false. 4) If true, the statement body 2 is executed. 4) If false, continue to judge the Boolean expression to see if the result is true or false. ...... 5) If no Boolean expression is true, the body of the statement in else is executed.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/87/4F/wKiom1fb-zGgy2XGAACAuvmzMT4578.png "title=" Qq20160916220116.png "alt=" Wkiom1fb-zggy2xgaacauvmzmt4578.png "/>

import java.util.scanner;/** *  selection structure: &NBSP;*&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF statement and switch statement The &NBSP;*&NBSP;IF statement has three formats  *      1) if (Boolean expression) { *            Statement Body; *      } *        Execution Flow: Evaluates the value of the Boolean expression to see if the return value is true or false.  *               If true, executes the statement body;  *               If False, the statement body is not executed; &NBSP;*&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;2) if (Boolean expression) { *            Statement Body 1; *      }else{ *            Statement Body 2; *      } *        Execution Flow: evaluates the value of the Boolean expression to see if the return value is true or false.  *               If true, the statement body is executed 1; *                If False, executes the statement body 2; *       3) if (Boolean expression 1) { *           statement body 1; *       }else if (Boolean expression 2) { *           Statement Body 2; *      }else{ *            Statement Body 3; *      } *       Execution Flow: Evaluates Boolean expression 1 first to see if its return value is TRUE or false *                If True, then execute the statement body 1; *                if False, continue to judge the Boolean expression 2 to see if its return value is TRUE or false *                If true, then long execution statement body 2; *                If it is false, then execute the statement body 3; */public class ifdemo1 {    public  Static void main (String[] args) {        scanner  Input = new scanner (system.in);         system.out.print ("Please enter first number:");         int num1 = input.nextint ();         system.out.print ("\ n Please enter a second number:");         int num2 = input.nextint ();         if (NUM1 &NBSP;==&NBSP;NUM2) {            system.out.println (num1+ "and" +num2+ "values are equal");        }else{         &nbSp;   system.out.println (num1+ "and" +num2+ "values are not equal");         }        system.out.println (10>20? ") 10 large ":" 20 large ");        int score = 88;         if (score <=100 && score >90) {             system.out.println ("excellent");         }else if (score <=89 && score >80) {             system.out.println ("good");         }else if (score <=80 && score >70) {             system.out.println ("Liang");         }else if (score <=70 && score >60 ) {           &NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Pass");        }else{    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("fail");         }    }}

This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1853368

The IF statement of the Java Process Control

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.