The program below functions as necessary but how do I reduce the amount of if statements. I have been told that if your function contains 2 or more if statements then your doing it wrong. Any suggestions? I've tried using switch statements but that hasn't worked because the case can't be a boolean.
for(int i = 1; i < 100; i++) { if(i % 10 == 3) { System.out.println("Fizz" + "(" + i + ") 3%10"); } if(i / 10 == 3) { System.out.println("Fizz" + "(" + i + ") 3/10"); } if(i % 10 == 5) { System.out.println("Buzz" + "(" + i + ") 5%10"); } if(i / 10 == 5) { System.out.println("Fizz" + "(" + i + ") 5/10"); } if(i / 10 == 7) { System.out.println("Fizz" + "(" + i + ") 7/10"); } if(i%10 == 7) { System.out.println("Woof" + "(" + i + ") 7%10"); } if(i % 3 == 0) { System.out.println("Fizz" + "(" + i + ") 3%==0"); } if(i % 5 == 0) { System.out.println("Buzz" + "(" + i + ")5%==0"); } if(i % 7 == 0) { System.out.println("Woof" + "(" + i + ")7%==0"); } if( (i % 7 !=0 ) && (i % 3 !=0 ) && (i % 5 !=0 ) && (i % 10 !=3) && (i % 10 !=5 ) && (i%10 !=7 ) ) System.out.println(i); }
You could maintain a mapping of strings to enums elsewhere in the program, pull out the enum associated with the returned string from the map (with a default NO_MATCH in case the string isn't in the map) and write a switch statement on the enums.
Exit an if Statement With break in Python The break is a jump statement that can break out of a loop if a specific condition is satisfied. We can use the break statement inside an if statement in a loop. The main purpose of the break statement is to move the control flow of our program outside the current loop.
Nested IFs are powerful, but they become complicated quickly as you add more levels. One way to avoid more levels is to use IF in combination with the AND and OR functions. These functions return a simple TRUE/FALSE result that works perfectly inside IF, so you can use them to extend the logic of a single IF.
How about creating a method for the cases:
public void printIfMod(int value, int mod){ if (value % 10 == mod) System.out.println(...); } public void printIfDiv(int value, int div){ if (value / 10 == div) System.out.println(...); }
Then instead of a bunch of if
you have a set of calls the the two methods. You might even create a single method that calls both of the above.
public void printIf(int value, int div){ printIfMod(value, div); printIfDiv(value, div); } for(int i = 1; i < 100; i++) { printIf(i, 3); printIf(i, 5); .... }
In the above code the number of ifs
is less of an issue to me than the amount of repeated code.
Here's a slight improvement using two switch statements
switch(i / 10){ case 3: // do something break; case 5: // do something else break; case 7: // do something else break; } switch(i % 10){ case 3: // do something break; case 5: // do something else break; case 7: // do something else break; }
Unfortunately you'll need one switch statement per divisor.
Alternatively, you can embrace OOP and come up with an abstraction like this:
public abstract class Processor { private final int divisor; private final int result; private final boolean useDiv; // if true, use /, else use % public Processor(int divisor, int result, boolean useDiv) { this.divisor = divisor; this.result = result; this.useDiv = useDiv; } public final void process(int i){ if ( (useDiv && i / divisor == result) || (!useDiv && i % divisor == result) ){ doProcess(i); } } protected abstract void doProcess(int i); }
Sample usage:
public static void main(String[] args) { List<Processor> processors = new ArrayList<>(); processors.add(new Processor(10, 3, false) { @Override protected void doProcess(int i) { System.out.println("Fizz" + "(" + i + ") 3%10"); } }); // add more processors here for(int i = 1; i < 100; i++){ for (Processor processor : processors) { processor.process(i); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With