Equivalent for Python 'pass' statement in Julia

4,191 views
Skip to first unread message

yaois...@gmail.com

unread,
Jul 22, 2014, 4:44:28 PM7/22/14
to julia...@googlegroups.com
Hi,

Python has a 'pass' statement which allows null operations to be placed in control flows with ease.
I guess I could just use a print statement, but I was just wondering whether Julia had an equivalent?

Thanks

Leah Hanson

unread,
Jul 22, 2014, 5:04:30 PM7/22/14
to julia...@googlegroups.com
Because Julia has begin-end blocks, you can just use an empty block.

Here are some examples of what I mean:

~~~
function foo()
end
~~~

~~~
if true
elseif false
else
end
~~~

~~~
for i=1:10
end
~~~

Does that do what you wanted?

-- Leah

yaois...@gmail.com

unread,
Jul 22, 2014, 5:07:11 PM7/22/14
to julia...@googlegroups.com
Thanks Leah, that was exactly what I was looking for!

Matt Bauman

unread,
Jul 22, 2014, 5:51:24 PM7/22/14
to julia...@googlegroups.com
The ternary syntax is one place where an empty block won't suffice — there must be an expression in both branches.  In such a case, you can use `nothing` (which is also what an empty block will return).  That said, it's more typical to refactor these cases into short-circuiting expressions:

cond ? expr : nothing # could be written as:
cond
&& expr

cond
? nothing : expr # becomes:
cond
|| expr

yaois...@gmail.com

unread,
Jul 22, 2014, 6:33:42 PM7/22/14
to julia...@googlegroups.com
Matt, could you elaborate?

Sorry, not quite sure what the ? means -- does that mean 'or'? Also not sure about what you mean by the phrase ternary syntax.

Here is loosely what I am trying to do:

for i in testarray
    if testarray > 0
        do this
    else
        do nothing

It seemed to work when I tested it with nothing after else?

Stefan Karpinski

unread,
Jul 22, 2014, 7:53:42 PM7/22/14
to Julia Users
On Tue, Jul 22, 2014 at 3:33 PM, <yaois...@gmail.com> wrote:
for i in testarray
    if testarray > 0
        do this
    else
        do nothing

Isn't this just equivalent to

Matt Bauman

unread,
Jul 22, 2014, 8:28:21 PM7/22/14
to julia...@googlegroups.com


On Tuesday, July 22, 2014 6:33:42 PM UTC-4, yaois...@gmail.com wrote:
Sorry, not quite sure what the ? means -- does that mean 'or'? Also not sure about what you mean by the phrase ternary syntax.

Sure, the ternary operator — called that because of its three parts — is simply a shorthand for an if-then-else chain.  It's a common construct in many languages.  Wikipedia has lots to say about it: https://en.wikipedia.org/wiki/%3F%3A

In short, it allows you to say things like:

testarray > 0 ? do_this() : do_that()

I read it is "Is testarray > 0? Then do_this().  Otherwise, do_that()."  It's especially nice for inline assignments:

x = condition ? value_if_true : value_if_false

Reply all
Reply to author
Forward
0 new messages