“如果"和“如果"有什么区别?带有“then"的语句在最后?

编程入门 行业动态 更新时间:2024-10-27 10:24:40
本文介绍了“如果"和“如果"有什么区别?带有“then"的语句在最后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我们在 if 语句的末尾放置一个 then 时,这两个 Ruby if 语句之间有什么区别?

What's the difference between these two Ruby if statements when we put a then at the end of the if statement?

if(val == "hi") then something.meth("hello") else something.meth("right") end

if(val == "hi") something.meth("hello") else something.meth("right") end

推荐答案

then 是一个分隔符,帮助 Ruby 识别条件和表达式的真实部分.

then is a delimiter to help Ruby identify the condition and the true-part of the expression.

if 条件 then true-part else false-part end

if condition then true-part else false-part end

then 是可选的除非您想在一行中编写一个 if 表达式.对于跨越多行的 if-else-end,换行符作为分隔符将条件与真实部分分开

then is optional unless you want to write an if expression in one line. For an if-else-end spanning multiple lines the newline acts as a delimiter to split the conditional from the true-part

# can't use newline as delimiter, need keywords puts if (val == 1) then '1' else 'Not 1' end # can use newline as delimiter puts if (val == 1) '1' else 'Not 1' end

更多推荐

“如果"和“如果"有什么区别?带有“then"的语句在最后?

本文发布于:2023-07-28 14:03:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   语句   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!