Branching statement:
- All the instructions are executed sequentially by default when no repetitions of some calculations are necessary.
- In some situation, we may have to change the execution order of statements based on condition or to repeat a set of the statement until certain conditions are met.
Tyes of branching statements:
1. If statement
2. If – else statement
3. Nested if....else statement
4. If ....else ladder
5. Switch case
- It is used to check the condition and if true the condition then it executes.
if (condition){ True statement; }
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a;
System.out.println("Enter a value
: ");
a = input.nextInt();
if (a >= 5) {
System.out.println("The
entered number " + a + " is greater than 5");
}
}
}
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a;
System.out.println("Enter a value
: ");
a = input.nextInt();
if (a >= 5) {
System.out.println("The
entered number " + a + " is greater than 5");
}
}
}
- It is used to check both conditions i.e. true and false.
- It executes only one condition at a time.
if (condition) { True statement } else { false statement; }
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a;
System.out.println("Enter a value
: ");
a = input.nextInt();
if (a >= 5) {
System.out.println("The
entered number " + a + " is greater than 5");
}
else {
System.out.println("The
entered number " + a + " is less than 5");
}
}
}
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a;
System.out.println("Enter a value
: ");
a = input.nextInt();
if (a >= 5) {
System.out.println("The
entered number " + a + " is greater than 5");
}
else {
System.out.println("The
entered number " + a + " is less than 5");
}
}
}
- Multiple if-else conditions are validating i.e. within condition check first after that execute.
if(condition 1) { if(condition 2) { True statement2; } else { False statement2; } } else { False statement1; }
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b;
System.out.println("Enter a result:\n 1.pass\n 2.fail\n press 1 or
2\n");
a = input.nextInt();
if (a == 1) {
System.out.println("Enter
total mark : ");
b = input.nextInt();
if (b > 450) {
System.out.println("You
can get first group i.e. maths group");
} else {
System.out.println("You
can get all group except the first group");
}
} else {
System.out.println("Passed
student only can join the 11th standard");
}
}
}
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b;
System.out.println("Enter a result:\n 1.pass\n 2.fail\n press 1 or
2\n");
a = input.nextInt();
if (a == 1) {
System.out.println("Enter
total mark : ");
b = input.nextInt();
if (b > 450) {
System.out.println("You
can get first group i.e. maths group");
} else {
System.out.println("You
can get all group except the first group");
}
} else {
System.out.println("Passed
student only can join the 11th standard");
}
}
}
- It is similar to nested if but it checks continually.
if(condition) { Statement1; } else if(condition 2) { Statement2; } else if(condition 3) { Statement3; } else { Statement4; }
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b, c;
System.out.println("Enter 3 values : ");
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
if ((a > b) && (a > c)) {
System.out.println("\n Biggest
value " + a + " is a");
} else if (b > c) {
System.out.println("\n Biggest
value " + b + " is b");
} else {
System.out.println("\n
Biggest value " + c + " is
c");
}
}
}
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b, c;
System.out.println("Enter 3 values : ");
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
if ((a > b) && (a > c)) {
System.out.println("\n Biggest
value " + a + " is a");
} else if (b > c) {
System.out.println("\n Biggest
value " + b + " is b");
} else {
System.out.println("\n
Biggest value " + c + " is
c");
}
}
}
switch(expression) { case condition 1: statement1; break; case condition 2: statement2; break; ......... ......... default statements break; }
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
System.out.println("Enter a choice 1 or 2 ");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("\n You
are chooses option 1 ");
break;
case 2:
System.out.println("\n You
are chooses option 2 ");
break;
default:
System.out.println("\n You
can only entered 1 or 2 choice ");
break;
}
}
}
package exampleprogram;
import java.util.Scanner;
public class ifcondition_program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
System.out.println("Enter a choice 1 or 2 ");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("\n You
are chooses option 1 ");
break;
case 2:
System.out.println("\n You
are chooses option 2 ");
break;
default:
System.out.println("\n You
can only entered 1 or 2 choice ");
break;
}
}
}
No comments:
Post a Comment