Pages

Monday, 16 May 2022

CONTROL STRUCTURES - Case control structures

Break Statement: This keyword helps us to control the loop from within it. Break will cause the loop to stop and will go immediately to the next statement after the loop.

Ex1:
class BreakDemo
{
    public static void main(String arg[])
    {
int i;
for (i = 0; i < 5; i++)
{
if (i >= 2)
{
break;
}
System.out.println("Inside For: "+i);
}
    System.out.println("Outside For :"+i);
    }
}
 
Output:
I:\>javac BreakDemo.java 
I:\>java BreakDemo 
Inside For: 0
Inside For: 1
Outside For :2

Continue Statement: continue will stop the current iteration and will move to the next one. Notice that inside a for loop, it will still run the third section.
Ex1:
class ContinueDemo
{
    public static void main(String arg[])
    {
    int i;
    for (i = 0; i < 5; i++)
    {
if (i >= 3)
{
break;
}
System.out.println("Skipped Break..."); 
if (i >= 1)
{
continue;
}
System.out.println("Skipped Continue...");
                }
    System.out.println("Exited from For: "+i);
    }
}

Output:
I:\>javac ContinueDemo.java 
I:\>java ContinueDemo 
Skipped Break...
Skipped Continue... 
Skipped Break...
Skipped Break... 
Exited from For: 3

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...