[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

>he wraps his switch cases in blocks switch(i) { case

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 72
Thread images: 11

File: 1451882173413.jpg (51KB, 1024x576px) Image search: [Google]
1451882173413.jpg
51KB, 1024x576px
>he wraps his switch cases in blocks

switch(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
>>
File: 1492981073501.jpg (8KB, 245x250px)
1492981073501.jpg
8KB, 245x250px
>>60443620
>he writes two breaks just to be sure
>>
File: 1494710639413.jpg (24KB, 400x267px) Image search: [Google]
1494710639413.jpg
24KB, 400x267px
>>60443753
>>
File: naomug.jpg (581KB, 1920x1080px) Image search: [Google]
naomug.jpg
581KB, 1920x1080px
>>60443750
switch(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.
>>
>>60443620
switch(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
>>
File: 1492831891533.png (341KB, 556x561px) Image search: [Google]
1492831891533.png
341KB, 556x561px
>>60443620
>Not aligning case with switch
>>
>>60444590
Write code in the options field and the programming language in the name field.
>>
>>60443620
switch (x) {
case 1:
// do stuff
break;
case 2:
// do more stuff
break;
}
>>
>>60443620
switch (i) {
case 1:
//do stuff
goto EXIT;
case 2:
//do stuff
goto EXIT;

}
EXIT:
>>
File: 1475498011922.png (39KB, 211x220px) Image search: [Google]
1475498011922.png
39KB, 211x220px
>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 goto
switch(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
}
>>
File: 1490416195864s.jpg (8KB, 192x187px) Image search: [Google]
1490416195864s.jpg
8KB, 192x187px
>>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.
>>
>>60443620
switch(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 enum
enum 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
>>
>>60443620
send(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
>>
File: 1494107702447.jpg (58KB, 547x547px) Image search: [Google]
1494107702447.jpg
58KB, 547x547px
>>60448323
>incrementing pointers
>>
>>60448323
>>60444678
Things like this prove I don't understand switch as much as I should.
>>
File: 1386026607167.jpg (35KB, 500x500px) Image search: [Google]
1386026607167.jpg
35KB, 500x500px
>>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
>>
File: 1476452183963.png (55KB, 299x315px) Image search: [Google]
1476452183963.png
55KB, 299x315px
>>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.
>>
File: 1494084830460.png (53KB, 256x256px) Image search: [Google]
1494084830460.png
53KB, 256x256px
>>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
>>
>>60454705
>>>/r/eddit
>>
>>60444893
new as fuck
Thread posts: 72
Thread images: 11


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.