user1508893
user1508893

Reputation: 9783

purpose of if (true)

I've seen some code written like this:

if (true) {
... // do something
}

Why would you want to do something like this? Is there any thing special about this structure?

THanks

Upvotes: 3

Views: 890

Answers (3)

Aram Kocharyan
Aram Kocharyan

Reputation: 20431

There have been times where I've added true || or false && inside a condition to force it to execute the branch and test the code - but only during development. The code you've posted doesn't need the if condition.

Upvotes: 1

Perry Horwich
Perry Horwich

Reputation: 2846

This is one of many ways to segment out code during testing/development. Many might debate whether or not it is good coding practice, but it can be a quick and handy way to compartmentalize code. It is also a quick way to execute code that follows a complex conditional statement that you want to test.

Might be able to use it like this:

/* if (my_comlex_or_rare_conditional_case) then */
if (true) then
{
 lots of code here....
} /*End if */

Upvotes: 2

Gordon Bailey
Gordon Bailey

Reputation: 3911

Pretty much any modern compiler would just optimize this away. My guess is that someone put it there during development to let them easily remove a block of code (by changing true to false), and either forgot or didn't bother to remove it when they were done.

Upvotes: 3

Related Questions