Avoid goto

Duration: 3 min

This video lesson is available to enrolled students.

Enroll to watch — ISRO Scientist/Engineer 'SC'

AI Summary

An AI-generated summary of this video lecture.

The lecture focuses on the `goto` keyword in C programming, emphasizing its drawbacks and demonstrating its usage through code examples. The instructor argues that `goto` should generally be avoided because it makes code unreliable and hard to debug, suggesting structured constructs like `if`, `for`, and `while` as better alternatives. He transitions from theoretical warnings to practical coding demonstrations, showing how `goto` can be used to jump to specific labels within a function.

Chapters

  1. 0:00 2:00 00:00-02:00

    The instructor introduces the topic with a slide titled "2 - The goto Keyword". He advises avoiding `goto` as it makes a C programmer's life miserable and programs unreliable, unreadable, and hard to debug. He suggests using `if`, `for`, `while`, and `switch` for more elegant solutions. He presents a code snippet where `if (goals <= 5)` triggers `goto sos`, jumping to a label `sos :` that prints "To err is human!". Conversely, the `else` block prints "About time soccer players learnt C" and "said goodbye! adieu! to soccer" before calling `exit()`. The instructor underlines the `goto sos` and `sos :` labels to show the jump mechanism and explains how the program terminates if goals are high.

  2. 2:00 2:41 02:00-02:41

    A second example demonstrates `goto` with nested loops to break out of multiple levels. The code initializes `i, j, k` and runs three nested `for` loops from 1 to 3. Inside the innermost loop, an `if` condition checks if `i == 3 && j == 3 && k == 3`. If true, it executes `goto out`, skipping the `printf` statement for that specific iteration. The label `out :` prints "Out of the loop at last!". The instructor underlines the loop conditions `i=1; i<=3; i++` and circles the final output message "Out of the loop at last!" to highlight the control flow transfer and the specific moment the jump occurs.

The lesson progresses from theoretical warnings about `goto` to practical examples showing how it transfers control flow, first with a simple conditional jump and then with a complex nested loop scenario, illustrating both the functionality and the potential for confusing logic. It highlights the importance of understanding label-based jumps in C programming.