+ 1
What does for(int x=o;x<5;x++){myArr[x]=42;} mean .?.please answer
3 ответов
+ 2
I guess, it's not x=o, but x=0, but anw
for loop iterates the variable x from start value 0 up to and not including end value 5 by the step ++ (1). And uses each value of that variable in the code in curly brackets {}.
So basically that code assigns value of 42 to all elements of the array myArr from position zero to and including position 4
+ 1
the "for" keyword meana that the instruction between {...} will be repeated, but how many times? you decide the number of time inside the (...) just after the for
for ( int x=0; x<5 ; x++ ) //x is an int variable created for this purpose
means that our "x" starts from 0 at the first loop
now that x is 0 let's see our instructions inside { }
myArr[x]=42 //remember x=0
it means that you assign the value 42 to myArr[0]
then you come back to the for and the program increases x (the command x++ in the "for"), then it controls that x is still <5 (the command x<5 in the "for").
if x is still <5, myArr[x]=42 is executed (the second time x will be 1 because it has been increased), so myArr[1]=42
+ 1
thank you so much guys ..it helped a lot