How to prematurely exixt a for loop? Goto ? Break?

How to prematurely exit a for loop? Goto Break?

What the prefered way to exit a for loop prematurly? I intented to use a goto statmetn, but in reading about using a goto statemetn to exit a loiop I read it's shoudl not be done with no explination of why.

Looking for advice on this.

Thank you

break.
It's what it is designed for. (Pun intended)

break, goto is a left over from the past which you should forget about :wink:

Why not use goto? Will the universe come to an end if used? Or is break just the preferred method and in reality either would work fine.

If you do it wrong, yeah. So using all safer C++ alternatives is the way to go :slight_smile:

Doug101:
Why not use goto? Will the universe come to an end if used? Or is break just the preferred method and in reality either would work fine.

If you want to start a long thread about programming language design, this is a good question to ask,
otherwise just accept the wisdom that goto is a long dead language feature, completely replaced by break, continue, return and switch().

Thank you for answering my questions and training me. Now could you educate me just a bit and give me a short explanation as to why? Not looking for a dissertation, but just enough so I can understand.

Thank you.

Using gotos can make a program into spaghetti very quickly.
That's not to say that careful use of goto isn't useful.

So it’s for the humans and not a software/hardware thing? One of the said it was a C thing which made me think exiting out of a for loop with a goto might leave some code in memory, such as the loop counter value which could lead to other problems. Where as a break cleanly exits the loop and the loop counter value could be flushed from memory.

Doug101:
So it’s for the humans and not a software/hardware thing? One of the said it was a C thing which made me think exiting out of a for loop with a goto might leave some code in memory, such as the loop counter value which could lead to other problems. Where as a break cleanly exits the loop and the loop counter value could be flushed from memory.

You're overthinking the capabilities of an eight bit microcontroller.

Since a 'for' loop is most commonly used to iterate over known number of items (like array indices), you may want to reconsider its use in your application.

Perhaps a 'while' loop would be better suited. Then, if you need to bail out, you simply set a variable such that the condition at the top of the loop evaluates to false. The loop will then exit "naturally".

To your question, 'goto' has been out of favor in CS theory since at least the 70's, probably earlier. Google 'structured programming'.

goto doesn't allow you to do anything that other structures don't allow you do to - and makes the code considerably harder to maintain. If you get used to using them, it will also make it harder to adapt to other programming languages that don't have a goto (most modern languages don't).

It's not a C thing per se - but since most other mainstream languages don't have goto, the matter of goto is often associated with c.

I am trying to learn. Giving me answers like spaghetti code or it’s out of favor is not educating me, it’s keeping me stupid.

You are correct I might be over thinking this but isn’t that how we learn? If I can. Understand why I should use goto then I should be able to apply that reasoning to other programming questions that come along.

By keeping me in the dark means you are keeping me stupld so I will continue to ask stupid questions. I’m trying to break that cycle and become just a bit smarter. Won’t you help?

Won't you help?

This topic has been debated for decades. Are you too lazy to use Google?

Search for "use of goto" to bring up 95 million discussions. Surely you will learn something.

DrAzzy:
goto doesn't allow you to do anything that other structures don't allow you do to - and makes the code considerably harder to maintain. If you get used to using them, it will also make it harder to adapt to other programming languages that don't have a goto (most modern languages don't).

It's not a C thing per se - but since most other mainstream languages don't have goto, the matter of goto is often associated with c.

Thank you.
So it's more a human readability thing and has nothing to do with the language or hardware. Goto is a command that can still be used but other commands are just more appropriate Kind of like the English word far-out, cool and give-me-five are not in use anymore. Instead we use outta-sight, that's hot and chest-bumps.

No not too lazy to use Google. In fact I'm putting forth the effort to become more educated by having a dialog with people who know more than I.

Doug101:
So it's more a human readability thing and has nothing to do with the language or hardware.

Saying "its a human thing" needs to be placed in context: it's humans that write, modify, and maintain code.

It's difficult to describe spaghetti code if you have never seen it, and most people these days will never have seen it because they have grown up with block-structured computer languages. Loops that overlap, ifs that might be subroutine calls, there's no indenting so you can't really see by looking what a piece of code is supposed to be doing or how it fits into the whole. There are a dozen different ways to accomplish what a while() statement does, and ever programmer has their favourite little idiom.

Actually, it's not just humans. The java programming language got rid of goto altogether to make it possible to prove mechanically that a given section of bytecode will not break security. At every point of execution, you can determine what variable types are going to be on the stack, so it's not possible to craft malicious code that turns an arbitrary integer into an object reference. Java class loaders run a security check on every bit of code before it is pulled into memory for execution. This makes it possible to run server farms - servers running code that you yourself didn't write.

Hows this for a rule-of-thumb: spaghettification of code happens when gotos are used to jump into the middle of a loop or other nested block. Jumping out is usually perfectly fine.

Doug101:
Giving me answers like spaghetti code or it’s out of favor is not educating me

Perhaps not, but if you had followed the suggestion in the sentence immediately following that statement: "Google 'structured programming'", you would have been provided with links to a plethora of information.

PaulMurrayCbr:
Saying "its a human thing" needs to be placed in context: it's humans that write, modify, and maintain code.

It's difficult to describe spaghetti code if you have never seen it, and most people these days will never have seen it because they have grown up with block-structured computer languages. Loops that overlap, ifs that might be subroutine calls, there's no indenting so you can't really see by looking what a piece of code is supposed to be doing or how it fits into the whole. There are a dozen different ways to accomplish what a while() statement does, and ever programmer has their favourite little idiom.

Actually, it's not just humans. The java programming language got rid of goto altogether to make it possible to prove mechanically that a given section of bytecode will not break security. At every point of execution, you can determine what variable types are going to be on the stack, so it's not possible to craft malicious code that turns an arbitrary integer into an object reference. Java class loaders run a security check on every bit of code before it is pulled into memory for execution. This makes it possible to run server farms - servers running code that you yourself didn't write.

Hows this for a rule-of-thumb: spaghettification of code happens when gotos are used to jump into the middle of a loop or other nested block. Jumping out is usually perfectly fine.

Thank you. I appreciate the explanation.