Nested if and Multi-Way if-else Statements

Image
Nested if and Multi-Way if-else Statements:           An if statement can be nested inside another if statement, forming a nested if statement. This allows for more complex decision-making in a program.           In a nested if statement, an if statement is placed inside another if statement. The inner if statement is considered to be nested within the outer if statement. There is no limit to the depth of nesting and an inner if statement can also contain another if statement.           Here is an example of a nested if statement: if (i > k) {     if (j > k)         System.out.println("i and j are greater than k"); } else {     System.out.println("i is less than or equal to k"); }           In this example, the inner if statement ` if (j > k) ` is nested inside the outer if statement ` if (i > k) `. This allows for different actions to be taken based on multiple conditions.           A nested if statement can be used to implement multiple alternatives. F

Named constants and naming conventions

Named constants:

        During the execution of a program, the value of a variable can be altered, whereas a named constant, or simply a constant, represents data that remains unchanged permanently. In our "ComputeArea" program, the constant "PI" retains its value consistently. To avoid repeatedly typing the value "3.14159" for "PI," it is possible to declare a constant for it. The following syntax is used to declare a constant:

final datatype CONSTANTNAME = value;

        A constant must be declared and initialized in the same statement. The keyword "final" in Java is used to declare a constant. For example, "PI" can be declared as a constant.

        The code for the "ComputeAreaWithConstant" 

  1. import java.util.Scanner;

  2. public class ComputeAreaWithConstant {
  3.     public static void main(String[] args) {
  4.         final double PI = 3.14159; // Declare a constant

  5.         // Create a Scanner object
  6.         Scanner input = new Scanner(System.in);

  7.         // Prompt the user to enter a radius
  8.         System.out.print("Enter a number for radius: ");
  9.         double radius = input.nextDouble();

  10.         // Compute area
  11.         double area = radius * radius * PI;

  12.         // Display result
  13.         System.out.println("The area for the circle of radius " +
  14.                 radius + " is " + area);
  15.     }
  16. }

        There are three advantages of utilizing constants: Firstly, if a value is used multiple times, you can avoid repetitive typing. Secondly, if you need to change the value of a constant (e.g., changing the value of "PI" from 3.14 to 3.14159), you only need to modify it in a single location in the source code. Thirdly, using a descriptive name for a constant enhances the readability of the program.

Naming conventions:

        Adhering to Java's naming conventions is crucial for ensuring readability and minimizing errors in your programs.

        To enhance clarity, it is recommended to choose descriptive names with straightforward meanings for variables, constants, classes, and methods in your program. Remember that names in Java are case-sensitive. The following conventions outline the naming guidelines for variables, methods, and classes:

  • - Variables and methods should be written in lowercase. If a name consists of multiple words, they should be combined into a single word, with the first word in lowercase and the first letter of each subsequent word capitalized. For instance, variables like "radius" and "area," and methods like "print" should follow this convention.
  • - Class names should capitalize the first letter of each word. For example, class names like "ComputeArea" and "System" should have their initial letters capitalized for each word.
  • - Constants should have every letter capitalized, and words should be separated by underscores. For example, constants like "PI" and "MAX_VALUE" should follow this convention.

        By adhering to these naming conventions, you ensure that your programs are easily readable and maintainable. Do not choose class names that are already used in the Java library. For example, since the System class is defined in Java, you should not name your class System

Comments

Popular posts from this blog

Types of Operators (Augmented assignment, Increment and Decrement, Logical)

History of The Java

Creating, compiling and Executing a simple java program