[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]

Why do for loops exist? Why not just an if statement and a goto?

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: 37
Thread images: 2

File: 1485096673489.jpg (228KB, 1041x1024px) Image search: [Google]
1485096673489.jpg
228KB, 1041x1024px
Why do for loops exist? Why not just an if statement and a goto? What exactly do they add?
>>
They add an additional layer of abstraction.
>>
Why do compilers exist? Why not just write in ones and zeroes?
>>
>>58751522
please program in some old basic, they handle loops like that
>>
File: npH0dEH.png (586KB, 777x578px) Image search: [Google]
npH0dEH.png
586KB, 777x578px
>>58751522
>if statement and a goto

For fucks sake have you never played with x86 assembly?

You don't even need an if statement.
>>
Why are we here? Just to suffer?
>>
>>58751522
goto tends to produce buggy code
>>
>>58751632
Not the same thing.
>>58751653
>>58751666
I know. But why do programming languages have this feature? It's just needless bloat.
>>58751705
How?
>>
>>58751522
>goto
>ever in anything high level
>>
>>58751522
>we do functional languages have a map function
>just recursively call the same function again over and over and use if
>so much better than passing it in
>why can't programming be harder please
>>
>>58751764
Lambdas are the dankest meme
>>
>>58751764
Not the same thing.

>for loop:
for(int i=0; i == n; i++) {
...
}

>goto
loop: do {
...
}
if i == n goto loop;
>>
>>58751986
loop: do {
...
}
i++
if i == n goto loop;
>>
>>58751996
loop: do {
...
}
if (i++==n) goto loop;
>>
>>58752124
>retardedly """clever""" code: the post
>>
>>58751986
kek

>>58751996
here you have a do..while loop, it will always execute the code at least once, so it's not the same.
Also would you kindly fucking declare i somewhere?
>>
>using goto
>>
>>58751714
>it's just needless bloat
Why, programming languages are just needless bloat. Why not simply write in Assembly which is what actually gets executed by the computer?
>>
Goto was the begining of the considered harmful meem
>>
>>58751522
because its easier and you shouldnt have to worry about that when your writing high level code. Its adding difficulty for no real reason. "needlessly bloat" jesus maybe a little but its not like your codes going to run that more effecient when you replace all loops with goto's
>>
>>58751522

because you're a faggot noob

programming is all about making the programming process shorter.

for wraps two statements into one and forces a consistent program flow.
>>
>>58751522

good luck looping through an array
>>
>>58751522
convenience
>>
>What exactly do they add?
Not littering your codebase with labels.
>>
>>58751714

>It's just needless bloat.
It's called syntactic sugar, and it's not bloat. It adds nothing to the generated assembly that you would not have generated with an if statement and a goto.
>>
>>58751522
>using goto
kys faglord, but seriously for loops keep things easy, understandable and safer, you know you are safe inside a for loop, if you forget about a goto it can be catastrophic.
>>
>>58751522
Code is written for people, not compilers, and should be written in a way that documents intent.
>>
>>58752928
>assembly gets executed by the computer
stop posting
>>
>>58752928
Assembly is harder to write, debug, and read.
>>58753488
>>58753520
>>58753460
But it isn't. Explain how this:
int i = 0;
loop: do {
...
}
i++;
if i == n goto loop;

is harder to read than this:
for(int i=0; i == n; i++) {
...
}

>>58753503
start at 0, increment by one, until i = array length.
>>58754030
That ia a good point. But surely it could be solved somehow, such as scope-specific labels?
>>58754058
But it doesn't make the structure immediately more apparent.
>>58754116
Which is what goto does.
>>
>>58752124
>>58751996

>>unknown reference i
>>
>>58754972
ASM is fine man, just have to know where your registers be
>>
>>58754972
>But it isn't. Explain how this:
>int i = 0;
>loop: do {
> ...
>}
>i++;
>if i == n goto loop;
>is harder to read than this:
>for(int i=0; i == n; i++) {
> ...
>}

are you literally fucking autistic?

1) count the fucking symbols
2) what do you do if you have multiple loops in sequence? nested? do you count them all out?

>>58752124
you know, I'd laugh at you too, but I actually have to give you props. never thought of that

for(;i++<x;)

I might actually use that. didn't think I'd learn something on this board today.
>>
>>58754972
>But it doesn't make the structure immediately more apparent.
it does, but just slightly.
It is a C feature, which doesn't have the primitives that allow for us to do
for i in (1..10):
print i

as in ~~python, or
map \x -> 2*x [1 2 3]
[/spoiler]
as in ~~ML.
It's a limitation of the language and because it was a popular language, it was adopted by it's descendants.
>>
>>58754972
>>58757186
it also keeps the loop infrastructure in just one line, instead of 2 or 3.
But yes, it's a weak abstraction.
>>
Kill yourself, you dumb fucking shit stain.
>>
this is why pajeets have taken over
>>
>>58751733
>>>58751522 (OP)
>>goto
>>ever in anything high level
>>58752831
>>using goto
>>58754068
>>>58751522 (OP)
>>using goto
>kys faglord, but seriously for loops keep things easy, understandable and safer, you know you are safe inside a for loop, if you forget about a goto it can be catastrophic.
>>58757303
>this is why pajeets have taken over

>Using goto
Thread posts: 37
Thread images: 2


[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.