Control statements

mail@pastecode.io avatar
unknown
java
2 years ago
2.0 kB
4
Indexable
Never
package com.sai.datastructures;

// library import
import java.util.Scanner;

// Class declaration
public class Prakash {

    // main method or function, starting point of program execution
    public static void main(String[] args) {
        int n = 20;

        /*
          = -> assignment
          == -> comparision
         */

        if (n % 2 == 1) {
            System.out.println("Even umber");
        } else if (n % 5 == 0){
            System.out.println("Number is a multiple of 5");
        } else {
            System.out.println("Shitty number");
        }


        // Switch case

        int num = 2;

        switch(2)
        {
            case 1:
                System.out.println("Case1: Value is: " + num);
                break;
            case 2:
                System.out.println("Case2: Value is: " + num);
            case 3:
                System.out.println("Case3: Value is: " + num);
            default:
                System.out.println("Default: Value is: " + num);
        }

        // for

        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < 3; i++) {

            if (i == 2) {
                continue;
            }

            if (i == 3) {
                System.out.println("Breaking from for loop at " + i);
                break;
            }

            int a = sc.nextInt();
            System.out.println("A value in for loop when  " + i + " is : " + a);
        }


        // while

        int j = 0;

        while (j < 5) {
            int a = sc.nextInt();
            System.out.println("While loop " + j + " is : " + a);
            j += 5;
        }


        // Do while

        int k = 0;

        do {
            int a = sc.nextInt();
            System.out.println("While loop " + k + " is : " + a);
            k++;
        } while(k < 2);


        // nested for loop

        for (int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                System.out.println("I - " + i + " J - " + j);
            }
        }
    }
}