Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce if statements [closed]

Tags:

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);     } 
like image 945
Calgar99 Avatar asked May 21 '13 12:05

Calgar99


People also ask

How do you avoid multiple if conditions?

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.

How do you break an IF condition?

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.

How do you avoid multiple nested if statements?

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.


2 Answers

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.

like image 93
John B Avatar answered Oct 12 '22 23:10

John B


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);         }     }  } 
like image 26
Sean Patrick Floyd Avatar answered Oct 13 '22 00:10

Sean Patrick Floyd