Chapter[11]: Master Java Programming: Easy Step-by-Step Guide with Examples for Beginners

Automate This. By Mrigank Saxena
5 min readJan 13, 2025

--

Designed by freepik

Alright, before we dive headfirst into coding, let’s take a moment to go over some basic techniques that I’ve found super helpful. Trust me, it’s worth it!

We’ve covered a lot of coding and theory up to this point, so now let me show you how to create basic logic and convert it into Java code. If you’re just starting with Java, no need to worry! In this article, I’ll walk you through fundamental coding concepts with easy-to-understand examples, showing both logical instructions and their Java code equivalents. Each example will be explained with comments so that even someone with no prior experience can understand how things work.

1. Add Numbers

Logical Instructions:

Start //Where code starts
Print "Enter two numbers";
IN1 = 23;
IN2 = 43;
Variable Add = SUM(23, 43); // This will calculate the sum of 23 and 43
OUT(Add); // This will output the sum of 23 and 43

Java Code:

// Main - Where code execution starts
public class Main {
public static void main(String[] args) {
// Print "Enter two numbers" to the console
System.out.println("Enter two numbers");

// Declare and assign values to the variables
int IN1 = 23; // Store the first number in IN1
int IN2 = 43; // Store the second number in IN2

// Calculate the sum of IN1 and IN2 and store it in the Add variable
int Add = IN1 + IN2; // SUM(23, 43) -> Adding the two numbers together

// Print the result of the addition to the console
System.out.println(Add); // OUT(Add) -> Output the sum
}
}

Explanation:

  1. System.out.println("Enter two numbers"); prints a message prompting the user for two numbers.
  2. The IN1 and IN2 variables store the values 23 and 43, respectively.
  3. The sum of IN1 and IN2 is stored in the variable Add.
  4. Finally, the result of the addition is printed using System.out.println(Add);.

2. Use If and Else in a Scenario

Logical Instructions:

Start //Where code starts
Print "Enter a number";
IN1 = 10;
IF (IN1 > 5) {
Print "The number is greater than 5";
}
ELSE {
Print "The number is not greater than 5";
}

Java Code:

// Main - Where the program execution begins
public class Main {
public static void main(String[] args) {
// Print "Enter a number" to ask the user for input
System.out.println("Enter a number");

// Declare the variable IN1 and assign it a value of 10
int IN1 = 10;

// Check if IN1 is greater than 5
if (IN1 > 5) {
// If condition is true, print this message
System.out.println("The number is greater than 5");
} else {
// If condition is false, print this message
System.out.println("The number is not greater than 5");
}
}
}

Explanation:

  1. if (IN1 > 5) checks if the number is greater than 5.
  2. If it is, the program prints “The number is greater than 5”.
  3. If the condition is not true (i.e., the number is not greater than 5), the else block is executed, and "The number is not greater than 5" is printed.

3. Use For Loop

Logical Instructions:

Start //Where code starts
Print "Count from 1 to 5";
FOR (IN1 = 1; IN1 <= 5; IN1 = IN1 + 1) {
Print IN1;
}

Java Code:

// Main - Where the program execution begins
public class Main {
public static void main(String[] args) {
// Print a message to the console
System.out.println("Count from 1 to 5");

// Use a for loop to count from 1 to 5
for (int IN1 = 1; IN1 <= 5; IN1++) {
// Print the value of IN1 each time the loop runs
System.out.println(IN1);
}
}
}

Explanation:

  1. The for loop runs from IN1 = 1 to IN1 = 5.
  2. IN1++ increases the value of IN1 by 1 after each loop iteration.
  3. The value of IN1 is printed in each iteration, displaying numbers from 1 to 5.

4. Use While Loop

Logical Instructions:

Start //Where code starts
Print "Count down from 5 to 1";
IN1 = 5;
WHILE (IN1 >= 1) {
Print IN1;
IN1 = IN1 - 1;
}

Java Code:

// Main - Where the program execution begins
public class Main {
public static void main(String[] args) {
// Print a message to the console
System.out.println("Count down from 5 to 1");

// Initialize the value of IN1
int IN1 = 5;

// Use a while loop to count down from 5 to 1
while (IN1 >= 1) {
// Print the value of IN1
System.out.println(IN1);
// Decrease IN1 by 1 after each loop
IN1--;
}
}
}

Explanation:

  1. The while loop runs as long as IN1 >= 1.
  2. Each time the loop runs, the value of IN1 is printed.
  3. After each print, IN1-- reduces the value by 1, eventually ending the loop when IN1 becomes less than 1.

5. Enhanced For Loop (For-Each Loop)

Logical Instructions:

Start //Where code starts
Print "Display all numbers in an array";
ARRAY = [1, 2, 3, 4, 5];
FOR EACH ELEMENT IN ARRAY {
Print ELEMENT;
}

Java Code:

// Main - Where the program execution begins
public class Main {
public static void main(String[] args) {
// Print a message to the console
System.out.println("Display all numbers in an array");

// Initialize an array of numbers
int[] array = {1, 2, 3, 4, 5};

// Use an enhanced for loop to print all elements in the array
for (int element : array) {
// Print the current element
System.out.println(element);
}
}
}

Explanation:

  1. The for-each loop (for (int element : array)) iterates over all elements in the array.
  2. For each iteration, the current element is printed.
  3. The loop automatically goes through all elements without the need for index tracking.

Difference Between While, For, and Enhanced For Loops

  • For Loop:
  • Best used when the number of iterations is known beforehand.
  • Example: Counting from 1 to 5.
  • While Loop:
  • Best used when you don’t know how many iterations you need, but the loop continues as long as a condition is true.
  • Example: Counting down from 5 to 1.
  • Enhanced For Loop:
  • Best used for iterating over collections (like arrays) where you need to access each element one by one.
  • Example: Displaying all numbers in an array.

Why Use If-Else and Loops in Java?

  • If-Else Statement:
  • Helps in making decisions based on conditions.
  • Example: Checking if a number is greater than 5.
  • Loops:
  • Loops are useful for performing repetitive tasks.
  • Use a for loop when you know how many times you need to iterate.
  • Use a while loop when you don’t know the number of iterations and just want the loop to continue as long as a condition is true.
  • Use an enhanced for loop when you are working with collections (like arrays) and want a simple way to iterate through all elements.

By now, you should have a solid understanding of how to write basic Java code, with the help of logical instructions and their Java code equivalents. You’ve learned how to add numbers, use decision-making (if-else), and handle repetitive tasks with loops (for, while, and enhanced for). Keep practicing, and you’ll soon get comfortable with Java’s powerful features!

Happy Coding!

--

--

Automate This. By Mrigank Saxena
Automate This. By Mrigank Saxena

Written by Automate This. By Mrigank Saxena

Join me as I share insights, tips, and experiences from my journey in quality assurance, automation, and coding! https://www.linkedin.com/in/iammriganksaxena/

No responses yet