精华内容
下载资源
问答
  • Bash While Loop Example

    2012-10-15 14:15:54
    How do I use bash while loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using while statement? The bash while loop is a control flow statement that al

    How do I use bash while loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using while statement?

    The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition. For example, run echo command 5 times or read text file line by line or evaluate the options passed on the command line for a script.

    bash while loop syntax

    The syntax is as follows:

    while [ condition ]
    do
       command1
       command2
       command3
    done

    command1 to command3 will be executed repeatedly till condition is true. The argument for a while loop can be any boolean expression. Infinite loops occur when the conditional never evaluates to false. For example following while loop will print welcome 5 times on screen:

    #!/bin/bash
    x=1
    while [ $x -le 5 ]
    do
      echo "Welcome $x times"
      x=$(( $x + 1 ))
    done

    Here is a sample shell code to calculate factorial using while loop:

    #!/bin/bash
    counter=$1
    factorial=1
    while [ $counter -gt 0 ]
    do
       factorial=$(( $factorial * $counter ))
       counter=$(( $counter - 1 ))
    done
    echo $factorial

    To run just type:
    $ chmod +x script.sh
    $ ./script.sh 5

    Output:

    120

    While loops are frequently used for reading data line by line from file:

    #!/bin/bash
    FILE=$1
    # read $FILE using the file descriptors
    exec 3<&0
    exec 0<$FILE
    while read line
    do
    	# use $line variable to process line
    	echo $line
    done
    exec 0<&3

    You can easily evaluate the options passed on the command line for a script using while loop:

    ......
    ..
    while getopts ae:f:hd:s:qx: option
    do
            case "${option}"
            in
                    a) ALARM="TRUE";;
                    e) ADMIN=${OPTARG};;
                    d) DOMAIN=${OPTARG};;
                    f) SERVERFILE=$OPTARG;;
                    s) WHOIS_SERVER=$OPTARG;;
                    q) QUIET="TRUE";;
                    x) WARNDAYS=$OPTARG;;
                    \?) usage
                        exit 1;;
            esac
    done
    .......
    ..

    How do I use while as infinite loops?

    Infinite for while can be created with empty expressions, such as:

    #!/bin/bash
    while :
    do
    	echo "infinite loops [ hit CTRL+C to stop]"
    done

    Conditional while loop exit with break statement

    You can do early exit with the break statement inside the whil loop. You can exit from within a WHILE using break. General break statement inside the while loop is as follows:

    while [ condition ]
    do
       statements1      #Executed as long as condition is true and/or, up to a disaster-condition if any.
       statements2
      if (disaster-condition)
      then
    	break       	   #Abandon the while lopp.
      fi
      statements3          #While good and, no disaster-condition.
    done

    In this example, the break statement will skip the while loop when user enters -1, otherwise it will keep adding two numbers:

    #!/bin/bash
     
    while :
    do
    	read -p "Enter two numnbers ( - 1 to quit ) : " a b
    	if [ $a -eq -1 ]
    	then
    		break
    	fi
    	ans=$(( a + b ))
    	echo $ans
    done

    Early continuation with the continue statement

    To resume the next iteration of the enclosing WHILE loop use the continue statement as follows:

    while [ condition ]
    do
      statements1      #Executed as long as condition is true and/or, up to a disaster-condition if any.
      statements2
      if (condition)
      then
    	continue   #Go to next iteration of I in the loop and skip statements3
      fi
      statements3
    done

    Recommended readings:

    • See all sample while loop shell script in our bash shell directory.
    • Bash loops from our Linux shell scripting tutorial guide
    • man bash
    • help while
    • help {
    • help break
    • help continue
    展开全文
  • [初学笔记] while loop

    2017-09-01 16:07:52
    while loop is a conditional loop 即系,满足条件时,会一直循环重复执行,,,直到 不满足 条件时才能结束循环 在MATLAB 中 while循环的语法是: while end while 循环反复执行程序语句只要表达式为 ...


    while loop is a conditional loop

    即系,满足条件时,会一直循环重复执行,,,直到 不满足 条件时才能结束循环


    在MATLAB 中 while循环的语法是:

    while <expression>
       <statements>
    end

    while 循环反复执行程序语句只要表达式为 true。

    表达式是 true,当结果不为空,并包含所有非零元素(逻辑或实际数字)。否则,表达式为 false。


    a =10;

    % while loop execution

    while( a < 20 )

    fprintf('value of a: %d', a);

    a = a + 1;

    end


    value of a: 10
    value of a: 11
    value of a: 12
    value of a: 13
    value of a: 14
    value of a: 15
    value of a: 16
    value of a: 17
    value of a: 18
    value of a: 19


    >>   
    alike = input ('how much do you like the dreamy?');
    while ((alike < 1) || ( alike > 10))
       fprintf('Error! Invalid input! Please enter number from 1 to 10.\r\n');
       alike = input ('how much do you like the dreamy?');
    end

    how much do you like the dreamy?

    11

    Error! Invalid input! Please enter number from 1 to 10.

    how much do you like the dreamy?

    1






    展开全文
  • python之循环for&while loop

    千次阅读 2020-07-05 13:33:48
    while loop for 循环 for循环一般用于集合类型的遍历循环,可以结合着循环使用,自身不需要使用到闭包 closure #''' #循环可以结合break使用,break关键字用来执行跳出循环操作. #for循环也可以配合else使用,只有...

    python之循环for & while loop

    1 for 循环

    for循环一般用于集合类型的遍历循环,可以结合着循环使用,自身不需要使用到闭包 closure

    #'''
    #循环可以结合break使用,break关键字用来执行跳出循环操作.
    #for循环也可以配合else使用,只有for循环的方法体全部执行玩之后才会执行else
    #'''
    for i in range(1,5) #[1,5),可以使用
    	print(i)
      if xxx:
        print('')
        # break
    else: 
      pass # or print() or TODO else
    

    2 while 循环

    while循环与java中的循环类似,也需要借助外部变量,形成闭包

    count = 1 
    while count<=5 :
      # loop body
    

    在python中,循环也是可以嵌套使用的,控制循环的方式与其他语言是差不多的。

    2.1 嵌套循环打印三角形

    ceng = 1 #表示层数 
    while ceng <=5: # 第一层循环,保证打印多少行
      count = 1
      while count <= ceng:
      	print('*' ,end='')
        count+=1
      print() # 换行
    

    2.2 打印九九乘法表

    #'''
    # 1*1=1	
    # 1*2=2	2*2=4	
    # 1*3=3	2*3=6	3*3=9	
    # 1*4=4	2*4=8	3*4=12	4*4=16	
    # 1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
    # 1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
    # 1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
    # 1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
    # 1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=8	
    # '''
    i = 1
    while i <= 9:
        j = 1
        while j <= i:
            print('{}*{}={}'.format(j, i, i * j), end='\t')
            j += 1
        print()
        i += 1
    
    展开全文
  • 现象 ARM MDK V6的编译器在对类似如下语句 while(Spi_GetStatus(M0P_SPI0, SpiTxe) == ...…/drv/SI24R1.c(52): warning: while loop has empty body [-Wempty-body] …/drv/SI24R1.c(52): note: put the semicolon on

    现象

    ARM MDK V6的编译器在对类似如下语句

    	while(Spi_GetStatus(M0P_SPI0, SpiTxe) == FALSE);  //等待发送缓冲器空
        Spi_SendData(M0P_SPI0, tx_dat);
    

    会有warning提示:

    …/drv/SI24R1.c(52): warning: while loop has empty body [-Wempty-body]

    …/drv/SI24R1.c(52): note: put the semicolon on a separate line to silence this warning

    按照提示使用分号单独分隔一行可以消除该warning

    解决方法

    可改为如下

    	while(Spi_GetStatus(M0P_SPI0, SpiTxe) == FALSE)
            ;  //等待发送缓冲器空
        Spi_SendData(M0P_SPI0, tx_dat);
    

    	while(Spi_GetStatus(M0P_SPI0, SpiTxe) == FALSE){
            ;  //等待发送缓冲器空
        }
        Spi_SendData(M0P_SPI0, tx_dat);
    
    展开全文
  • Shell Script While Loop Examples

    千次阅读 2011-05-04 13:31:00
    Shell Script While Loop Examples by Vivek Gite on July 16, 2009 · 7 comments Can you provide me a while loop control flow statement shell script syntax and example that allows code to be executed...
  • Oracle/PLSQL: While Loop

    2007-10-31 17:06:00
    Oracle/PLSQL: While Loop The syntax for the WHILE Loop is: WHILE conditionLOOP {.statements.}END LOOP...You would use a WHILE Loop when you are not sure how many times you will execute the loop ...
  • tf.cond and tf.whileloop 本篇博客梳理一下 tensorflow python client API 与tf.cond和 tf.whileloop相关的部分,对于这两个api 的详细解释,请看我的第一篇博客和第二篇博客,这篇博客讲一些api 的实现细节。 ...
  • MySQL 循环方法 while loop repeat 详解

    千次阅读 2018-03-25 13:44:34
    存储过程中使用到的循环while 循环 :# 语法while 条件 do 循环体;end while;# 创建自定函数使用 while 循环mysql&gt; DELIMITER ;; -&gt; CREATE PROCEDURE sum1(a INT) -&gt; BEGIN -&gt; ...
  • while循环语句: 1:语法 -- WHILE 循环语法:WHILE <布尔表达式> (当表达式成立时执行loopLOOP要执行的语句;END LOOP;其中:循环语句执行的顺序是先判断<布尔表达式>的真假,如果为 真则循环...
  • oracle存储过程中的while loop循环示例

    千次阅读 2017-04-20 16:22:57
    declare  load_date date; --声明一个循环变量 begin ... load_date := to_date('201001', 'yyyymm');... while load_date  to_date(to_char(add_months(sysdate, -1), 'yyyymm'), 'yyyy
  • In BASH, a variable is changed in a loop, but is kept untouched when quit loop; i.e, the new value set in loop is lost. function showme { echo -n "Loop=$linecount" sh -c 'echo " PID=$PPID"' }
  • -- MySQL中的三中循环 whileloop 、repeat 求 1-n 的和 -- 第一种 while 循环 -- 求 1-n 的和 /* while循环语法: while 条件 DO 循环体; end while; */ -- 实例: create procedure sum1(a int) begin...
  • linux shell ----while Loop

    2013-03-17 14:37:08
    Syntax: while [ condition ] do command1 command2 command3 .. .... Loop is exec
  • while loop 可以根据条件不必执行一次。 语法: while 条件表达式 loop 执行语句 end loop 2.快速入门 create or replace procedure pro1(v_in_ename varchar2,v_in_n number) is v_empno number:=200; be
  • 【Excel VBA】Do...until / Do...while loop

    千次阅读 2019-03-26 08:56:19
    Do Until/while适用于不知道要loop多少次的情况 1. Do until Sub Simple_Do_Until_V1() StartCell = 8 Do Until Range("A" & StartCell).Value = "" Range("B" & StartCell).Value = Range("A" & StartCell)....
  • javascript 循环速度 for loop || while loop

    千次阅读 2007-12-11 11:51:00
    javascript做为客户端的脚本语言,一般来说都是速度是第一.要求速度就要看怎样才会让速度最快. 循环基本上所有语言中都有的,也是最常用的,它的体现,基本能代表...for in while循环分为2种(do-while语句不考虑):while
  • 实例1: 思路:在屏幕上循环地画多个圆弧,每个圆弧的坐标和形状参数逐渐改变; 核心代码片段:while(i){ ellipse(100+0, 20+i*2, 100, 20); ellipse(270,
  • 2 while loop and for loop  2.1 demo def list_extend_many1(lists): """Returns a list that is the concatenation of all the lists in the given list-of-lists.""" result = [] for l in lists: ...
  • 第一次测试 c=now while k k=k+1 wend d=now ...response.write "while "& ((d-c)*24*60*60)& "秒" ...do while n ...loop f=now response.write "do while "&((f-e)*24*60*60)& "秒" %> a=now
  • 直接上代码: DELIMITER $$ ... end while loop_while; --结束while循环 end loop cursor_loop; --结束loop循环 close cursor_test; --关闭游标 end $$ --存储过程结束 DELIMITER;
  • 这个下午为了改这段代码改了一下午。纠结的点是: ... % break the while loop  end    gender = gender; % save the right answer  end % end, when it's the correct answer
  • while (true){ //while 是抄来的所以不懂得 使用的原因 //关系到socket_close要不要运行,什么时候运行(需要更进一步研究) print_r("暂停\n"); $socket_accept = socket_accept($socket_create); print_r("继续...
  • while_loop.py

    2020-12-06 04:46:04
    python 2.7 while loop 示例
  • nodejs while-loop

    2017-06-20 16:18:00
    node-while-loop  A while loop alternative for Nodejs based on promises. Install $ npm install --save node-while-loop   Usage var loop = require('node-while-loop'); var i = 1; loop....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 258,040
精华内容 103,216
关键字:

whileloop