Program control statements

Program control statements

Program control statements divided into three types:

  1. Sequence Structure
  2. Select structure
  3. Loop structure                                                   

Sequence Structure

According to the implementation of the program from top to bottom

E.g

public static void main(String[] args){
     int a=2;
     int b=3;
     int c=a+b;
     System.out.print("c is"+ c);
}

Select structure

Perform a certain part of the program that select statement to be executed next based on the determination condition.

Selection structure contains the structure is divided into:

  1. if the structure
  2. if-else if结构
  3. switch structure

if the structure

basic structure

........
if(选择条件){
    代码块1
    .....
}else{
    代码块2
    .....
}

if-else if结构

....
if(条件1){
    代码块1
}else if(条件2){
    代码块2
}else{
    代码块3
}
......

Note: You can repeat unlimited in theory, else if, but generally do not mind using too much.

switch structure

The switch statement is a multiple-choice statements

For example

  switch(today){
        case 0:
        todayStr="星期六";
        break;
        case 1:
        todayStr="星期天";
        break;
        case 2:
        todayStr="星期一";
        break;
        case 3:
        todayStr="星期二";
        break;
        case 4:
        todayStr="星期三";
        break;
        case 5:
        todayStr="星期四";
        break;   
        case 6:
        todayStr="星期五";
        break; 
    }

Loop structure

The cyclic structure is divided into two for loops and while loops, loop divided into two types, one is the known number of cycles, another condition is to know the end of the cycle.

The basic structure for loop

.....
for(循环初始化;循环条件;循环的步长或周期){
    循环体;
}
....

Nested loop

.....
for(循环初始化;循环条件;循环周期或步长){
    for(循环初始化;循环条件;循环周期或步长){
            循环体;
    }
}

Note: theoretically unlimited nested loops can be nested, but generally do not mind too much nesting, make the program too deeply.

example

import java.util.Scanner;
class D04_6{
    public static void main(String[] arge){
        for(int i=1;i<=8;i++){
            for(int k=1;k<=8-i;k++){
                System.out.print("    ");
            }
            for(int j=1-i;j<=i-1;j++){
                System.out.printf("%4.0f",Math.pow(2,i-1-Math.abs(j)));
            }
            System.out.println();
        }
    }
}

while loop

The basic structure of the while loop

1. Initialize loop
while (2. Loop continuation condition) {
    3 loop
    step 4. cycle
}

the while (to true) {
     1. Initialization loop
     2. The loop continuation condition
     3 loop
     4 loop steps
}

The second way of looking at a more convenient and simple, but a little carelessness will happen an infinite loop

example

import java.util.Scanner;

class D05_6{
    public static void main(String[]args){
        Scanner scanner =new Scanner(System.in);//提示输入一个数字
        System.out.print("请输入一个数字:");
        int num = scanner.nextInt();
        int sum = 9+sumDigits(num);//调用函数sumDigits
        System.out.print(sum);
    }
    public static int sumDigits(int num) {
        int a=0;
        while(true){//利用循环计算各个位上的数之和
            a+=num%10;
            num=num/10;
            if(num==0){
                break;
            }
        }
        return a;//返回 a
    }
}   

 

Published 13 original articles · won praise 1 · views 243

Guess you like

Origin blog.csdn.net/Yi_nian_yu_dian/article/details/104226927