+ 2

I didn't understand the last line of the hint .

can any one help me to make clear for the meaning of the last line of the hint given in swift programming for loop... please help me out

19th Apr 2017, 2:48 PM
Harikrishnan Moovendran
Harikrishnan Moovendran - avatar
4 Respuestas
+ 7
Now I understand what you're talking about. Given this code: for var index = 0; index < 3; ++index { print("index is \(index)") } What the hint is trying to explain is that the value of variable "index" can only be accessed inside the for loop because it's a local variable. When the loop ends the variable "index" is destructed and loses the value. If you want to access the value of the variable when loop ends, you should declare it outside the loop. Like this: var index; for index = 0; index < 3; ++index { print("index is \(index)") } When loop ends you can retrieve the value because the variable was created outside the loop and it's not destructed.
20th Apr 2017, 2:20 AM
Ismail
Ismail - avatar
+ 5
Post the link to the page where the hint appears.
19th Apr 2017, 10:11 PM
Ismail
Ismail - avatar
0
Constants and variables in the initialization expression (such as var index = 0) are only valid within the for loop itself. To retrieve the final value of index after the loop ends, you must declare index before the loop begins.
20th Apr 2017, 2:01 AM
Harikrishnan Moovendran
Harikrishnan Moovendran - avatar
0
the post is the hint that could not understand.please help me to understand it...
20th Apr 2017, 2:02 AM
Harikrishnan Moovendran
Harikrishnan Moovendran - avatar