Class 24
Class 24
Class 24
==================
Jump statement is used to jump from one section of code to another section.
i) break statement
i) break statement
-------------------
It is used to break the execution of loops and switch case.
ex:
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
break;
System.out.println("stmt2");
}
}
o/p:
C.T.E : break outside switch or loop
ex:
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(true)
{
break;
}
System.out.println("stmt2");
}
}
o/p:
C.T.E : break outside switch or loop
ex:
---
class Test
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
break;
}
System.out.print(i+" "); //1 2 3 4
}
}
}
ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
continue;
System.out.println("stmt2");
}
}
o/p:
C.T.E : continue outside of loop
ex:
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(true)
{
continue;
}
System.out.println("stmt2");
}
}
o/p:
C.T.E : continue outside of loop
ex:
---
class Test
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
System.out.print(i+" "); //1 2 3 4 6 7 8 9 10
}
}
}
Various ways to declare the methods in java
============================================
1) No return type with No argument method
Q) Write a java program to perform sum of two numbers with no returntype with no
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller method
sum();
sum();
}
//callie method
public static void sum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();
//logic
int c=a+b;
class Test
{
public static void main(String[] args)
{
//caller method
sum();
}
//callie method
public static void sum()
{
int sum=0;
for(int i=1;i<=10;i++)
{
sum+=i;
}
System.out.println("sum of 10 natural numbers is ="+sum);
}
Q) Write a java program to perform sum of two numbers using no returntype with
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();//10
System.out.println("Enter the second number :");
int b=sc.nextInt();//20
//caller method
sum(a,b);
}
//callie method
public static void sum(int a,int b)
{
int c=a+b;
System.out.println("sum of two numbers is ="+c);
}
Q) Write a java program to perform cube of a given number using no returntype with
argument method?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();
//caller method
cube(n);
}
//callie method
public static void cube(int n)
{
//int result=(int)Math.pow(n,3);
Assignment
==========
Q) Write a java program to check given number is prime or not?