zoukankan      html  css  js  c++  java
  • Java程序流程控制:判断结构、选择结构、循环结构


    本文内容:

    • 判断结构
      • if
    • 选择结构
      • switch
    • 循环结构
      • while
      • do-while
      • for
      • for each
    • break、continue
    • return

    首发时间:2017-06-22 21:34

    修改时间:

    1. 2018-03-16 17:01

    判断结构:

    • java中使用if作为判断结构
    • if语句有三种格式:
      • image
      • image
      • image
    package study.program_struct;
    
    import java.util.Scanner;
    
    public class if_useage {
        public static void main(String args[]){
            int i;
            Scanner reader=new Scanner(System.in);
            i=reader.nextInt();
            if(i>=90){
                System.out.println("i>=90");
            }else if (i>60){
                System.out.println("60<i<90");
            }else{
                System.out.println("i<=60");
            }
        }
    }

    选择结构:

    • java使用switch语句来构成选择结构
    • switch语句的格式:
      • image
    • switch语句选择的类型只有四种:byte,short,int,char【即上面的i只能为这几种,1.7进行了扩展,可以采用一些特殊类型如枚举类型,String
    • 匹配到结果后,需要使用break来退出,不然会向下顺序执行完所有选择
    package study.program_struct;
    
    import java.util.Scanner;
    
    public class switch_useage {
        public  static void main(String args[]){
            int i;
            Scanner reader=new Scanner(System.in);
            i=reader.nextInt();
            switch (i){
                case 1:System.out.println("1");break;
                case 2:System.out.println("2");break;
                case 3:System.out.println("3");break;
                case 4:System.out.println("4");break;
                default:System.out.println("default");
            }
        }
    }

    循环结构:

    • java中有三种循环结构:while,do-while,for
    • while:

        • while语句的格式:image
    package study.program_struct;
    
    public class While_usage {
        public static void main(String args[]){
            int i=5;
            while(i>0){
                System.out.println(i);
                i=i-1;
            }
        }
    }
    • do-while:

        • do-while语句的格式:image
        • do-while特定:无论条件是否满足,循环体至少执行一次。
    package study.program_struct;
    
    public class While_usage {
        public static void main(String args[]){
    
            do {
                System.out.println("hello");
            }while (false);
        }
    }
    • for:

        • for语句格式:image
    package study.program_struct;
    
    public class For_usage {
        public static void main(String args[]){
            for (int i=0;i<5;i++){
                System.out.println(i);
            }
        }
    }

    补充:

    • for-each:

      • for each结构是jdk5.0新增加的一个循环结构)image
      • 定义一个变量用于暂存集合中的每一个元素,并执行相应的语句。
      • 集合表达式(int 副本:原本)必须是一个数组或者是一个实现了lterable接口的类(例如ArrayList)对象。
      • 缺点: 无法对指定下标操作或难以对指定下标操作。

    image_594bc627_1d6e


    breakcontinue

    • break可以用来跳出选择结构和循环结构
    • continu可以用来打断循环结构中的当次循环,直接进行下一次循环。

    image

    package study.program_struct;
    
    public class For_usage {
        public static void main(String args[]){
            for (int i=0;i<5;i++){
                if(i%2==0)continue;
                System.out.println(i);// 1,3
            }
        }
    }

     


    使用return来结束方法:

    java中也可以使用return来中断循环。

  • 相关阅读:
    JavaScript
    LeetCode(17)Letter Combinations of a Phone Number
    LeetCode(96)Unique Binary Search Trees
    LeetCode(16)3Sum Closest
    Python更换pip源,更换conda源
    LeetCode(15)3Sum
    LeetCode(94)Binary Tree Inorder Traversal
    LeetCode(14)Longest Common Prefix
    LeetCode(29)Divide Two Integers
    LeetCode(12)Integer to Roman
  • 原文地址:https://www.cnblogs.com/progor/p/7067322.html
Copyright © 2011-2022 走看看