Is there a CASE statement or a GOTO command?

Post your questions and help other users.

Moderator: Martin

Post Reply
Econdoc
Posts: 153
Joined: 28 May 2016 20:06

Is there a CASE statement or a GOTO command?

Post by Econdoc » 22 Feb 2018 08:57

Many of my scripts result in deeply nested if then> else blocks. Is there a tool similar to a DO CASE in dBase or SELECT CASE of Visual Basic? It could really help in some situations.

Is it possible to use something like a GOTO statement to jump over some script lines?

Just curious. I have not found anything like this, but would welcome both options.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Is there a CASE statement or a GOTO command?

Post by Desmanto » 22 Feb 2018 15:33

goto, switch, case are reserved keywords, but I don't know how to use them till now. It seems not yet implemented.

DO CASE / SELECT CASE / SWITCH CASE
For DO/SELECT/SWITCH, the closest one I can suggest is Map value. I use this heavily lately, typically nested one (along with list). Example you have selection choice and map that to each choice. You can then pass the variable to the map to produce the value you want.

Code: Select all

db = newMapFromValues(
1, "apple",
7, "banana",
3, "cherry" );

select = 7;
selectedfruit = db[select]; //selectedfruit = banana
This only gives you the end value. There is no direct way to put some script, such as in the SELECT CASE, where you can use "select = banana".
But, as usual, we always have workaround for it. ;) You can put a proper script for the key and eval() that, similar to the function concept at the index. This is the example

Code: Select all

dbscript = newMapFromValues(
"apple", 'a = concat("apple", " : 7")',
"banana", "b = 'stock not available'",
"cherry", "message = " + '"This is example"');

selectedfruit = "apple";
eval(dbscript[selectedfruit]); // a = "apple : 7"
Basically you can put any length of working script inside the map and use that as a proper function. Just remember to double quote or single quote properly. You can see the example at cherry, how I purposely concatenate using +, to make sure the "This is example" is properly double quoted, and then enclosed with another single quote.


GOTO
Do you have any usage case that requires the goto? I remember facing the same problem but solve it using (nested) if(). Example you have script layout like this.

1. block A
2. checking statement if(something)
3. block B
4. return
5. label
6. block C

So if at logic 2, you wanna check for something. If it is true, goto 5. label, if false, stay on track, 3. block B. Basically that is if() something, then C else B. You can rearrange the block become.

1. block A
2. if(something)
3. block C
4. else block B

That gives the same result of jumping all over the block C when the if(something) result in false. You can nest this and do it multiple of times, thus having nested if().
But the ugly of this method is I usually keep the code indentation clean. When arranging them like this, I need to put 2 spaces for every lines in block C and block B. (I am melancholy when it comes to code indentation).

When the block keep getting longer (more than 50 lines) and they are not part of same logic, I will split them out to separate script/expression. Lately I have using expression to split this kind of logic. I can have 3-5 consecutive expression/script to split out the logic properly. It is easier to create and debug later. Going for 5 elements, only add 16 ms overhead on execution, which is still very fast. So I suggest it is better to split the longer script for better view and debug (and logic). The suggestion for making efficient script by combining them into one is mostly for single or 2 to 3 lines of script.


====================
Ternary If Operator
Thanks to you question as well, I reread at the end of the script documentation. Automagic hide a big secret at the end of the script documentation. We can have something like ternary operator, but using if() else block. Something like

Code: Select all

if(i == 0)
{
  j = 0;
}
else
{
  j = 1;
}
can be simplified to single line of script like this. Wow :o

Code: Select all

j = if(i == 0) 0 else 1;
Going to use this in my script. 8-) As I almost use the former multiline format.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply