How to print anything without using a semicolon

In this tutorial, we will learn how to print anything without a semicolon in C++. This question is often asked in interviews as the interviewers test us on the command of the language rather than just the basic syntax. The trick is very small but knowing this will perhaps help you to gain knowledge and know how things work. You might be able to mold this and use it somewhere else.

To print anything without a semicolon anywhere in your code

The logic behind performing it is quite straight forward and simple. We will learn to perform this cool tricks with the help of a few examples.

The conventional way of printing anything:

cout<< "This is how we print any statement usually";



Now if the question is to print a line without using semicolon anywhere in the code.
There are several methods to perform this trick.

  1. using if statement
    if(cout<<"This is the trick to print a line with if statement!")


  2. using while statement
    When using the while loop just remember to put “!” before cout statement or else it will go in an infinite loop.
while(!(cout<<"This is the trick to print using while loop"))


Now we will see the logic behind it. In the statements like if, else and while; it’s not necessary to use relational or logical operators every time, we can use cout and cin as well in some cases.

The complete code to perform this trick:

 if(cout<<"Hello World")
 while(!(cout<<"This will be printed in a new line!))


 Hello World
 This will be printed in a new line!

I hope these two different ways to print string or anything without a semicolon in C++ will be helpful to you.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *