zoukankan      html  css  js  c++  java
  • break continue exit return 的区别

    [root@localhost day1]# cat ss.sh 

    #!/bin/bash


    for ((i=0;i<5;i++))

    do
    if [ $i -eq 3 ]
    then
    break
    #continue
    #exit

    #return

    fii
    echo $i;sleep 1
    done
    echo "ok"

    ###################################################

    [root@localhost day1]# sh ss.sh           #由此看出break是直接终断循环的
    0
    1
    2
    ok

    ——————————————————————————————

    [root@localhost day1]# sh ss.sh           #continue效果 还是中断当前循环,继续进行下一循环
    0
    1
    2
    4
    ok

    ——————————————————————————————

    [root@localhost day1]# sh ss.sh           #exit 直接退出了脚本
    0
    1
    2

    return :return must be function 

    1.中断循环

    2.返回值

    [root@localhost day1]# sh return.sh
    0
    1
    2
    return.sh: line 12: return: can only `return' from a function or sourced script    #必须是函数
    3
    4
    ok

    #!/bin/bash

    fuction_return (){              #加入函数
    for ((i=0;i<5;i++))

    do
    if [ $i -eq 3 ]
    then
    #break
    #continue
    #exit
    return
    fi
    echo $i;sleep 1
    done
    echo "ok"
    }
    fuction_return

    [root@localhost day1]# sh return.sh               #结果
    0
    1
    2

  • 相关阅读:
    三角形的最大周长
    Java 虚拟机运行时数据区详解
    四数相加 II
    Java 注解详解
    四因数
    【论文笔记+复现踩坑】End-to-end Recovery of Human Shape and Pose(CVPR 2018)
    假如 Web 当初不支持动态化
    保姆级干货分享
    C# ±180的值转成0-360
    C# 校验算法小结
  • 原文地址:https://www.cnblogs.com/sxgaofeng/p/9174413.html
Copyright © 2011-2022 走看看