科学网

 找回密码
  注册

tag 标签: If语句

相关帖子

版块 作者 回复/查看 最后发表

没有相关内容

相关日志

Linux下 shell 编程之 for 循环 & if 语句
tao164411096 2018-7-7 23:20
使用bash 1.for 循环中遇到的问题 if #注意空格和“-”如果两个命令在一行则需在命令之间加;分号 then echo ================================================================================ echo This is a script to automate extract ofile,nrms and NEU echo Usage: Extracfile2018_ljt _DOYstr _DOYend -gnss expt echo Example: Extractfile2018_ljt 10 13 G cnss echo ================================================================================ fi exit 2.循环中,变量增加的命令 ((doy++))或者letdoy=doy+1或者letdoy++ 设置变量 = 左右两边不能有空格 例如 #!/bin/bash _DOYstr=$1 _DOYend=$2 for ((i=${_DOYstr}; i${_DOYend}; i++)) 等价与 for ((i=$1; i$2; i++)) do { } done 有限次的循环用for循环,希望程序持续循行用while 1. 常用的for循环结构 #语法一 for 变量 in 值1 值2 值3.. do 程序块儿 done # #语法二 for 变量 `命令` do 程序块儿 done # #语法三 for ((初始值; 循环控制; 变量变化)) do 程序块儿 done
个人分类: shell/bat|3804 次阅读|0 个评论
If语句在R语言数据判别中的作用
Bearjazz 2012-3-29 13:33
If 语句在 R 语言数据判别中的作用 熊荣川 六盘水师范学院生物信息学实验室 xiongrongchuan@126.com http://blog.sciencenet.cn/u/Bearjazz If 语句通常用作一种条件判断,在很多编程语言中都会使用到,其基本格式为 if ( statement1 ) statement2 statement1 为条件判断语句, statement2 为操作命令 举例如下 a=2 定义一个常量 a if(a0) print("Thank you") 使用 if 的判断操作语句。如果 a 大于零,则打印“ Thank you ” "Thank you" 顺利的打印 为了以后重复使用方便,我们把这个判断写成一个方程的格式 ispositive = function(x) { if(x0) print("The number is positive") } 写一个命名为 ispositive 的函数,判断我们输入的数字是否为正数 ispositive(3) 当输入 3 时 "The number is positive" 结果表示输入为正数 ispositive(-3) 如果我们输入一个负数呢? 结果什么也没有。这是因为我们的含有 if 语句判断中没有考虑负数的情况 考虑两种情况的的判断,有是有结果“非此即彼”,一次我们要同时考虑“彼”和“此”的情况。 if ( statement1 ) statement2 else statement3 ifpositive = function(x) { if(x0) print("The number is positive") else print("The number is not positive") } 当输入为正数是,打印 "The number is positive" 。如果不是正数则打印 "The number is not positive" ifpositive(2) "The number is positive" 输入正数 2 时,成功打印了 "The number is positive" ifpositive(-1) "The number is not positive" 输入为负数,成功打印了 "The number is not positive" 多层次判断。有时候,“情况”特别多,已经超出“非此即彼”的范围,这个时候就需要多层次判断。 if ( statement1 ) statement2 else if ( statement3 ) statement4 else if ( statement5 ) statement6 else statement8 ifpositive = function(x) { if(x0) print( "The number is positive" ) else if(x==0) print("The number is zero") else print("The number is negative") } 当输入为正数是,打印 "The number is positive" 。如果不是正数则打印 "The number is not positive" ifpositive(-1) "The number is negative" 输入正数 -1 时,成功打印了 "The number is negative" ifpositive(7) "The number is positive" 输入为 7 时,成功打印了 "The number is positive" ifpositive(0) "The number is zero" 输入为 0 时,成功打印了 "The number is zero"
个人分类: 我的研究|8576 次阅读|0 个评论

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-16 03:30

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部