0
Is it possible to initialize more than 1 variable in a single for loop?!
5 Respuestas
+ 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++
+ 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
}
+ 1
yes it is very much possible you can have incriments you can have values specification both are valid
+ 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 ||