Class 24

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

4) Jump Statement

==================
Jump statement is used to jump from one section of code to another section.

We have two types of jump statements.

i) break statement

ii) continue statement

i) break statement
-------------------
It is used to break the execution of loops and switch case.

For conditional statement we can use if condition.

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
}
}
}

ii) continue statement


-------------------
It is used to continue the execution of loops.

For conditional statements we can use if condition.

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

2) No return type with Argument method

3) With return type with No argument method

4) With return type with Argument method

1) No return type with No argument method


------------------------------------------
If we don't have arguments then we need to ask input values inside callie 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;

System.out.println("sum of two numbers is ="+c);


}

Q) Write a java program to display sum of 10 natural numbers using no returntype


with no argument method?

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);
}

2) No return type with Argument method


-------------------------------------
If we have arguments then we need to ask input values inside main method.

Here number of arguments depends upon number of inputs.

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);

int result = n*n*n;

System.out.println("cube of a given number is ="+result);


}

Assignment
==========
Q) Write a java program to check given number is prime or not?

You might also like