- Programs in java will be executed in sequential manner means line by line execution of statements.
- By using control statements we can control the flow of execution.
- We have three types of control statements in java.
- Decision making Statements.
- Iteration statements.
- Branching statements.
1.Decision making statements in java:
- In some cases we will have a situation like need to execution set of statements based on a condition
- In this scenario we need to use decision making statements in java.
- if
- if-else
- if-else-if
- switch
Simple if
- Based on some condition if we want to execute some set of statements then we need to use if condition in java.
- We need to keep all the required statements inside if block .
- If the condition is true then control executes the whole block otherwise it skips the if block of statements.
- if( condition ){
- // code
- }
Java example program on if control statement:
- package controlstatementsinjava;
- import java.util.Scanner;
- public class ControlStatementsInjava {
- /**
- * @website: www.instanceofjava.com
- */
- public static void main(String[] args) {
- Scanner in= new Scanner(System.in);
- System.out.println("Please enter a number");
- int n=in.nextInt();
- if(n%2==0){
- System.out.println(n+" is even number");
- }
- if(n%2!=0){
- System.out.println(n+" is odd number");
- }
- }
- }
Output:
- Please enter a number
- 5
- 5 is odd number
If else condition:
- If the condition is true then control executes the whole block otherwise it skips the if block of statements
- if condition is false if we need to execute another set of statement we can write another if and keep all the statements in that block.
- instead of checking multiple times we can simply use else block .
- More about in else statement in java
- if( condition ){
- // code
- }else{
- //code
- }
if else control statement with example programs:
- package controlstatementsinjava;
- import java.util.Scanner;
- public class ControlStatementsInjava {
- /**
- * @website: www.instanceofjava.com
- */
- public static void main(String[] args) {
- Scanner in= new Scanner(System.in);
- System.out.println("Please enter a number");
- int n=in.nextInt();
- if(n%2==0){
- System.out.println(n+" is even number");
- }else{
- System.out.println(n+" is odd number");
- }
- }
- }
Output:
- Please enter a number
- 5
- 5 is odd number
if else ladder :
- In some cases we may get requirement with multiple conditions in such cases we need to use if else ladder.(if -else-if-else if -else...)
- if( condition ){
- // code
- }else if{
- //code
- }else {
- //code
- }
if else if control statement with example programs:
- package controlstatementsinjava;
- import java.util.Scanner;
- public class ControlStatementsInjava {
- /**
- * @website: www.instanceofjava.com
- */
- public static void main(String[] args) {
- Scanner in= new Scanner(System.in);
- System.out.println("Please enter your age");
- int age=in.nextInt();
- if(age<=18){
- System.out.println("Your age is between 1-18");
- }else if ((age>=18) && (age<=35)){
- System.out.println(" Your age is between 18-35");
- }
- else if ((age>=35) && (age<=50)){
- System.out.println(" Your age is between 35-50");
- }else{
- System.out.println(" Your age is above 50");
- }
- }
- }
Output:
- Please enter your age
- 20
- Your age is between 18-35
Switch statement in java:
- Instead if writing number of if conditions we can use single switch statement
- switch(value)
- case n1: statements;
- break;
- case n2 : statements;
- break;
- default: statements;
- }
Java example program on switch statement in java:
No comments:
Post a Comment