Print Hello World without semicolon in C++



In this article, we will learn to print Hello World without a semicolon in C++. The semicolon (;) is used to terminate statements. The above can be achieved by using control structures like loops, conditionals, or function calls.

Different Approaches

There are multiple ways to write a C++ program without semicolons ?

Note: Doing this is a very bad practice and should never be used in real code. This is presented just as informational content.

Using if Statements

The easiest way to write a C++ Program without Semicolons is by using if statements. Almost all statements in C++ can be treated as expressions. So, if we place the statement inside an if statement with a blank pair of parentheses, we don't have to end it with a semicolon anymore.

Example

Below is an example of printing Hello World without a semicolon using an if statement ?

#include<iostream> using namespace std; int main() { if (std::cout << "Hello world!") {} }

Output

Hello World

Example

You can even take inputs, declare variables, define functions, etc this way. For example ?

#include<iostream> using namespace std; int main() { if (int N = 1) { if (std::cin >> N) {} if (std::cout << N) {} } }

Output

This will give the output(if you enter the number 21) ?

21

Using a while Loop

A while loop can execute std::cout without a semicolon, and break exits the loop immediately after printing.

Example

Below is an example of printing Hello World without a semicolon using a while loop ?

#include <iostream> using namespace std; int main() { while (std::cout << "Hello, World!\n") break; } 

Output

Hello World

Using Switch Statement

A switch case can execute std::cout inside a case block without a semicolon. switch(1) ensures a valid case is always executed and case 1 then prints the message in the block.

Example

Below is an example of printing Hello World without a semicolon using a switch Statement ?

#include <iostream> using namespace std; int main() { switch (1) { case 1: std::cout << "Hello, World!\n"; } } 

Output

Hello World
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-07T12:11:49+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements