90 likes | 103 Views
Explore the functionality and applications of break, continue, and return statements in programming. Learn how these statements can control flow within loops and methods, enhancing your code efficiency and readability.
E N D
Jump Statement • Three jump statements • Break, continue and return
Break and Continue with the nested loop result: j 1 and 2) only break
Break as a form goto • A break statement exits a loop before an entry condition fails. • The break statement can also be employed by itself to provide a form of “goto”. The format is: break label:
Example – use break to exit from loop break once it becomes negative
Continue - Explanation • Sometimes it is necessary to exit from the middle of a loop. • Sometimes we want to start over at the top of the loop. • Sometimes we want to leave the loop completely. For Example for (int i = 0; i < length; i++) { if (m[i] % 2 == 0) continue; // process odd elements... } it will process the odd case
Example - continue 1, 3, 5, 7 are missing
Return • It has purpose to exit from the current method • It jumps back to the statement within the calling method that follows the original method call. Example • return expression
Summary • Selection Statements • If and switch • Iteration Statements • While, do-while, for and nested loops • Jump Statements • Break, continue and return