Sequence of Instructions in Java

ZoneTechPro
By -
0

Sequence of Instructions

Java follows a structured execution order for arithmetic calculations and programming constructs. In this post, we explore key concepts such as variable reassignment, the final keyword, expressions, statements, blocks of code, and comments.

1. Reassigning Variables

In Java, variable values can be modified after initialization.

Example:

Reassigning Variables
Reassigning Variables



 Output: 

         1

         2


2. The final Keyword

The final keyword prevents modifications to variables, methods, and classes.

2.1 Final Variables :

A variable declared as final remains constant.

Example:


Sequence of instructions
Final keyword


Output: 

    60

    Attempting to modify a final variable results in an error.


3. Expressions

Expressions are combinations of values, variables, and operators.

3.1 Order of Operations (BODMAS)

The standard precedence follows:

  • Brackets
  • Orders (Exponents, Roots)
  • Division
  • Multiplication
  • Addition
  • Subtraction

Example:



Order of Operations
Order of Operations


Output: 

    8
    2

4. Statements

A statement is a complete executable unit, ending with a semicolon (;).

Example: 


Statements
Statements

    Output: 

        1

         Omitting a semicolon results in a syntax error.

5. Blocks of Code

    A block consists of multiple statements enclosed within {}.

    Example:


     
Blocks of Code
Blocks of Code


6. Comments

        Comments enhance code readability and documentation.

    6.1 Single-line Comments: 

        Start with // and apply to a single line.

            // This is a single-line comment

            int num = 15; // Assigning value


    6.2 Multi-line Comments

            Start with /* and end with */.


            /*

           Multi-line comments can span

           across multiple lines.

            */

            int num = 15;

    

Summary

  • Variables in Java can be reassigned unless declared final.
  • final variables remain constant.
  • Expressions follow BODMAS rules.
  • Statements must end with ;.
  • Blocks of code are enclosed within {}.
  • Comments can be single-line (//) or multi-line (/* */).




Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!