CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Reading: K&R 3.7-3.8.
Please submit a question by 9 p.m. on Monday, 28 February 2011.
Goto
Can you explain what the code in section 3.8 that is used to explain "goto" means? the parts they leave out make it kind of confusing for me. What exactly does it do when it finds a match? it just says: goto found.
The goto
operation means change where you are in the sequence
of statements you're executing, so that the next statement to execute
is the one with the associated label
. Therefore,
effectively means goto
foundget out of these loops, and jump
to the
.
found label
Goto seems pointless, the same result could just be given by statements in the if conditional right?
Goto seems to make code less concise, so why bother?
You can get the same results without the goto operation, but there are times that it can make the code a bit more concise (and perhaps even clearer). In the end, it's one of those things that you should know about, but should also use sparingly.
The reading says "(a label) can be attached to any statement in the same function as the goto." I assume this means that goto-ing (going to?) a label is just executing the next statement after it. If this is true, what happens if you goto a label at the end of a function? Does it change if it's main due to the implicit return 0 at the end of main?
Yes, you can put a label at the end of a function. However, if you
put the label after the last return statement, the client of your
function is likely to get garbage as a return value. The same holds
for main.
goto seems like it could be useful for error-testing.
Yes.
The textbook didn't explicitly tell us what a "label" is. Could you explain that further?
It's simply a word followed by a color that is used as the target of a goto statement.
Kernighan and Ritchie advise against the use of goto statements, saying that though gotos often shorten codes, they are more difficult to understand. What is your opinion on goto statements? Should they be implemented or avoided?
I rarely use gotos, and prefer that others avoid them, too. For certain error-handling situations, they are useful, but that's about it.
I don't understand goto in general...would we be using it much in our codes?
Rarely.
How often is goto statement used?
Most programmers rarely use goto. Certain circumstances, such as the example in the book, are the rare exceptions.
Why is goto rarely used?
Because it makes your code much harder to analyze. Consider a simple while or for loop (without a break or goto). At the end of the loop, you know that the condition no longer holds. But if you can leave the loop with break or goto, the condition may still hold.
Continue
I'm unclear about the use of continue. By starting the next iteration of the loop, wouldn't it neglect everything written after it?
Yes, that's the point of continue.
Is continue really needed? From my understanding a program
could be written
if (TEST) ; else STATEMENT;
However, K&R implies it would need to look like:
if (TEST) continue; else STATEMENT;
Actually, you can write it like
if (TEST) continue; STATEMENT
The primary advantages (as in many other cases) are that you can get more readable and concise code in certain situations. (One appears below.)
What's the difference between using continue and just having no action and incrementing your counter variable?
Sometimes you are deeply nested in conditionals, and it's hard to express exactly what you want. Suppose we have two parts to our loop. In the first part, we do something if X is positive. In the second part, we do something is X is less than 100. (So, we might want to do both parts.) Suppose also that in the first part, we discover that some special case is met, so we don't want to do the second part. We can express this with
if (X >= 0)
{
....
if (special_case (X))
continue;
} // if (X is positive)
if (X <= 100)
{
} // if X is less than 100
It's harder to express this in other ways.
Instead of writing if....continue...why don't we just write while it is not like this, do this...?
Well ... one is a simple conditional (within a loop), and the other is a loop. More importantly, the continue breaks out of the middle of the loop, returning you to the top.
I still don't get the difference between continue and break - could you provide an example other than the one in the book?
Continue returns you to the top of the loop. Break exits the loop altogether. I'll diagram it in class.
I don't think I understand continue very clearly. Could we run this over in class?
Yes, quickly.
Can you give an example of trim, goto and strlen together?
Not right now.
How do you return multiple different error messages in different cases?
Different labels. Different conditions.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Mar 1 10:36:36 2011.
The source to the document was last modified on Tue Mar 1 10:36:34 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/control-flow-misc.html.