Posts

Showing posts from July, 2023

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 di...

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 di...

Two-Way if-else Statements

Image
Two-Way if-else Statements:           An if-else statement allows a program to choose between two different execution paths based on whether a condition is true or false.           While a one-way if statement performs an action only when the specified condition is true, a two-way if-else statement provides alternative actions to be taken depending on whether the condition is true or false.           The syntax for a two-way if-else statement is as follows: if (boolean-expression) {     statement(s)-for-the-true-case; } else {     statement(s)-for-the-false-case; }           The flowchart of this statement is depicted in Figure -1.    Figure-1              Figure-1 An if-else statement executes statements for the true case if the Boolean expression evaluates to true; otherwise, statements for the fal...

If statements

Image
 If statements:           An if statement is a programming construct that allows a program to choose between different paths of execution based on a specified condition. In the previous program, a message like "6 + 2 = 7 is false" is displayed. If you want the message to be "6 + 2 = 7 is incorrect," you can use a selection statement to make this minor modification.           In Java, there are several types of selection statements available: one-way if statements , two-way if-else statements, nested if statements, multi-way if-else statements , switch statements, and conditional expressions.           A one-way if statement is used to execute a block of code only if a certain condition is true. The syntax for a one-way if statement is as follows: if (boolean-expression) {     statement(s); }           The flowchart in Figure-1a visually represents how J...

Numeric type conversions

Image
Numeric type conversions:            Floating-point numbers can be converted into integers using explicit casting. When performing binary operations with operands of different types, Java automatically converts the operand with the smaller range to the type with the larger range. For example, if you multiply an integer (3) with a floating-point number (4.5), Java will convert the integer to a floating-point value (3.0) before performing the multiplication. So, 3 * 4.5 is equivalent to 3.0 * 4.5.           n Java, you can always assign a value to a numeric variable that supports a larger range of values. For example, you can assign a long value to a float variable. However, assigning a value to a variable with a smaller range requires explicit type casting. Type casting is an operation that converts a value from one data type to another. Widening a type refers to casting a type with a smaller range to a type with a larger r...

Operator precedence and associativity

Image
 Operator precedence and associativity:                   Operator precedence and associativity in Java determine the order in which operators are evaluated in an expression. Operator precedence defines which operators have higher priority over others, while associativity determines the order in which operators of the same precedence are evaluated.           Java follows a set of rules for operator precedence and associativity, which are as follows: 1. Parentheses: Operators enclosed in parentheses `()` have the highest precedence. Expressions inside parentheses are evaluated first. 2. Postfix operators: Postfix operators like `++` and `--` have the highest precedence after parentheses. They are evaluated from left to right. 3. Unary operators: Unary operators such as `++`, `--`, `+`, `-`, `!`, and `~` are evaluated next. They have a right-to-left associativity. 4. Multiplicative operators: Multi...

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

Image
  Types of Operators :  Augmented Assignment Operators:           Augmented operators, also known as compound assignment operators, allow you to combine arithmetic operators with the assignment operator in a concise way. Instead of writing separate statements to perform an operation and assign the result back to a variable, you can use augmented operators to achieve the same result in a more compact form.           For example, let's consider the statement `count = count + 1;`. This statement increases the value of the variable `count` by 1. In Java, you can use the addition assignment operator (`+=`) to achieve the same result. So, the statement can be written as `count += 1;`.           The `+=` operator is an example of an augmented assignment operator. It combines the addition operator (`+`) with the assignment operator (`=`). This operator adds the value on the right side of the oper...

Evaluating Expressions and operator Precedence

Image
 Evaluating Expressions and operator Precedence:           Java expressions follow the same evaluation rules as arithmetic expressions. When writing a numeric expression in Java, you can directly translate the arithmetic expression using Java operators.           For example, consider the arithmetic expression: (3 + 4x /5 )- (10(y - 5)(a + b + c) / x) + 9((4/x) + (9 + x)y)            This can be translated into a Java expression as: (3 + 4 * x) / 5 - 10 * (y - 5) * (a + b + c) / x + 9 * (4 / x + (9 + x) / y)           While Java has its own internal mechanism for evaluating expressions, the result of a Java expression and its corresponding arithmetic expression is the same. Therefore, you can safely apply the arithmetic rules for evaluating a Java expression. Operators enclosed within parentheses are evaluated first, and nested parentheses are evaluated from ...

Numeric Data Types , Operations and Literals

Image
 Numeric Data Types :                     Each data type in Java has a specific range of values associated with it. When you declare a variable or constant, the compiler sets aside memory space based on its data type. Java offers eight primitive data types that cater to numeric values, characters, and Boolean values. In this section, we will focus on introducing numeric data types and the operators used with them.           Table-1 lists the six numeric data types, their ranges, and their storage sizes. Table-1           In Java, there are four types available for representing integers: byte, short, int, and long. It is important to choose the appropriate type based on the range of values your variable needs to hold. For instance, if you know that an integer will always fall within the range of a byte, it is recommended to declare the variable as a byte to optimiz...

Named constants and naming conventions

Image
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"   import java.util.Scanner; public class ComputeAreaWithConstant {     public static void main(String[] args) {         final double PI = 3.14159; // Declare a con...

Assignment statements

Image
Assignment statements:             An assignment statement designates a value for a variable. An assignment statement can be used as an expression in Java.           Once you declare a variable, you have the ability to give it a value by utilizing an assignment statement. In Java, the assignment operator is represented by the equal sign (=). The structure for assignment statements follows this syntax: variable = expression;           An expression denotes a computation that involves values, variables, and operators, which, when combined, yield a result. Let's examine the subsequent code as an illustration: int y = 1; // Assign the value 1 to the variable y double radius = 1.0; // Assign the value 1.0 to the variable radius int x = 5 * (3 / 2); // Assign the value of the expression to x x = y + 1; // Assign the sum of y and 1 to x double area = radius * radius * 3.14159; // Compute the area ...

Reading input from console, identifiers and variables

Image
    Reading input from console:           In Java, the standard output device is referred to as  System.out , while the standard input device is referred to as System.in. By default, the output device is the display monitor, and the input device is the keyboard. To display information on the console, you can use the println method to output primitive values or strings.           Console input is not directly supported in Java, but you can utilize the Scanner class to create an object that can read input from System.in. The following line of code demonstrates how to create a Scanner object for this purpose:                     Scanner input = new Scanner(System.in);           By using the syntax "new Scanner(System.in)", you instantiate a Scanner object. The line "Scanner input" declares a variable named "input" of type Scann...

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