0

Is it possible to initialize more than 1 variable in a single for loop?!

14th Jul 2015, 9:28 AM
Jagmeet Singh Khanuja
Jagmeet Singh Khanuja - avatar
5 Answers
+ 4
If I'm following your question correctly, then no, it's not possible to "initialize/define" ANY variable within a for-loop to be used later in the program. For example, in C++, the below code would return an error ... for (int i = 0; i < 2; i++) { int x = 5; x += 1; } std::cout << x << std::endl; It would return an error, because the x is declared within the for-loop, and only used within the for-loop. If you would've initialized it BEFORE the for-loop, then you would be able to use it within the loop as well as after the loop. Somebody correct me if I'm wrong. It's been a while since I've used C++
21st Feb 2018, 2:59 PM
Fox
Fox - avatar
+ 2
Yes you can example For (int x = 5 , y = 9; x <23; x++) { //enter code here } But remember, you can only use those variables in this loop. If you want to use those variables out side the loop, you need to initialize the before the loop Example int x; int y; for (;x<23;x++) { Enter code here }
22nd Oct 2015, 3:01 PM
BitVX
BitVX - avatar
+ 1
yes it is very much possible you can have incriments you can have values specification both are valid
20th Sep 2016, 3:22 PM
Sandeep S G
Sandeep S G - avatar
+ 1
yes it is possible to initialize multiple variables ,ofcoz you can also have multiple update , multiple conditions can also be used in for loop using logical operator such as && and ||
23rd Feb 2018, 7:20 PM
amer syed
amer syed - avatar