I have this code:
for (int i = 0; i < var; i++)
for (int j = 0; j < var2; j++)
// something which using j variable
Can I use i variable from first loop instead var2 variable?
I have this code:
for (int i = 0; i < var; i++)
for (int j = 0; j < var2; j++)
// something which using j variable
Can I use i variable from first loop instead var2 variable?
a quick note: if your code used brackets{} then the scope of the variables lives within the brackets boundaries. Wihtout the brackets in c your loop lives until the next semi colon (in other words one line of code)
for (int i = 0; i < var; i++) {
// i can use i here but not j
for (int j = 0; j < i; i++) {
// something which using j variable
// something using i variable
}
// i can use i here but not j
}
to learn more look up Scope in C https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm#:~:text=A%20scope%20in%20any%20programming,which%20is%20called%20local%20variables.
The following code will be an infinite loop until limit reached (the value i will keep increasing, if you have replaced var2 with i )
#include <stdio.h>
int main()
{
int var=10;
for (int i = 0; i < var; i++)
for (int j = 0; j < i; i++)
{
printf("test %d", i );
}
}