What is the difference between break and continue statement? Explain with exemple.

The break statement is used in the loop to terminate the iteration of the loop. At the vary point the break statement is encountered the loop terminates. But in case of continue, when it is encountered, the control of execution ignores the remaining statements in the loop and starts execution from the very beginning statement of the loop.

Example for break statement:
    int i;
    for(i=0;i<=10;i++){
    printf("%d",i);
    if(i==3)
    break;
    }
Output: 0 1 2 3
When if condition becomes true, the loop terminates.

Example for continue statement:
    int i;
    for(i=0;i<=10;i++){
    if(i==3)
    continue;
    printf("%d",i);
    }
Output: 0 1 2 4 5 6 7 8 9 10
When if condition becomes true continue is encountered, the loop ignores the remaining lines in the loop. So 3 has not been printed.

Comments

Popular posts from this blog

How to Create Parrot OS Style Terminal/Bash Prompt on Any Linux Distro

What is Linux?

What is The Difference Between Declaration And Definition of a Variable/Function?