Loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Loops

for(int ii=0; ii<4;ii++){ for(int ii=0;ii<5;ii++){ // } } Can the same variable be used though the scope is different?

7th Jan 2022, 6:11 PM
Sanjay Kamath
Sanjay Kamath - avatar
3 Answers
+ 1
If the scope is different than so is the variable 'ii' in the outer loop is a completely different variable from the 'ii' in the inner loop This is called variable shadowing and happens both in c++ and in python, as well as in many other programming languages You can't get access to a shadowed variable
7th Jan 2022, 8:07 PM
Angelo
Angelo - avatar
+ 1
Yes. res = 0 for i in [[1],[2],[3]]: for j in i: res+=j
7th Jan 2022, 6:39 PM
Shadoff
Shadoff - avatar
+ 1
Yes you can, only in c++ the variable ii will be overridden.
7th Jan 2022, 6:52 PM
Solo
Solo - avatar