>he wraps his switch cases in blocksswitch(i) {
case 1: {
// do stuff
break;
}
case 2: {
// do stuff
break;
}
}
Shut up you greasy turd.
Unnecessary clutter.
>>60443620
That's some old crufty language's way to do it. Probably also only works on int or or char or some stupid thing like that.
Modern languages can do nicer things without extra blocks:val thing = "face"
val foo = thing match {
case "bar" | "baz" => "resulting string"
case "joji" => "another result"
case r"(\d+)$d" => d.toInt
case _ :: tail => "tail of default string: " + tail
}
>>60443620
>Not the webm
Fuck off faggot, no lazy fags welcomed here
>>60443620
>he writes two breaks just to be sure
>>60443753
>>60443750switch(i) {
case 1:
// do stuff
break;
case 2:
// do stuff
break;
}
>>60443804
Yea, that's what I did when I learned case/switch only worked on int, char or string in some languages.
It is god damn horrible and retarded.
>>60443753
Is that Scala?
I can never decide if Scala is nice or an abomination.
>>60443620
it can make a difference due to scoping
>>60443842
This specifically is Scala.
> I can never decide if Scala is nice or an abomination.
It's a powerful language with a lot of features. You can of course use powerful languages with a lot of features features to create veritable abominations.
But this kind of code is pretty damn simple and obvious & it won't create any complexity further down in the program. Shouldn't be bad to use.
>>60443620switch(i) {
case 1: {
// do stuff
} break;
case 2: {
// do stuff
} break;
}
How do you people post code on /g/?
yeah I know you can do this:
class 4chan{
public static void main(String[]args){...
but I wanna do it in cool white boxes like you
>>60443620
>Not aligning case with switch
>>60444590
Write code in the options field and the programming language in the name field.
>>60443620switch (x) {
case 1:
// do stuff
break;
case 2:
// do more stuff
break;
}
>>60443620switch (i) {
case 1:
//do stuff
goto EXIT;
case 2:
//do stuff
goto EXIT;
}
EXIT:
>tfw to intelligent to use an default case
class 4chan{
public static void main(String[]args){
System.out.println("Thanks friend");
}
}
>he doesn't vertically align his braces
dropped
>>60443620
>using breaks
>using gotoswitch(i) {
if(false){
case 1:
// do stuff
if(false){
case 2:
// do stuff
}}
}
>study Java every night for 2 hours at least
>forget what I learned the next night
How do I stop being a brainlet, I went through loops 2 nights in a row and I forget all about them on the 3rd night
>>60444680
Get plenty of sleep during your usual sleeping hours.
Learn and exercise over a somewhat longer period of time (a few weeks will already yield fairly permanent results if you were well rested).
#include <stdio.h>
int main(int argc, char **argv)
{ switch(argc)
{ case 1:
{ puts("You entered a single argument."); }
case 2:
{ puts("You entered two arguments."); }
default:
{ printf("You entered %d arguments.\n", argc); }}}
Daily reminder that matching curly brackets should always be in the same column.
Correct:foobar ()
{
// blah blah blah
}
Incorrect, reevaluate your life senpai:foobar () {
// blah blah blah
}
>>60444642
>intelligent
>an
while (condition) {
(args0);
(more_args0); }
if (something) {
(args1);
(more_args1); }
else {
(continue) }
>>60445004
For maximum autism drop a few tabs on the semi-colons too.
>>60444885
I did this too when I started to program.
After a while I just stopped, especially when working with other people that don't do it - not worth the effort.
>>60443620
The point of switch cases is so you can make compact split between many different cases.
if it can't be put in a single line, there is something wrong with the code.switch(condition) {
case enum_a: func_a(); break;
case enum_b: func_b(); break;
case enum_c: func_c(); break;
default:
//handle exceptions
//usually requires more lines
break;
}
>>60443620
>Anime
Hi.
Go to hell.
>>60444885
for functions, yes.
For loops or if statements, no.
>>60443620
>using switch statements
using SingleFileContainingAllMyClasses;
public static void main(String[] args)
{
int case = CaseManager.getCase();
int checkedCase = isCaseOne(case) ? CaseValues.CaseOne : isCaseTwo(case) ? CaseValues.CaseTwo : isCaseThree(case) ? CaseValues.CaseThree : CaseValues.Error;
ErrorManager.Process(checkedCase);
}
static int isCaseOne(int case)
{
if(case==CaseChecker.CaseOne)
{
//Do Something
}
return (case==CaseChecker.CaseOne);
}
static int isCaseTwo(int case)
{
if(case==CaseChecker.CaseTwo)
{
//Do Something
}
return (case==CaseChecker.CaseTwo);
}
static int isCaseThree(int case)
{
if(case==CaseChecker.CaseThree)
{
//Do Something
}
return (case==CaseChecker.CaseThree);
}
>>60445419
Aside from looking clunky what is wrong with this
>>60443830
> define associative array
> write functions
> craft both together
> you have switch
>>60445953
Casting int to bool in returns.
>>60443620switch(i)
{
case 1:
// do stuff
break;
case 2:
int n;
// do stuff
case 3:
// do stuff
break;
}
checkmate, atheists.
>>60443620
just write if statements
it looks better and much more readable than switch shit
oh and its faster too
>>60445164
>default
>handle exceptions
what?
>>60445999
>not using an enumenum E {
a {void func(){/* do a stuff*/}},
b {void func(){/* do b stuff*/}},
c {void func(){/* do c stuff*/}};
abstract void func();
}
// usage
getE().func();
>>60446217
>if sstatements are faster than a switch
in what language?
>>60446230
That's possible too. But I like my way more.
>>60446258
Probably any compiler that doesn't convert cases to ifs, which can't be many.
>>60444629
this desu
>>60443620
of course i do because im not some spaz faggot. i also put opening brackets on a line of their own because thats the correct way.
>>60446332
> convert cases to ifs
u wot m8
they both get converted to jump tables so use whatever you want and stop being an asspi
>>60446332
java converts switches to tableswitch (18+ cases) or lookupswitch (<18 cases) jvm instructions. lookupswitch performs around the same as if/else, tableswitch is faster for 18 or more cases.
>>60446566
b-but he's smarter than the compiler!!
>>60444570
I don't know why but this seems like the best way.
>>60444590
People who don't read the rules and faq should be punished somehow.
>>60445419
>>60445953
switches work on jumps to memory based on a hashing function, much less computational penalty than a conditional
>>60443620send(to, from, count)
register short *to, *from;
register count;
{
register n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}
>>60448323
>register
that's some fancy whitespace you've got there
>Let's defeat the purpose of a switch
>>60448323
>incrementing pointers
>>60448323
>>60444678
Things like this prove I don't understand switch as much as I should.
>>60444678
>using goto
>not using comefrom
>>60444678
>>60448323
what the fuck is even the point of the switch if you're allowed to subvert it's purpose?
How does the compiler even parse a switch block?
Some hacks I've seen seem incredibly arbitrary, almost like a switch block translates 1:1 to machine code and disregards variable scope or other C niceities.
>>60444607
print "test"
>>60448954
For the most part, C ends up as a cross-platform wrapper around Assembler. The C standard actually defines that the insides of a switch block only have to be valid C, with the addition of "case" and "default" as value-based jump labels.
It's not even a matter of "falling through" cases, but merely going on with execution without any regrets to them, unless you put a break.
That said, Duff's Device pls go.
>>60448323
This is some ugly archaic function signature syntax. Do it like this:int send(short *to, short *from, int count)
{
register int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}
Also, adding "register" to the parameters is silly. On nearly every modern ISA, with the exception of 32-bit Intel, the parameters are passed in through the registers already. Even on 32-bit intel, a decent compiler should put all of the variables into registers, since there are only 4, while there are 6 registers to spare.
>>60451342
>do it like this
no, don't do it at all
>>60451342
I always liked pre-ansi c syntax.
The only issue with traditional function declaration is you can't use it to forward declare anything.
>>60443822
>he unindents break
>>60443620
>2017
>switch statement
>>60443842
it's kinda both
>>60444885
the correct one is the one used by convention by most of people programming in the language
>>60451243
What's wrong with Duff's device?
>>60443620
You need to explicitly put breaks because pajeets don't know about fall-through. Only for individual projects you can go the most elegant way.
Braces are only needed if you need to have separate contexts in each case. Its absolutely a good practice.
The safest switch will have, in addition to breaks and braces, a default clause.
>>60451403
>The only issue with traditional function declaration is you can't use it to forward declare anything.
That and the fact that it looks like garbage.
>>60444745
1. isnt a program always its own first argument
2. without a "break", wont case 1 print case 1 and 2
>>60444680
"What I hear, I forget. What I see, I remember. What I do, I understand." -Confucius
#pseudoblock
if case1
f()
else
break
if case2
g()
else
break
if case3
h()
else
break
i dont get why people make it so complicated
>>60444893
new as fuck