if statement inside a switch case can it be done

can i ask an If inside a switch case if so how is it done

ie

case 0:   // value1---- 2 options from here
    if (somthing == somthing else)
   {
   Serial.println("done it")
   }
   else
    Serial.println("option 2 inside case 1");
    break;

  case 1:    // value 2  ----- 2 more options here
    if (somthing == somthing)
   {
   Serial.println("may have done it")
   }
   else
    Serial.println("Option 2 inside case 1");
    break

can i ask an If inside a switch case if so how is it done

Yes, and just like that.

Using ifs inside cases is perfectly valid but, of course, in your example

if (somthing == somthing)

will always be true, but I suspect that you did not mean what you wrote

Please post your whole program or a short example that exhibits the problem and a description of what happens and what you think should happen when you run it.

but I suspect that you did not mean what you wrote

I think OP meant what he/she wrote, but not what you read.

Waits with bated breath .....

UKHeliBob:
Waits with bated breath .....

For what?

OP said:

    if (somthing == somthing else)

You copied that as:

if (somthing == somthing)

While C++ doesn't allow spaces in variable names (FORTRAN does), and his/her code won't actually compile, the idea was to compare two different things.

im still working on it will copy paste code as soon as i have done it but i am getting errors so i will post with errors give me 10 mins

PaulS:
For what?

OP said:

    if (somthing == somthing else)

You copied that as:

if (somthing == somthing)

While C++ doesn't allow spaces in variable names (FORTRAN does), and his/her code won't actually compile, the idea was to compare two different things.

The if statement under case 1 is

    if (somthing == somthing)

So UKHeliBob seems to have copied correctly.

So UKHeliBob seems to have copied correctly.

Oh, OK. I didn't read that far.

   char command = page;
   switch (command) {
  
  case '1': {  // value1---- 2 options from here
    if (answer= a);
    {
   Serial.println("Option1 Screen 1");
    }
    
   else
    Serial.println("option 2 Screen 1");
    break;
  }

im getting

'else' without a previous 'if'
    if (answer= a);
    {
   Serial.println("Option1 Screen 1");
    }
    
   else

The body of the if statement is that ; on the end. Then, you have a useless set of curly braces delimiting a block of code. Then, you have an else statement that does not follow an if statement.

BINGO

   char command = page;
   switch (command) {
  
  case '1': {  // value1---- 2 options from here
    if ((p.x > 137 && p.x < 210)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option1");
} else if ((p.x > 80 && p.x < 112)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option3");
}

No errors but i'm not getting a response from the first true output i should be seeing the words "option1" showing in my serial terminal and i am not.

to confirm the if statement is true here is the readout from the serial

 X 178	 = 317 Y 
case value = 1

but nothing else below X is within 137 & 210 and y is between 280 & 320 and case 1 is true

Treat us Mike, and post your complete code.

tammytam:
Treat us Mike, and post your complete code.

its not complete

the commands are not running if a statement is true
but i don't have any errors

It's far easier to debug code that involves multiple if tests if you use multiple if statements.

if(p.x > 137)
{
   if(p.x < 210)
   {
      if(p.y > 280)
      {
         if(p.y < 320)
         {
             // Press IS inside the box
         }
      }
   }
}

Now, why might this be better? Suppose you have 4 buttons. You can have 4 ugly, complex, redundant if statements, or you can have 16 simple if statements. Right? Well, no. You don't actually need 16 if statements for 4 buttons. If buttons 1 and 2 are on the same row, and buttons 3 and 4 are on the next row, and lined up, then you only need to test that p.x is greater than the left edge of one button to test that the press is in either button 1 or button 3. Similarly, the p.x value will be less than some value for the press to be in 1 or 3. So, two tests, and you know that the press could be in one of two buttons. Two more tests, and you know that it could be buttons 2 or 4. Two more tests to confirm the possibility of being 1 or 3 or 2 or 4. Hmmm, words are not helping all that much. But, for more than 2 buttons, nested if statements are far easier to deal with. And, of course, it is easy to add else statements and Serial.print() statements, to confirm that the left, right, top, and bottom tests are, or are not, successful, to see if the test results match your expectations.

My buttons are as image below

there are 4 screens each with the buttons in the same place.
only 2 screens have all buttons active. but they are all showing

all the button functions and commands are ready to use i just can't get them to activate now even though the statement is showing as true

Post all of your code, otherwise we're just guessing.

all the button functions and commands are ready to use i just can't get them to activate now even though the statement is showing as true

The snippets of code you have posted don't do any more than print that a particular button has been pressed.

If your real code does more than that, and you need help with it, you need to post it.

ok here you are i know there is one button missing from the code but i can add it in afterwards

int page = 0;     // page number active
//---------------------- Main Loop ----------------------------
void loop() {
  ctp.begin();
  Serial1.begin(115200);
  Serial.begin(115200);
  
  // Wait for a touch 
  if (! ctp.touched()) {
    return;
  }

  // Retrieve a point  
  TS_Point p = ctp.getPoint();
  
 // flip it around to match the screen.
  p.x = map(p.x, 240, 0, 240, 0);
  p.y = map(p.y, 320, 0, 0, 320);
  
  // Print out raw data from screen touch controller
  
  Serial.print(" X "); Serial.print(p.x);
  Serial.print("\t = "); Serial.print(p.y);
  Serial.println(" Y ");
  Serial.print(page);
  delay(500);
  
 
   char command = page;
   switch (command) {
  
     // --------------check to see if screen 1 is loaded-----------------
     
  case '1': {  // Screen 1 Active As when screen 1 loads it chages the value of page to 1
     if ((p.x > 137 && p.x < 210)&& (p.y > 280 && p.y < 320)) {
   mainmenu();}
   else if ((p.x > 80 && p.x < 112)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option1.2");}
   
      else if ((p.x > 45 && p.x < 85)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option1.3");}
   
      else if ((p.x > 5 && p.x < 24)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option1.4");}}
   
   // -----------------check to see if screen 2 is loaded------------------
   
     case '2': {  // Screen 2 Active As when screen 2 loads it chages the value of page to 2
    if ((p.x > 137 && p.x < 210)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option2.1");}
   
   else if ((p.x > 80 && p.x < 112)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option2.2");}
   
      else if ((p.x > 45 && p.x < 85)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option2.3");}
   
      else if ((p.x > 5 && p.x < 24)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option2.4");}}
   
   // -----------------check to see if screen 3 is loaded------------------
   
     case '3': {  // Screen 3 Active As when screen 3 loads it chages the value of page to 3
    if ((p.x > 137 && p.x < 210)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option3.1");}
   
   else if ((p.x > 80 && p.x < 112)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option3.2");}
   
      else if ((p.x > 45 && p.x < 85)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option3.3");}
   
      else if ((p.x > 5 && p.x < 24)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option3.4");}}
   
   // -----------------check to see if screen 4 is loaded------------------
   
     case '4': {  // Screen 4 Active As when screen 4 loads it chages the value of page to 4
    if ((p.x > 137 && p.x < 210)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option4.1");}
   
   else if ((p.x > 80 && p.x < 112)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option4.2");}
   
      else if ((p.x > 45 && p.x < 85)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option4.3");}
   
      else if ((p.x > 5 && p.x < 24)&& (p.y > 280 && p.y < 320)) {
   Serial.print("option4.4");}}

}


}
void mainmenu(){
  page = 1;
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
  }
  Serial.println("OK!");
  tft.fillScreen(BLACK);

  Title();
  Grid();
  Buttons();
{

PaulS:
The snippets of code you have posted don't do any more than print that a particular button has been pressed.

If your real code does more than that, and you need help with it, you need to post it.

code posted i know it just posts to serial screen i just didn't want the screen jumping all over the place until i can see output in the serial monitor