3. Java three program structures: sequence, branch, loop

Branch statement is the most frequently used structure in writing code, and loop is an essential artifact for program repetition code. Today, let’s summarize the knowledge points of the two.
First of all, there are three structures in Java programs, sequence, branch, and loop .

1. Sequence structure: execute from top to bottom, from left to right

2. Branch structure

if structure:
       if(){

           }
if···else···
       if(){

           }else{

           }
if···else if···
       if (){

           }else if(){

           }
switch···case:
       switch(){        case value 1:                break;        case value 2:                break;                ···········        default:                break;        } Note: switch brackets can only be, enumeration, Why can't the long data type be used in the brackets of byte, char, short, int and string ? Answer: Because the essence of switch is still the == logical operation of the if statement, as mentioned in the previous article, the data type calculated by Java is int, so float, double, and long that are larger than int data type cannot be automatically converted, so they cannot be used. in the switch.










3. Cycle structure

The types of cycles are roughly divided into two types: cycles with known cycle times and cycles with unknown cycle times

loop with unknown loop count

       while (loop condition) {               loop body               }        when the loop condition is true, execute the loop body, and then judge the loop condition until the loop condition is false to end the loop do        {               loop body        }while (loop condition)





Known number of cycles cycle

       for(1;2;3){                    4;               } Execution sequence: 1, 2, 4, 3, 2, 4, 3, 2·······2        1: loop variable initialization        2: loop condition        3: loop variable Change amount        4: loop body for loop flow control keywords: break; jump out of the entire loop continue; skip this loop and start the next loop Both follow the principle of proximity, affecting        the        closest loop loop nesting The number of inner loops is called time complexity. The outer layer loops once, and the inner layer loops once        for(){               for(){               }        }







       
       







Guess you like

Origin blog.csdn.net/lzq2357639195/article/details/119720907