mamdouh abdelfatah
mamdouh abdelfatah

Reputation: 53

How to make multiple if statements

I'm using processing with java mode and I want to make more than 32 if conditions when I make it like normal way like this

If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else {//dosomething}

But only the first condition works and the rest of conditions don't work Sometimes it gives me error " Eof "

I tried also this style

If (-------- == --------){//dosomething}

Else {//dosomething} 

If (-------- == --------){//dosomething}
Else {//dosomething} 


If (-------- == --------){//dosomething}
Else {//dosomething} 

It also didn't works only the first if condition works

Note there is one time the gui didn't show the buttons or labels

How could I answer this how could I write many if conditions

this is example the value of y1 and y comes from other device and changes one and zero when I press a hardware button this code is not complete but only the concept and also there is from y1 to y20 and from x1 to x20 only the first condition that works and I try all the rectangles with it

this is update for the question the full code

**in this code I use 17 pushbutton in order to read from them if it is pressed the rectangle color will be black and if it is not pressed the rectangle will be white , It works well but only for 9 inputs but the rest of the 17 didn't work ,please note that I checked all the inputs and all connections and they are working well so how could I solve this **

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int x1 = 255;
int x2 = 255;
int x3 = 255;
int x4 = 255;
int x5 = 255;
int x6 = 255;
int x7 = 255;
int x8 = 255;
int x9 = 255;
int x10 = 255;
int x11= 255;
int x12 = 255;

void setup()
{
  size(780, 600);

  //println(Arduino.list());

  arduino = new Arduino(this, Arduino.list()[0], 57600);

    arduino.pinMode(13, Arduino.INPUT);
    arduino.pinMode(12, Arduino.INPUT);
    arduino.pinMode(11, Arduino.INPUT);
    arduino.pinMode(10, Arduino.INPUT);
    arduino.pinMode(9, Arduino.INPUT);
    arduino.pinMode(8, Arduino.INPUT);
    arduino.pinMode(7, Arduino.INPUT);
    arduino.pinMode(6, Arduino.INPUT);
    arduino.pinMode(5, Arduino.INPUT);
    arduino.pinMode(4, Arduino.INPUT);
    arduino.pinMode(3, Arduino.INPUT);
    arduino.pinMode(2, Arduino.INPUT);

}


void draw()

{ 

  roundRect(10, 10, 150, 90, x1);
  roundRect(180, 10, 150, 90, x2);
  roundRect(350, 10, 150, 90, x3);
  roundRect(520, 10, 150, 90, x4);

  roundRect(10, 120, 150, 90, x5);
  roundRect(180, 120, 150, 90, x6);
  roundRect(350, 120, 150, 90, x7);
  roundRect(520, 120, 150, 90, x8);

  roundRect(10, 230, 150, 90, x9);
  roundRect(180, 230, 150, 90, x10);
  roundRect(350, 230, 150, 90, x11);
  roundRect(520, 230, 150, 90, x12);


  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(13) == Arduino.HIGH ) {
    x1=0;
  }                                                                                                       
  else if (arduino.digitalRead(13) == Arduino.LOW) {
    x1=255;
  }
  /////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(12) == Arduino.HIGH ) {
    x2=0;
  }
  else if (arduino.digitalRead(12) == Arduino.LOW) {
    x2=255;
  }
  /////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(11) == Arduino.HIGH ) {
    x3=0;
  }
  else if (arduino.digitalRead(11) == Arduino.LOW) {
    x3=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(10) == Arduino.HIGH ) {
    x4=0;
  }                                                                                                         
  else if (arduino.digitalRead(10) == Arduino.LOW) {
    x4=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(9) == Arduino.HIGH ) {
    x5=0;
  }
  else if (arduino.digitalRead(9) == Arduino.LOW) {
    x5=255;
  }
  //////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(8) == Arduino.HIGH ) {
    x6=0;
  }
  else if (arduino.digitalRead(8) == Arduino.LOW) {
    x6=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(7) == Arduino.HIGH ) {
    x7=0;
  }
  else if (arduino.digitalRead(7) == Arduino.LOW) {
    x7=255;
  }
  ////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(6) == Arduino.HIGH ) {
    x8=0;
  }
  else if (arduino.digitalRead(6) == Arduino.LOW) {
    x8=255;
  }
  /////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(5) == Arduino.HIGH ) {
    x9=0;
  }
  else if (arduino.digitalRead(5) == Arduino.LOW) {
    x9=255;
  }

  ////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(4) == Arduino.HIGH ) {
    x10=0;
  }
  else if (arduino.digitalRead(4) == Arduino.LOW) {
    x10=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(3) == Arduino.HIGH ) {
    x11=0;
  }
  else if (arduino.digitalRead(3) == Arduino.LOW) {
    x11=255;
  }
  //////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(2) == Arduino.HIGH ) {
    x12=0;
  }
  else if (arduino.digitalRead(2) == Arduino.HIGH) {
    x12=255;
  }
  //////////////////////////////////////////////////////////////
}

void roundRect(float x, float y, float w, float h, float f) {

    float corner = w/10.0;
    float midDisp = w/20.0;

    fill(f);

    beginShape();  

    curveVertex(x+corner, y);
    curveVertex(x+w-corner, y);
    curveVertex(x+w+midDisp, y+h/2.0);

    curveVertex(x+w-corner, y+h);
    curveVertex(x+corner, y+h);
    curveVertex(x-midDisp, y+h/2.0);

    curveVertex(x+corner, y);
    curveVertex(x+w-corner, y);
    curveVertex(x+w+midDisp, y+h/2.0);

    endShape();

  } 

enter image description here

Upvotes: 2

Views: 6115

Answers (3)

Kevin Workman
Kevin Workman

Reputation: 42176

The other questions are telling you to avoid putting a semicolon after an if statement (which you've said you didn't do) or to change your design (which doesn't really answer your question). These answers aren't wrong, but they also don't really address your fundamental question.

It sounds like you're just confused about the basic syntax of an if else-if else chain.

Here is the basic syntax:

if(x > 75){
   println("A");
}
else if(x > 50){
   println("B");
}
else{
   println("C");
}

Note that Java (and therefore Processing) is case-sensitive, so if, else if and else are all lower-case.

It's also important to make sure you count your opening and closing curly brackets. If they don't match up exactly, then you'll get the error you're talking about. Here's an example of bad code:

if(x > 75){
   println("A");
else if(x > 50){
   println("B");
}

You can also place unrelated if statements one after the other. They syntax for that just looks like this:

if(x > 75){
   println("A");
}
else if(x > 50){
   println("B");
}
else{
   println("C");
}

if(y < 1){
   println("D");
}
else if(y < 2){
   println("E");
}
else{
   println("F");
}

You can get more complicated and do things like put an if statement inside another if statement, but it doesn't sound like that's what you're trying to do here.

Upvotes: 1

GhostCat
GhostCat

Reputation: 140417

Short answer: you don't do that.

Long answers: such lengthy chains of if/else are the opposite of good OO design! You don't ask objects about some state; to then make changes on some other thingy.

Instead, you should be using polymorphism.

Example:

abstract class Whatever {
  void doSomething();
// ... probably other methods as well

with and various concrete subclasses, like

class SpecificWhatever extends Whatever {
  @Override void doSomething() { // ...

And finally, you have

class WhateverFactory {
  Whatever createFrom(...) {
  // here you might actually need a switch or if/else 

The essential point here is: you try hard to avoid the if/else/switch coding style. It adds a lot of complexity to your code and it makes it harder to maintain your code base (because it causes so much coupling within your classes).

And, just to give a more direct hint on your question; one pattern that I like is:

if (whatever) {
  doSomething();
  return;
}

if (somethinElse) {
  doSomethingElse();
  return;
}

That comes in really handy if your methods are supposed to return a value; and you can reduce the two lines to a single:

return doSomething();

Yes, that is an violation of the good old "single entry single exit" paradigm; but I think it makes if/elses chains (those that you can't avoid) much easier to read.

Given your input, I am wondering if you couldn't use one or more Map<Integer, Integer> objects. So, you ask the map what the X value for some Y input should be (if those numbers are really constant literals).

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533442

When you write

if (-------- == --------){//dosomething};

This is the whole statement. The ; means you can't place an else after it.

Instead you should write

if (-------- == --------) {
    //dosomething
} else if (-------- == --------) {
    //dosomething else
}

If the first condition is the only one which is called, it means it is always true.

BTW If you have long chains of these, it is likely there is a better way to do this such as using a switch or polymorphism depending on what you are trying to do.

Upvotes: 10

Related Questions