Reputation: 13699
Is there a way to use a for loop as a condition? Something like this?
if((for(int x = 0; x < 5; x++){if(x == 2){return true;}return false;}) == true)
Edit:
Another example
if(for(int x = 0; x < 5; x++) == 2)
I just wanted to know if it could be done. I expect that Blagovest Buyukliev and marzapower answers are accurate, based on my question. Thank you SO for you helpful responses.
Upvotes: 0
Views: 351
Reputation: 5611
It will probably depend on the language you are writing your code. Generally the for
loops do not return a value, unless you include them within an anonymous function also commonly known as lambda function.
In ruby
you could accomplish something like that this way:
res = lambda {|array| for i in array do return true if i == 2 end }.call(0..4)
but in Java
you will never be able to do such a thing easily without defining a new method.
Generally speaking procedural methods (like ruby
, perl
, python
, lisp
, etc.) will provide you with built-in methods for handling anonymous functions, while other languages like C
, C++
, Java
, etc. do not have these characteristics.
By the way, it should be clear that a for
loop is a construct in all the languages and not a function, so it should never return a value (like an integer or a boolean or whatever else) but only handle the flow of the code through the processor. Anonymous functions provide us with the ability of incapsulating simple control codes in an inline function.
Upvotes: 2
Reputation: 44804
That wouldn't make much sense, as C-ish loops are just execution control structures. There's no type that you can say loops in general have.
From your examples, what it looks to me like you are asking for is the ability to add simple inline functions without having to actually go somewhere else and write down a full function with its own name and whatnot. These are called lambdas.
If you are using C, I'd suggest just making small functions (perhaps even macros - ick) that build and return the type you want.
If you are using C++ there is some stuff in the standard libary in <algorithm>
and <functional>
you might be interested in. For your given example, I think find_if()
would do what you are looking for. Generally that stuff is more of a PITA to use than it is worth though. You have to create a full-blown predicate object to do it, which is way more code and work than just creating your one-line function would have been in the first place.
Boost adds lambda support to C++, and the next standard is supposed to add it properly to the language.
Most functional languages support lambdas, but they generally don't use C syntax like this.
Upvotes: 2
Reputation: 43548
In most languages, no.
for
is a statement, not an operator. Unlike operators, statements do not yield a result and cannot be nested into expressions. The condition of the if
statement expects an expression that can be evaluated to a boolean value, not a statement.
In languages like Perl and Python you may want to look at the map
operator.
Upvotes: 1
Reputation: 2398
No, since they are both statements. You need an expression into the if condition. Furthermore, the return statement returns the function in which it has been used.
Why would you do that anyway?
Upvotes: 1
Reputation:
This is not good style. Split it up. If you are trying for one-liners Java is the wrong language my friend.
Upvotes: 0