Java Process Control Statements

Source: Internet
Author: User

1.1 If statement:

The IF statement refers to some sort of processing if a certain condition is met.

1  Public classIfdemo {2      Public Static voidMain (string[] args) {3         intx = 5;4         if(X < 10) {5X + +;6         }7 System.out.println (x);8     }9}

Operation Result:

1.2 If ... Else statement:

A if...else statement is a process that is performed if a certain condition is met, otherwise it is handled in another way. For example, to determine the parity of a positive integer, if the number can be divisible by 2 is an even number, otherwise the figure is an odd. the specific syntax format for the IF...ELSE statement is as follows:

1  Public classifelsedemo{2      Public Static voidMain (string[] args) {3          inti = 16 ;4          //Judging variable, is odd even, divided by 2, see remainder 0 15          if(i% 2 = = 0 ){6System.out.println (i+ "is even");7}Else{8System.out.println (i+ "is odd");9          }Ten     } One}

Operation Result:

1.3 If ... else if ... .. Else statement:

The If...else if...else statement is used to judge multiple conditions and perform many different kinds of processing. For example, the grading of a student's test scores, if the score is greater than the grade of the superior, otherwise, if the score is greater than the grade of good , otherwise, if the score is greater than 60 grade is in, otherwise, the rating is poor. the specific syntax format for the if... else if...else statement is as follows:

1 /*2 if else if Else statement3 suitable for the realization of multi-condition judgment in the program4 Writing format:5 if (condition) {6 If execution body7 }else if (condition) {8 If execution body9 }else if (condition) {Ten If execution body One }else{ A else's execution body -      } - If the condition in the if is true, the If execution body the if the condition is false, execute the else execution body - A statement that contains multiple if, as long as there are 1 if the condition is true, the other code is not executing - */ -  Public classifelseifdemo{ +      Public Static voidMain (string[] args) { -         //The result judgment request, the result >80 the result >70 the result >60 inferior lattice +         //define variables, save scores A         intGrade = 75; at         //use if ELSE if statement to judge scores -         if(Grade > 80 ){ -System.out.println (grade+ "score is excellent"); -}Else if(Grade > 70){ -System.out.println (grade+ "score is good"); -}Else if(Grade > 60){ inSystem.out.println (grade+ "score is in"); -}Else{ toSystem.out.println (grade+ "score is poor"); +         } -     } the}

Operation Result:

1.4 selection Structure If statement and ternary operation transformation

The ternary operator, similar to the If-else statement, has the following syntax:

1 judging conditions? Expression 1: Expression 2

The ternary operator gets a result that is usually used to assign a variable, and when the condition is determined, the result of the operation is the value of expression 1, otherwise the result is the value of expression 2

1 /*2 Replace if statement and ternary operator3 Requirement: Already known two number, calculate maximum value4 two integers, compare size5    6 use if or ternary7 Judging conditions are many, using if8 ternary, must have the result, if can have no result9 */Ten  Public classifelsedemo_1{ One      Public Static voidMain (string[] args) { A         inti = 15; -         intj = 6; -         //use the IF statement to determine the maximum value the         if(i>j) { -System.out.println (i+ "is the maximum value"); -}Else{ -System.out.println (j+ "is the maximum value"); +         } -          +         //using ternary arithmetic to implement A         intK = i>j?i:j; atSystem.out.println (k + "is the maximum value"); -     } -}

Operation Result:

Java Process Control Statements

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.