Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

Control Flow, Arrays, Packages, JavaDoc Comments in Java

 



1. Control Flow

Control flow statements control the order in which statements are executed in a Java program. They include:

1.1 Conditional Statements

  • if Statement:

·         if (condition) {

·             // Code to execute if the condition is true

·         }

  • if-else Statement:

·         if (condition) {

·             // Code if condition is true

·         } else {

·             // Code if condition is false

·         }

  • if-else-if Ladder:

·         if (condition1) {

·             // Code if condition1 is true

·         } else if (condition2) {

·             // Code if condition2 is true

·         } else {

·             // Code if all conditions are false

·         }

  • switch Statement: Suitable for multiple conditions based on integers, strings, etc.

·         switch (variable) {

·             case value1:

·                 // Code for value1

·                 break;

·             case value2:

·                 // Code for value2

·                 break;

·             default:

·                 // Code if no case matches

·         }


1.2 Looping Statements

  • for Loop:

·         for (int i = 0; i < 10; i++) {

·             System.out.println(i);

·         }

  • while Loop:

·         int i = 0;

·         while (i < 10) {

·             System.out.println(i);

·             i++;

·         }

  • do-while Loop: Executes at least once before checking the condition.

·         int i = 0;

·         do {

·             System.out.println(i);

·             i++;

·         } while (i < 10);

  • Enhanced for Loop (for-each): Mainly used for arrays and collections.

·         int[] numbers = {1, 2, 3, 4, 5};

·         for (int num : numbers) {

·             System.out.println(num);

·         }


1.3 Control Flow Statements

  • break: Terminates a loop or switch statement.
  • continue: Skips the current iteration and continues with the next iteration of the loop.
  • return: Exits a method and optionally returns a value.

2. Arrays

Arrays are data structures that store multiple elements of the same type in a contiguous memory location.

2.1 Declaring and Initializing Arrays

int[] numbers = new int[5]; // Declaration and instantiation

int[] values = {10, 20, 30, 40, 50}; // Declaration and initialization

2.2 Accessing Array Elements

System.out.println(values[0]); // Output: 10

2.3 Multi-Dimensional Arrays

int[][] matrix = {

    {1, 2, 3},

    {4, 5, 6},

    {7, 8, 9}

};

System.out.println(matrix[0][1]); // Output: 2

2.4 Array Length Property

int[] arr = {10, 20, 30};

System.out.println("Array Length: " + arr.length); // Output: 3


3. Packages

Packages in Java are used to group related classes and interfaces, providing a modular structure and avoiding naming conflicts.

3.1 Creating a Package

package mypackage;

 

public class MyClass {

    public void display() {

        System.out.println("Hello from mypackage!");

    }

}

3.2 Using a Package

To compile:

javac -d . MyClass.java

To use the package:

import mypackage.MyClass;

 

public class Test {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.display();

    }

}


4. JavaDoc Comments

JavaDoc comments are special comments used to generate HTML documentation for classes, methods, and fields.

4.1 Syntax

/**

 * Class representing a simple calculator.

 * @author Krishna

 * @version 1.0

 */

public class Calculator {

 

    /**

     * Adds two integers.

     * @param a the first integer

     * @param b the second integer

     * @return the sum of a and b

     */

    public int add(int a, int b) {

        return a + b;

    }

 

    /**

     * Subtracts one integer from another.

     * @param a the minuend

     * @param b the subtrahend

     * @return the difference of a and b

     */

    public int subtract(int a, int b) {

        return a - b;

    }

}

4.2 Generating JavaDoc

javadoc Calculator.java

This will create an HTML documentation file for the Calculator class.


5. Example Program (Using Everything Above)

package mypackage;

 

/**

 * Class demonstrating Control Flow, Arrays, and JavaDoc comments.

 */

public class Demo {

   

    /**

     * Displays all elements of an integer array.

     * @param arr the array to be displayed

     */

    public void displayArray(int[] arr) {

        for (int num : arr) {

            System.out.println(num);

        }

    }

 

    /**

     * Calculates the sum of all numbers in a 2D array.

     * @param matrix the 2D array

     * @return the total sum of elements

     */

    public int sum2DArray(int[][] matrix) {

        int sum = 0;

        for (int[] row : matrix) {

            for (int num : row) {

                sum += num;

            }

        }

        return sum;

    }

 

    public static void main(String[] args) {

        Demo demo = new Demo();

 

        // Demonstrating Arrays

        int[] numbers = {1, 2, 3, 4, 5};

        System.out.println("Array Elements:");

        demo.displayArray(numbers);

 

        // Demonstrating Control Flow

        int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};

        System.out.println("Sum of Matrix Elements: " + demo.sum2DArray(matrix));

    }

}


 

No comments:

Post a Comment