How to Print ‘Hello, World!’ Without Using a Semicolon

Raghu Chavva
2 min readDec 24, 2022

--

selfie in front of grave

Printing "Hello, World!" Without a Semicolon

If you're a programmer, you're probably familiar with the classic "Hello, World!" program, which is often used as a simple example to teach the basics of a programming language. This program typically consists of a single line of code that prints the phrase "Hello, World!" to the console. But did you know that there are several ways to print the phrase "Hello, World!" without using a semicolon in the code?

Here are a few examples:

1.Using an if statement:

if (System.out.printf("Hello, World!") == null) {}

In this example, the printf method is used to print "Hello, World!" to the console, and the if statement is used to evaluate the return value of the printf method. Since the printf method returns null, the condition of the if statement is always true, and the code inside the curly braces is executed. In this case, the code inside the curly braces is empty, so nothing is actually done. However, the printf method still gets called and "Hello, World!" is printed to the console.

2. Using a for loop:

Here is the full example of using a for loop to print "Hello, World!" without using a semicolon:


for (int i = 0; i < 1; System.out.printf("Hello, World!")) {
i++;
}

In this example, the for loop is used to execute a single iteration. The loop's initialization clause sets the variable i to 0, the loop's termination clause checks if i is less than 1, and the loop's iteration clause increments i by 1 after each iteration.

Inside the loop's body, the printf method is used to print "Hello, World!" to the console. The printf method is called as part of the loop's iteration clause, which means it is called after each iteration of the loop. Since the loop only executes a single iteration, the printf method is only called once and "Hello, World!" is printed to the console.

These examples may seem unconventional, but they demonstrate how you can use control statements and loops to print the phrase "Hello, World!" without using a semicolon in the code itself.

It's important to keep in mind that while it's possible to write code in unconventional ways, it's generally best to follow coding conventions and use semicolons where they are typically expected in order to make the code easier to read and understand. However, it can be fun to experiment with different approaches and see what's possible.

I hope these examples have given you some ideas for how to print "Hello, World!" without using a semicolon. Happy coding!

--

--

Raghu Chavva

Hi there!! I am Raghu Reddy, A software engineer at Accenture.Admin of the Instagram account @Java_quizs - 50k (https://www.instagram.com/java_quizs/).