Break Continue and Goto Statement

Break, Continue and Goto statement are the "Jump Statement" used within the loops(for,while,do..while) to control the flow of compiler. Each of them have unique features-

main Image

  • Break statement is used to "Exit" the loop.
  • Continue statement is used to make the compiler go back again to the "Condition"(in case of while and do...while loop) or "Update Expression"(in case of for loop).
  • Goto statement is used to move the compiler in an specific or desire location.
break, continue and goto are the keywords and are written in the lower case

Break Statement

Break Statement is often use in switch_case statement as well as in loopsAs soon as the break statement is encountered within a loop, the loop iterations(Iteration is the repetition of a process in order to generate a sequence of outcomes) stops there and compiler returns from the loop immediately to the first statement after the loop. for eg:
Example 1
Example 1
Let "a < b" and "a = 0" then the compiler goes to break statement then it will get out from the loop and go to the very next statement. So the output is "Loop exit" .

So How is "Break" different from "Exit":
In "break" the compiler goes out from a loop but in "exit()" it terminates the program.

Continue Statement

Sometime in a program we need to skip some statement or makes the compiler go back to the iteration. For this we use continue statement. 
Example 2
Example 2

In while and do...while loop the compiler goes to or jumps to conditional statement to recheck the condition and according to that it moves further.

Example 3
Example 3
In for loop the compiler goes to or jumps to update statement and after then it works accordingly.

Goto Statement

Sometime in a program we want our compiler to move to a specific desire location. And for that we use goto statement.
Syntax: goto identifier_name;
              Statement 1;
              Statement 2;
                      :
              Statement n;
              identifier_name:
                         Statement a;
After declaring the identifier with goto, you just have to write that identifier name followed by " : " where you want your compiler to go.
for eg:
Example 4
Example 4

Things To Remember

  1. Since break, continue, goto are keywords so they are in lower case.
  2. Remember how continue works with each and every loops.
  3. Break and Continue can only be used inside a loop or switch_case statement.
  4. Goto can be used in or out side of a loop.

Check out our different post to get more concept.

For giving some suggestion please check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog. Thank you for your support and time...

Post a Comment

0 Comments