#Transfer Statement-
In Java, you can control the flow using statements like break
, continue
, return
, or throw
. For transferring control between methods or classes, you can use method calls or exceptions. Need more details?
Break // stop program executive
continue // condition true, this part skip
return // return value.
*break program-
public class jump{
public static void main (String[] args) {
For (int i=1;i<=10;++1)
{
if (1==5)
Break ;
System.out.print (i+" ");
output-
1 2 3 4
* continue program. -
public class jum {
public static void main(String[] args) {
for (int i=1;I<10;++1)
{
if (I==5)
continue;
Stystem.out.print (i+" ");
}
}
}
output-
1 2 3 4 5 6 7 8 9 10
// (5 skip)
Q - What is Switch Statement ? Full explaination?
switch is a multiple choice decision making selection statement, it used when we want to select only one case out of multiple cases.
In Java, the switch
statement is used for multi-branching based on an expression's value. It allows different code blocks to be executed depending on the expression.
Syntax-
Switch (exp)
case1 ( statement 1) ;
break;
case2 ( Statement 2) ;
break;
Case3 (Statement 3) ;
break;
case n (statement n);
break;
default: statement
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");break; // Additional cases for other days
default:
System.out.println("Invalid day"); }
In this example, if dayOfWeek
is 3, it prints "Wednesday" as it matches the case 3
. The break
statement exits the switch
block after a matching case.
The optional default
case executes when none of the specified cases match the expression's value.
Note: switch
supports byte
, short
, char
, int
, string
(from Java 7 onward), and some enumerated types as the expression.
* Program (Arithmetic operator)
What is operator ?. Full explaination
operator is a symbol that is used to perform operator according to user requirement
Operators in Java are unique symbols that perform operations on operands, which are distinct sets of data. These operands might be expressions, variables, or numbers. Java offers several kinds of operators:
Arithmetic Operators: Perform basic arithmetic operations such as adding, subtracting, multiplying, and modulus (remainder).
Some examples of operators for comparison involve contrasting values and return correct or false results based on characteristics such as greater or equal to, a lower value than that is, greater than, equivalent to, or less than to, and not equivalent to.
Logical Operators: Apply AND, OR, and NOT, among other logical operations, to boolean values.
Type Operator
Arthmetic operator (+,-,*,÷)
Relational operator (= =, != , >, <,)
logical operator (&&, ||)
increment /decrement
* Arthmetic operator program.
Class calculate
public static void main (String [] args) {
int a, b, c, ch;
System.out.print("Enter any two numbers");
Scanner s= new Scanner (system.in);
a: S. NextInt();
b=s.nextInt();
System out.print("Enter your choice:");
ch= nextInt () ;
switch(ch)
{
case 1:(a+b) ;
System.ocul print ("Addition "+c);
break;
case2:(a-b) ;
system. out. print (substraction) ;
break;
case 3: (a*b) ;
System.oul print("multiplication +c) ;
break;
case 4: c=a/b;
System.out.print(" division"+c) ;
break;
cuse 5:c=a%b;
System .out. print ("Reminder" +c) ;
break;
default:
System. out .print("invalid Choice");
}
}
}
output-
Enter your the number
20 , 10
Enter choice your :1
30
#increment/decrement-
Their are two type per
1) pre /post increment (++a/a++)
2) pre/post decrement (--a/a--)
increment decrement operator-
public class operator 2{
public static void main(String[] args) {
int a=10;
System.out.println(a--); //10---------------)
System.out.printIn (--a) //8
System.out.printin (a++) //8--------------9)
System.out.println (++a) //10
}
}
Output-
10
8
8
10
#Relational Operator-
public class operator 2 {
public static void main (string[] args) {
int a=10, b=20;
System.out.printin(a==b); // 10==20
System.out.printin (a!=b); // 10! =20
System.out.printin (a>b); //10 > 20
System.out.println(a<b); // 10<20 System.out.println(a>=3); //10 >=20
System.out.printIn (a<=b); //10<=20
output
False, true, False, true, false, true.
#logical operator-
public class operator 2{
public state void main(string []arg) {
int a=1, b=20;
system. out. printIn(a==b && a! =b) ;
system. out. printIn(a==b ||a! =b) ;
system. out. printIn(!(a>b)) ;
Output-
False, true, true
#assignment operator -
public class operator 2 {
public static void main(String[] args){
int a =10 //simple assignment operates
a+ = 10 //compond assignment = a=a+10
System.out.printIn (a);
}
}
Output-
20
*public class operator 2 {
static void main(String[] args{
int a=10; b=20,C=50;
int r = (a>b)?(a><?a:c): (b>c? b:c);
System out print (r);
}
}
Output-
50
# What is method & Full explanation?
method is an group / block of code which take input from the user, processed it, & given o/P
A method in Java is similar to a collection of guidelines that a machine adheres to in order to carry out a certain operation. It is defined inside a course, which is a coding structure. A method can accept input values, has a name, and indicates what kind of result, if any, it returns. Observe this simple example:
Note:
1) method runs only when it called method
2) code recusability.
public class MathOperations {
// This method adds two numbers.
public int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
// The main method where the program starts is
public static void main(String[] args). {
// Creating an object of MathOperations
MathOperations calculator = new MathOperations();
// Calling the add method
int result = calculator.add(5, 7);
// Displaying the result
System.out.println("Sum: " + result);
}
}