Using break statement, you can 'jump out of a loop' whereas by using continue statement, you can 'jump over one iteration' and then resume your loop execution.
Eg. Break Statement
Eg. Break Statement
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- namespace break_example
- {
- Class brk_stmt {
- public static void main(String[] args) {
- for (int i = 0; i <= 5; i++) {
- if (i == 4) {
- break;
- }
- Console.WriteLine("The number is " + i);
- Console.ReadLine();
- }
- }
- }
- }
Output
The number is 0;
The number is 1;
The number is 2;
The number is 3;
The number is 0;
The number is 1;
The number is 2;
The number is 3;
Eg.Continue Statement
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- namespace continue_example
- {
- Class cntnu_stmt
- {
- public static void main(String[]
- {
- for (int i = 0; i <= 5; i++)
- {
- if (i == 4)
- {
- continue;
- }
- Console.WriteLine(“The number is "+ i);
- Console.ReadLine();
- }
- }
- }
- }
Output
The number is 1;
The number is 2;
The number is 3;
The number is 5;
The number is 2;
The number is 3;
The number is 5;
No comments:
Post a Comment