Home
Username:
Password:
Advanced C Programming Tutorials

Execution Flow / goto




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

The goto keyword has been maligned and slandered more than anything else in any programming language and it's true that with modern programming structures and the way compilers optimize things today you can write code without using the goto statement. But the problem with it has never been how it worked or what it did. It was with how it was used. People put labels in what seemed like random places in the code and branched here and there willy-nilly. That creates what is known as spaghetti code and can lead to no end of confusion. But using goto statements wisely can result in code that is well structured and easy to read. I pointed out earlier that the switch-case statement is nothing more than a computed multi-way goto statement. An if statement is a conditional expression paired with a goto statement. Historically it's a conditional branch. By the way, in a study of running programs taken several years ago, it was found that the majority of branches were taken. That is something like 65 percent of the time the code branched instead of falling through to the next line of code. A for loop is a block of code with a conditional goto at its top to be taken when the loop is not executed and a conditional goto at the bottom to be taken if the loop is to be executed again. Even a function call is a goto statement. It stores the return address in a known location, then jumps to the code in another location. The code in the other location when it's completed uses a previously-stored address and executes a goto to jump back. A break or continue statement is a form of a goto. I'll have more about those two in a later lesson. The inclusion of a goto statement is never absolutely necessary. You can always write code in a different way but there could be occasions where you'd like to use it depending on your personal style. For example, you could be in a situation with nested loops and you want to get out of them. The break command only breaks the inner loop. To break out of both loops you'll need to declare a flag, set it in the inner loop, then break, then test it in the outer loop and break again if it's set. Or you can just use the goto the way it's done in this example. If you have a situation where you need to start an iteration over again from scratch, a goto statement can be used to abandon the current loop and start it all over. The rules of the goto are fairly simple. The identifier on a goto statement must be a label and the label must be inside the same function as the goto. And be careful about jumping into the middle of a block of code without the variables in the block being initialized; particularly loop counters. If you skip the initializations, you can wind up with anything.

Tutorial Information

Course: Advanced C Programming
Author: Arthur Griffith
SKU: 33965
ISBN: 1-935320-24-6
Release Date: 2009-01-30
Duration: 5.5 hrs / 82 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available