Change all value of variable

I want to change all the values of a variable to false(in this case) Where I have something like this:

Element0 : true

Element1 : true

Element2 : true

Element3 : false

Element4 : true

and there you keep going, so for this I would like to know how to set all of this to false, something like this:

example[0-50] = false;

I already know another way to do it but I don't want something so complex that is this:

for(var i = 0;i < 50; i++){
    example *= false;*
*}*
*```*
*<p>But as I say I want something more easy like the first example.</p>*
*<p>Note: I want it to work also with other kind of variables like integers.</p>*

1 Answer

1

Well, if you have seperate variables, they have nothing to do with each other so you can't set them all to something with one (short) command. If it's a collection (array or enumerable type) the easiest way is what you wrote, using a for loop.

Some examples:

var example = new int[5];  // 5 integer variables in one array

for (var i in example) {
    i = 0;
}

or the index approach like you did:

var example = new int[5];  // 5 integer variables in one array

for (var i = 0; i < example.length; i++) {
    example *= 0;*
*}*
*```*

Yep. And they aren't that "complex". Use them a bit and you'll find them easy enough to use.