Difference between break and continue statement with an example
Answers
Answer
Difference Between break and continue statement
Break and continueBoth “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop.
Break and continueBoth “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop.
Break and continueBoth “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop. The break statement is primarily used as the exit statement, which helps in escaping from the current block or loop. Conversely, the continue statement helps in jumping from the current loop iteration to the next loop.
Break and continueBoth “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop. The break statement is primarily used as the exit statement, which helps in escaping from the current block or loop. Conversely, the continue statement helps in jumping from the current loop iteration to the next loop.C++ supports four jump statements, namely ‘return’, ‘goto’, ‘break’ and ‘continue’. Java supports three jump statements ‘break’ ‘continue’ and ‘return’. Let’s study the difference between break and continue in the context of C++ and Java.
Hope this helps u
break
Break statement is used to terminate a loop, block of code.
For example :-
for i in range(5):
if i == 4:
break
print(i)
Following code would output:
0 1 2 3
Now, when the value of the variable i equals 4 the if statement evaluates true and hence the break statement is called. therefore, Other values of i are not printed.
continue
Continue statement is used to skip a step in a loop or function.
For example :-
for i in range(5):
if i == 4:
continue
print(i)
Following code would output:
0 1 2 3 5
Now, when the value of i becomes 4 the if statement is called (because the condition becomes true) and hence the continue statement is called which skips everything else and goes to the next step.
I have given both the examples in the Python programming language but the principle of break & continue are same in every programming language.