× Home

Conditions and Loops

Condition

It provide check for the statement.

if-else statement

Used to check the condtion, it checks the boolean condition True or False.

Syntax

                        

                            if(boolean expression True or False){
                                // body
                            } else {
                                // do this
                            }
                        
                    

Example:

                        

                            public class IfElse {
                                public static void main(String [] args){
                                    int salary = 25400;
                                    if(salary>10000){
                                        salary = salary + 2000;
                                    } else {
                                        salary = salary + 1000;
                                    }

                                    System.out.printh(salary);
                                }
                            }
                        
                    

Multiple if-else statement

It executes one condition from multiple statements.

Syntax:

                            

                                if(condition 1){
                                    // code to be executed if condition 1 is true 
                                } else if (condition 2){
                                    // code to be executed if condition 2 is true
                                } else if (condtition 3){
                                    // code to be executed if condition 2 is true
                                } else {
                                    // code to be executed if condition 2 is true
                                }
                            
                        

Example:-

                            

                                public class MultipleIfElse {
                                    public static void main(String [] args){
                                        int salary = 25400;
                                        if(salary<=10000){
                                            salary+=1000;
                                        } else if(salary <= 20000){
                                            salary += 2000;
                                        }else{
                                            salary += 3000;
                                        }
                                        System.out.println(salary);
                                    }
                                }
                            
                        

Output :- 28400

Loops

Loops are used to iterate a part of program several times.

for loop

Is it gernerally used when we know how many times loop will iterate.

Syntax :-

                            

                                for(initialization; condition; increment/decrement){
                                    // body 
                                }
                            
                        

Example: print numbers from 1 to 5

                            

                                public class ForLoop {
                                    public static void main(String [] args){
                                        for(int num = 1; num <=5; num++){
                                            System.out.println(num);
                                        }
                                    }
                                }
                            
                        

Example 2 :- print numbers from 1 to n

                            

                            input java.util.Scanner;
                            public class ForLoop {
                                public static void main(String[] args){
                                    Scanner in = new Scanner(System.in);
                                    int n = in.nextInt();
                                    for(int num=1; num <= n; num++){
                                        System.out.print(num + " ");
                                    }
                                }
                            }
                            
                        

While loop

It is used when we don't know how many time the loop will iterate.

Syntax

                            

                                while(condition){
                                    // code to be executed
                                    // increment/decrement 
                                }
                            
                        

Example

                            

                                public class Whileloop{
                                    public static void main(String[] args){
                                        int num = 1;
                                        while(num <= 5){
                                            System.out.println(num);
                                            num++;
                                        }
                                    }
                                }
                            
                        

do while loop

It is used when we want to execute our statement at least one time.

  • It is called exit control loop because it checks the condition after execution of statement.

Syntax

                            

                                do{
                                    // code to be executed
                                    // update statement - increment/decrement 
                                } while(condition);
                            
                        

Example :

                            

                                public class doWhileloop{
                                    public static void main(String[] args){
                                        int n=1;
                                        do{
                                            System.out.println(n);
                                            n++;
                                        } while(n<=5);
                                    }
                                }
                            
                        

While loop

Do while loop

→ Used when number of iteration is fixed

→ Used when we want to execute the statement at least ones

→ Entry controlled loop

→ Exit controlled loop

→ no semicolen required at the end of while (condition)

→ semicolon is required at the end of while (condition)

Programs

Program to find largest of three numbers

"Take 3 interger input from keyboard. Find the largest number among them"

Approach 1

                            

                                import java.util.Scanner;
                                public class LargestOfThree {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        int a = in.nextInt();
                                        int b = in.nextInt();
                                        int c = in.nextInt();

                                        int max = a;
                                        if(b > max){
                                            max = b;
                                        }
                                        if(c > max){
                                            max = c;
                                        }
                                        System.out.println(max);
                                    }
                                }
                            
                        

Approach 2

                            

                                import java.util.Scanner;
                                public class LargestOfThree {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        int a = in.nextInt();
                                        int b = in.nextInt();
                                        int c = in.nextInt();

                                        int max = 0;
                                        if(a > b){
                                            max = a;
                                        } else {
                                            max =b;
                                        }          
                                        if(c > max){
                                            max = c;
                                        }                       
                                        System.out.println(max);
                                    }
                                }
                            
                        

Approach 3

Using Math.max :- Math is a class present in java.lang package and max is a function present in it which takes two number as an argument and return maximum out of them.

                            

                                import java.util.Scanner;
                                public class LargestOfThree {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        int a = in.nextInt();
                                        int b = in.nextInt();
                                        int c = in.nextInt();

                                        int max = Math.max(c,Math.max(a,b));                   
                                        System.out.println(max);
                                    }
                                }
                            
                        

Alphabet case check

“ Take an input character from keyboard and check weather it is Upper case alphabet or lower case alphabet”

                        

                            import java.util.Scanner;
                            public class AlphabetCaseCheck {
                                public static void main(String[] args) {
                                    Scanner in = new Scanner (System.in);
                                    char ch = in.next().trim().charAt(0);
                                    if (ch > 'a' && ch <= 'z'){
                                        System.out.println("Lowercase");
                                    }
                                    else {
                                        System.out.println("Uppercase");
                                    }
                                }
                            }
                        
                    

Find the nth Fibonacci number.

Fibonacci numbers → a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers.
Ex :- 0,1,1,2,3,5,8,13…

" Given three input a, b, n a is the starting number of Fibonacci series and b is the next number after a, n is an number to find the nth Fibonacci number"

                        

                            import java.util.Scanner;
                            public class FibonacciNumbers{
                                public static void main(String[] args) {
                                    Scanner in = new Scanner (System.in);
                                    int n = in.nextInt();
                                    int a = in.nextInt();
                                    int b = in.nextInt();
                                    int count = 2;
                                    while(count <= n){
                                        int temp = b;
                                        b = b+a;
                                        a = temp;
                                        count++;
                                    }
                                    System.out.println(b);
                                }
                            }                             
                        
                    

Counting occurrence

"Input two numbers, find that hoe many times second number digit is present in first number"
Ex :- first number = 14458
Second number = 4
Output = 2, because 4 is present 2 times in first number

                        

                            import java.util.Scanner;
                            public class CountingOccurence {
                                public static void main(String[] args) {
                                    Scanner in = new Scanner(System.in);
                                    int count = 0;
                                    int Fn = in.nextInt();
                                    int Sn = in.nextInt();
                                    while (Fn>0){
                                        int rem = Fn % 10;
                                        if (rem == Sn){
                                        count++;
                                    }
                                    Fn = Fn/10;
                                    }
                                    System.out.println(count);
                                }
                            }                             
                        
                    

Reverse a number

"A number I input from the keyboard and Show the output as Reverse of that number "
Example :- Input :- 12345
Output :- 54321

                            
                              
                                import java.util.Scanner;
                                public class ReverseANumber {
                                    public static void main(String[] args) {
                                        Scanner in = new Scanner(System.in);
                                        int num = in.nextInt();
                                        int ans = 0;
                                        while(num > 0){
                                            int rem = num % 10;
                                            num /= 10;
                                            ans = ans * 10 + rem;
                                        }
                                        System.out.println(ans);
                                    }
                                }                                 
                            
                        

Calculator Program

                                

                                    import java.util.Scanner;
                                    public class Calculator {
                                        public static void main(String[] args) {
                                        Scanner in = new Scanner(System.in);
                                        // Take input from user till user does not press X or x
                                        int ans = 0;
                                        while (true) {
                                            // take the operator as input
                                            System.out.print("Enter the operator: ");
                                            char op = in.next().trim().charAt(0);
                                            if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') {
                                                // input two numbers
                                                System.out.print("Enter two numbers: ");
                                                int num1 = in.nextInt();
                                                int num2 = in.nextInt();
                                                if (op == '+') {
                                                    ans = num1 + num2;
                                                }
                                                if (op == '-') {
                                                    ans = num1 - num2;
                                                }
                                                if (op == '*') {
                                                    ans = num1 * num2;
                                                }
                                                if (op == '/') {
                                                    if (num2 != 0) {
                                                    ans = num1 / num2;
                                                    }
                                                }
                                                if (op == '%') {
                                                    ans = num1 % num2;
                                                }
                                                } else if (op == 'x' || op == 'X') {
                                                    break;
                                                } else {
                                                    System.out.println("Invalid operation!!");
                                                }
                                                System.out.println(ans);
                                            }
                                        }
                                    }