Home > database >  If-else statement trouble
If-else statement trouble

Time:04-11

My project uses an if-else statement to determine if a random image from a list of images matches to an image in another list. I'm trying to find a piece of code that will allow me to set the if-else statement so when it asks: if randomImage == list[?]. In the question mark I need code that will go through the entire list and see if the randomImage matches from ANY of the elements in the list. Here's a snippet of code: trash[randomTrash] generate a random image from the list trash. I need it so it checks if the random image of trash is equal to an image in another list. It needs to go through recycle list and determine if an element is equal to it.

CodePudding user response:

There is probably an easier way to do this depending on your project specifics, but you should be able to use a for loop that loops through each element in your list.

for (int element = 0; element < list.length; element  ) {
   if (randomImage == list[element]) {
      // randomImage matches with an element in the list. Assuming you are using some boolean variable 'match' which is initialized to be false.
      match = true;
   }
}

CodePudding user response:

it's really helpful if you tag the language you're using, so people know how exactly to address the issue in particular.

The most common and straightforward approach you would see is looping through the whole list by index, and comparing each image to the one from it, and that's a solid working one.

If you're on a language supporting list comprehension you could take an approach similar to this,

 [x for x if x==image...]

then check if the list is empty for your if/else condition.

Please let us know in particular if it's something more specific you're looking for.

  • Related