Why can't I use i++ in for loop in Swift - swift

I know the difference between i++ and ++i in Swift. As the official document said, it is better to use ++i to increment i.
But I wonder why I get a syntax error using i++ in the for loop.
The code looks like this:
for var i = 0; i < 10; i++{
println("hello")
}
However, it is OK to use either i++ or ++i in other cases. Is there any restrictions in for loop?

The error says that:
Operator is not a known binary operator
The cause is very simple: you need to add a blank between the operator and the opening curly brace:
i++ {
^
without that, the compiler takes ++{ as a binary operator, with i and print("hello") as its arguments
The problem doesn't happen with the prefixed version of the increment operator because the i variable makes a clear separation between the ++ operator and the curly brace (letters and numbers cannot be used to define operators).

Related

operator or method in swift which work as walrus operator of python

walrus operator of python language ( := )
work:- assign the value & also return that value.
language like swift at value assign it return nothing.
how to implement walrus operator kind a thing in swift language ?
I think it done by make function, pass address of variable & value.
assign value in that address & return value.
Is this work or any other way for this?
Joakim is correct.
Swift doesn't have a unary operator like C.
In C, you could do:
b = 10;
while (b>0) {
print(b--);
}
In Swift, there isn't a unary ++ or -- operator, so you would do:
var b = 10
while (b > 0) {
print b
b -= 1
}
but, really, in Swift, you'd do this instead
for b in (0...10).reversed() {
print b
}
See Reverse Range in Swift

ObservableBuffer giving IndexOutOfBounds in Scala

I am mesmerized. The below code is giving me an indexoutofbound error. But if I were to delete the slashes enabling for(j <- i until tmpArray.length) it would work. I really do not understand why it is happening, and would appreciate an explanation.
for(i <- 0 until tmpArray.length)
{
// for(j <- i until tmpArray.length)
// {
if( date.getValue != null && tmpArray(i).date != date.getValue )
{
tmpArray.remove(i)
}
// }
}
You're modifying the array as you "iterate" over it.
You are actually iterating over the range 0 until tmpArray.length, which is calculated up front. At some point, you reduce the length of the array (or, I assume so, as I can't find remove on the Array class). But it's still going to continue the iteration up to whatever the last index was when you created the range.
When you uncomment the inner for block, you're making it recompute the range for each step of the outer for. And it just so happens that the j range will simply have nothing in it if i >= tmpArray.length. So it inadvertently guards against that failure.
This is very C-style (imperative) code. It looks like all you're trying to do is remove some items from an array. That's what filter is for.
val result = tmpArray.filter { d =>
if(date.getValue != null && d != date.getValue) false else true
}
This creates a new array (result) by passing an anonymous function to tmpArray.filter. It will pass each item in the array to your "predicate", and if it returns true, it'll keep that item in result, otherwise it will omit it.
You should note that I avoided saying "loop". Scala's for isn't for making loops. It's actually syntax sugar for calling methods like foreach and map. Google "scala for comprehensions" for more detail.
If you insist on creating a C-style loop using indexes and a loop variable, you'll want to use while, so that you can check if i < tmpArray.length each time.

Proper swift 2.2 syntax with for loop [duplicate]

This question already has an answer here:
Replace c style for-loop in Swift 2.2.1
(1 answer)
Closed 6 years ago.
I wrote the following code in swift(2.2):
for var i = 2; sqrt(Double(num)) >= Double(i); i += 1 {.....}
However, I keep getting a warning message which reads "c-style for statement is deprecated and will be removed in future version of swift".
So, what is the proper way of writing the same loop is "Swift style"? Casting the value of num as double gives me error with "Swift style". Any suggestions?
Thank you.
You can refactor your deprecated C-style for loop using the for-in construct
for i in 2...Int(sqrt(Double(num))) { }
However if you really want to go essential try defining this operator to find the square root of an int rounded down to the closest int.
prefix operator √ {}
prefix func √ (number: Int) -> Int {
return Int(sqrt(Double(number)))
}
Now you can write your for loop this way
for i in 2...(√num) { }
The For loop in Swift 2.2 is re-designed for use in iterating over a sequence, such as ranges of numbers, items in an array, or characters in a string. The condition in your For loop is not easily converted into sequence or range, so it would be best to re-write it as a While loop.

How does incrementation operator work within a loop

I've just started off with perl, and while trying out a few compound statements, I wrote this:
my $ct;
while ($ct++ < 10) {
print $ct;
}
It prints out:
12345678910
I was not expecting it to print 10. How does the logic for the loop really work?
According to perdoc, a TERM operator has the highest precedence. $ct gets incremented to 10 after iterating the loop where it is 9. When it becomes 10, while loop is supposed to exit. So why is 10 still printed out?
Think of it like
while ($ct < 10) {
$ct += 1;
print $ct;
}
(increment after comparison)
On the other hand, ++ on the left side of the variable will increment first, and then do comparison,
while (++$ct < 10) {
print $ct;
}
This is quite intuitive for someone with C background; from perldoc:
"++" and "--" work as in C. That is, if placed before a variable, they increment or decrement the variable by one before returning the value, and if placed after, increment or decrement after returning the value.
Its because you are using the postfix operator. its first compared then incremented.

More elegant coffeescript loop

JS:
for(i=this.current.arr.length;i<this.counterLength;i++){
dosomthing();
dosomethingelse();
}
COFFEE:
i = #current.arr.length
while i < #counterLength
dosomthing()
dosomethingelse()
i++
I know coffeescript has great loop syntax candy, but I can't find a more elegant way of writing it than this. Is there a more coffeescripty way of doing this?
I know about:
for currentArr in current.arr
//and
for currentArr, 1 in current.arr
but i needs to start at #currentLength and not 0
The [..] operator is what you are looking for:
start = this.current.arr.length
end = this.counterLength
for [start...end]
dosomthing()
dosomethingelse()
No need to predefine start and end, I just used it to make the code a bit clearer. Note that if start is greater then end, then it will go backwards.
Actually you need [...] operator, because you used < instead of <= in the code. The [...] operator excludes the last element.