How do I optimize the use of my if statements?

Hello, I am curious how to put the if statements listed below into a format that can be more easily called to (like a function or a class). I am working on a traffic simulation where there is a 4 way intersection that has a light in each road. Only one of the lights is green at a time and the green light cycles through each road.

Basically, when a light turns green, there is a process that involves incrementing counters. After that, there is a 25% probability that a car will appear, which is applied to each road independently. I currently have it so that this block of code is pasted for each light process but my code is very long. I wanted to make it into a concise function or class so that I can write out this code once, then just "call" it to run the probabilities.

Also, if you could include what the format/method is called and why this scenario applies to it I would greatly appreciate it. I learned about classes and functions in a class but didn't know which one this would apply to or why it should apply there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  if (road[0] >= 0) { 		//If road[0] (Top) doesn't have a car in it
      if (rand() % 100 < 25) { 		//Test the 25% chance for a car to appear
        road[0]++; 			//if true, increment the element
      }
    }

    if (road[1] >= 0) { 		//If road[1] (Left) doesn't have a car in it
      if (rand() % 100 < 25) { 		//Test the 25% chance for a car to appear
        road[1]++; 			//if true, increment the element
      }
    }

    if (road[2] >= 0) { 		//If road[2] (Bottom) doesn't have a car in it
      if (rand() % 100 < 25) {		//Test the 25% chance for a car to appear
        road[2]++; 			//if true, increment the element
      }
    }

    if (road[3] >= 0) { 		//If road[3] (Right) doesn't have a car in it
      if (rand() % 100 < 25) { 		//Test the 25% chance for a car to appear
        road[3]++; 			//if true, increment the element
      }
    }
this looks like your best bet to me:

1
2
3
4
5
6
7
8
for(i = 0; i < somemaxsizevariablethatseemstobe4; i++)
{
    if (road[i] >= 0) { 		//If road[0] (Top) doesn't have a car in it
      if (rand() % 100 < 25) { 		//Test the 25% chance for a car to appear
        road[i]++; 			//if true, increment the element
      }
    }
}
Okay I will definitely give it a try when I get a moment. Thank you for your input! It makes sense.
you are welcome! There are times you can reduce a bunch of correlated if statements into a weird but smaller and more efficient logically equivalent statement. Electronics engineers do this in hardware with something called a karnaugh (sp?) map, which is a technique I recommend learning. However, this is a 2 edged sword... you do less work in less code, but it often becomes incomprehensible to humans if the relationship between the conditions is not well documented and what you are doing clearly explained. Its actually usually better to just write explicit code that can be understood :) I came in expecting that kind of question, but yours is just repeated code, which almost always means "should have looped" or "should have made a function" depending on the format. You should also learn your boolean logic and the common laws around it. You can use that to reduce stuff as well.




Last edited on
Topic archived. No new replies allowed.