• break

    What is break?:

    • break will terminate a loop or exit a switch

    Why use break?:

    • allows you to stop the loop/switch if an error or irregular condition is detected

    How to use break?:

    break.syntax:

      Things to note:
    • there are no arguments
  • break Examples

    Simple break Example:

    This program shows how to use a break statement:

    break.c:

      Things to note:
    • the loop is completely exited
    • no other iterations of the loop is executed

    break Example with Nested Loops:

    This program shows what happens when using a break statement within a nested loop:

    break_inner.c:

      Things to note:
    • break only exits inner loop
    • a break within the outer loop is needed to exit the outer loop as well
  • continue

    What is continue?:

    • continue allows you to bypass the remainder of the current iteration of a loop

    Why use continue?:

    • continue allows the loop to continue on to the next iteration
    • break, in contrast, will exit the loop entirely

    How to use continue?:

    continue.syntax:

      Things to note:
    • there are no arguments
  • continue Example

    continue Example:

    This program shows how to use a continue statement:

    continue.c:

      Things to note:
    • the current iteration of the loop is skipped
    • the loop continues on to the next iteration
  • goto

    What is goto?:

    • goto allows you to jump to different sections of your program
    • goto is a carry-over from older programming languages

    Why use goto?:

    • you should not use goto unless absolutely necessary because goto program flow is hard to follow
    • you should try to use other flow control mechanisms before using goto:
      • if/else
      • break
      • continue
    • the only possible time to use goto might be to break out of jump out of a doubly nested loop
      • but even then you could use two if - break statements

    How to use goto?:

    for.syntax:

      Things to note:
    • you need add a label somewhere in your program
    • labels cannot have the same name
    • the labelled code is still executed as normal code, even if the program reaches that portion of the program without first reaching the goto statement
  • goto Example

    This program shows how to use goto

    goto.c:

      Things to note:
    • the out_of_both_loops statements are still executed when the program exits out of both loops without the goto statement
  • return

    What is return?:

    • return causes the program logic to return to the point from which the function was called
    • return optionally returns information to the point from which the function was called

    Why use return?:

    • to return a value
    • to return control of the program
    • to end the function
      • if within the main function, then the program will end

    How to use return?:

    return.syntax:

      Things to note:
    • return does not need to return an expression, e.g. expression is optional
  • return Example

    This program shows how to use return

    return.c:

  • Fake a Switch with goto

    Update this program to implement a switch using goto

    switch_incomplete.c:

  • Assignment

    Homework Problem 29.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_29_1.c
      • example:
      • zhaol_29_1.c
    • Write a program that allows the user to input a positive integer. The program will count from 1 up till the positive number. For numbers that are divisible by 3, print out Fizz instead of the number. For numbers that are divisible by 5, print out Buzz instead of the number. For numbers that are divisible by 3 and 5, print out FizzBuzz instead of the number. Try to use the continue statement or the goto statement to accomplish this.

      For example:

      Please enter the max number to count to:
      [user enters in 31]
      1
      2
      Fizz
      4
      Buzz
      Fizz
      7
      8
      Fizz
      Buzz
      11
      Fizz
      13
      14
      FizzBuzz
      16
      17
      Fizz
      19
      Buzz
      Fizz
      22
      23
      Fizz
      Buzz
      26
      Fizz
      28
      29
      FizzBuzz
      31