-->
Page 1 of 1

Compiler problem: switch statement

PostPosted: Mon Jan 27, 2020 4:09 am
by ayesha
Hi Guys This is Emma It has been long time that I'm working on a project or arduino. Well let me explain the issue I'm getting with my arduino switch and it's timing.

I believe the arduino that I got from an online electronic store as i think it's SWITCH statement has some compile time problems.

If I try to declare a boolean variable in a CASE statement as illustrated in "case 2: in the snippet below, the compiler throws an error.

I add this to the other CASE statement problem I flagged earlier: ie. the compiler does not throw an error if you misspell "default" as "defalut.")

CODE

switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
boolean X;
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}


This appears to be a problem with the compiler, NOT my code, and it seems to happen only with nested SWITCH statements.

Thank in advance for your prompt response.

Re: Compiler problem: switch statement

PostPosted: Wed Jan 29, 2020 5:29 pm
by AcmeUK
This appears to be a problem with the compiler, NOT my code, and it seems to happen only with nested SWITCH statements.

Are you sure?
Searching Google for 'arduino boolean statement' yields this https://www.arduino.cc/reference/en/language/variables/data-types/bool/
Which tells us:-
Reference > Language > Variables > Data types > Bool
bool
[Data Types]
Description

A bool holds one of two values, true or false. (Each bool variable occupies one byte of memory.)
Syntax

bool var = val;
Parameters

var: variable name.
val: the value to assign to that variable.
Example Code

This code shows how to use the bool datatype.

int LEDpin = 5; // LED on pin 5
int switchPin = 13; // momentary switch on 13, other side connected to ground

bool running = false;