Logic Control Based on JAVA

Source: Internet
Author: User

Overview: Keywords of Java statements (Process Control and logical control): if, switch, while, do while, for, break, continue, return [java] package com. cxy. base;/*** @ author cxy */public class BaseTest2 {public static void main (String [] args) {int I = 1; // The simplest if, of course, here else can not write if (I = 1) {System. out. println ("I equals 1");} else {System. out. println ("I:" + I);} // nested if: An if block or else block contains the if statement. If (I <2) {if (I = 1) {System. out. println ("I equals 1");} else {System. out. println ("I less than 2 but not equal to 1") ;}} else {System. out. println ("I greater than 2");} // multiple if: Multiple if (Example A) if (I = 1) {/* your operations */} if (I = 2) {/* your operations */} // if-else if (example B) if (I = 1) {/* your operations */} else if (I = 2) {/* your operations */}/* note the difference between Example A and example B. * If instance A is equal to or not equal to 1, the system will judge whether I is equal to or not 2 * for example, when I is not equal to 1, the program will judge that I is not equal to 2 *. In fact, I personally feel that the if-else if mode is multiple if, it can also be said that it is nested if, which has both features, but more strictly speaking, it is nested ** // if-else, if there are many situations, the code will be very bloated, at this time, you can consider using switch (I) {case 1: {// your break operation;} case 2: {// your operation break;} default: break ;} /* Explanation: * 1. if I (the variable in the switch bracket) is equal to 1 (case 1), execute the code block under this case. If not, execute the default code block * 2. the variables in the switch brackets can only be of the byte, short, char, and int types. However, from Java 7, they can be of the String type * 3. we recommend that you have a break for each case method. Otherwise, unexpected results will appear (unless you know the result is the same) * 4. the following example can only be run in Java 7. Please comment out all the breaks and then view the result **/String flag = "abc"; switch (flag) {case "aaa ": {System. out. println ("case abc"); break;} case "abc": {System. out. println ("case abc"); break;} case "bbb": {System. out. println ("case bbb"); break;} default: {System. out. println ("no suitable flag found here"); break ;}} System. out. println ("------------------------"); // while and do while System. out. print ("while test:"); int j = 0; // change to 5 while (j <5) {System. out. print (j + ""); j ++;} System. out. println (); System. out. print ("do while test:"); j = 0; // change to 5 do {System. out. print (j + ""); j ++;} while (j <5); // note the difference between while and do while. while is judged before execution, while do while executes the statement first (whether or not the condition is met for the first time) and then judges // here, change the value of j in the two fields to 5. Check the result. // pay attention to the endless loop when using these two keywords, when the while expression must be false, otherwise the program will keep repeating the System. out. println (""); System. out. println ("------------------------");/* for commonly used Loop Method * There are two () points after the for, three expressions * The first expression: Initialize something, generally, it is the variable that controls the loop * The second expression: the loop condition. If it is true, the loop will continue (so make sure there are some moments here that will be false, otherwise, an endless loop occurs. * The third expression controls the changes in the cyclic variable (to ensure that the program can end normally, rather than an endless loop) */System. out. print ("for test:"); for (int k = 0; k <6; k ++) {System. out. print (k + "");} System. out. println (""); System. out. println ("------------------------");/* break, continue, return * break: forcibly terminate the loop (jump out of the loop). Under the switch, it jumps out of the case and determines * continue: ends the loop, start to execute the next loop * return: the original intent is used in the return value of the method, but if there is a loop body in the method, you can use return to side control the loop **/System. out. print ("break, continue test:"); for (int k = 0; k <6; k ++) {if (k = 2) {continue ;} // when k is 2, end the loop (not printed) and start the next loop if (k = 4) {break;} // when k is 4, directly end the loop (although k is still within the range of the cyclic condition) System. out. print (k + "");} System. out. println (""); // result: break test: 0 1 3 System. out. println ("------------------------"); // This Is A nested loop, there are loops in the body. // modify break, continue, and return in if respectively to experience their different for (int n = 0; n <3; n ++) {for (int m = 0; m <3; m ++) {if (m = 1) {// continue; // break; // return;} System. out. print (n + "," + m + "");} System. out. println () ;}// draw a conclusion: return makes the return of the entire method much easier than simply ending a loop. If there is a loop in a method, use return with caution to control the loop to end the System. out. println ("------------------------"); System. out. println ("the program ends normally ");}}

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.