answersLogoWhite

0

Generally speaking, one will find empty statements ("do nothing" statements), in one of three places:

  1. As the body inside a "while" or "do-while" loop
  2. As the body inside a "for" loop
  3. As the first statement body in an "if..then..else" conditional (i.e the body for the "then" portion)

The first case is very bad programming (not just bad style, but bad design). There should never be an instance when you want a do-nothing body to a while or do-while loop.

The second case is usually frowned up as bad style, but there are certainly cases where it can be the proper choice. In such a case, the loop conditionals inside the for statement perform all the work, and there is no need for the body to do anything. An example would be a case where we need to find the index number of the position of a certain element that is known to be stored in an unsorted array. Here's what that would look like:

arraypos=0

for ( ; myArray[arraypos++].equals("somevalue") == false ; ) {

// do nothing

}

The third case where an empty statement is found is as the "then" body portion of an "if...then...else" statement. In this case, the do-nothing exists for readability, since the statement could easily be re-written to be just an "if...then" conditional, with the test condition negated. However, sometimes it is easier (or more understandable) to write a test condition one way, and the inverse of that being much less clear. E.g.

if ( complex_condition ) {

// do nothing

} else {

do_a_bunch_of_stuff();

}

can sometimes be clearer than

if ( ! (complex_condition) ) {

do_a_bunch_of_stuff();

}

It should be rare that this is needed, but it can help the code flow in certain cases where the logic behind the conditional is easier to understand than the inverse of that conditional.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Is missing statement an empty statement that does nothing?

empty statement does nothing, 'missing statement' is an error-message from the compiler, eg: { if (x==2) } corrected version: { if (x==2); }


Why empty set is open on the basis of def?

The empty set is open because the statement: "if x in A, some neighborhood of x is a subset of A" is true! If A is empty, the hypothesis: "if x in A" is false and so the statement is vacuously true.


Write on the composition of the atmosphere and it's usefullness to living organisms?

why i am in school and goal


In java what is empty statement.Explain its usefulness?

An empty statement in Java is just that: a statement with nothing in it. There are typically two ways to represent this:A single semicolon ;An empty block {}The usefulness of this type of statement is limited. The main use that I can think of is to fulfill the statement required for a loop structure.Here is an example that I recently used:while (sf(++n) != i);This loop will constantly call the method sf with increasing values of n until the return value of sf(n) is equal to i. Each loop in Java must have some code to execute in the loop body. In this case, all necessary work is done in the condition, and so the mandatory loop body is an empty statement.While there may be other (more clear) ways of writing this bit of code, this is an example of where that empty statement can be used.


A homophone for this statement Seeing the empty table the restaurant manager that the had left without paying his bill?

Seeing the empty table the restuarant manager guessed that the guest ahd left without paying his bill.


How utility differs from usefullness why it is subjective and why it is difficult to quantify?

Utility is how something is useful. Utility is something that can be useful.


Is the statement true or false Excel automatically creates an empty row in a table so that you are ready to enter the first record in the table?

I do not understand what you are asking. When you create a new worksheet, all of the rows are empty.


What are expression and statement in c?

All that is interesting in C code is formed from declarations and expressions. A declaration is a statement while an expression becomes a statement when you add a semicolon at the end of the expression. A semicolon by itself is a statement (the empty statement). Basically, anything that can be evaluated (has a value) is an expression. Thus assignments, operations and function calls are all expressions. Unlike an expression, a statement has no value. Statements are used to specify the order of execution, however the compiler is free to re-order statements to improve efficiency provided the re-ordering has no effect on the result. A (possibly empty) group of statements enclosed in braces, { and }, is known as a block or compound statement. A name declared in a block falls from scope at the end of the block.


Is the head of a polititian really made up 99.99999999 percent of empty space?

While the statement that the head of a politician is primarily made up of empty space is a simplification for comedic or rhetorical effect, it is based on the fact that atoms are mostly empty space due to the distribution of electrons around the nucleus. However, to say that a politician's head is 99.99999999% empty space is an extreme exaggeration and not scientifically accurate.


Which statement is consistent with the results of Rutherford's gold foil experiment?

The statement that is consistent with the results of Rutherford's gold foil experiment is that atoms are mostly empty space with a dense, positively charged nucleus at the center. Rutherford's experiment showed that most of the alpha particles passed through the foil undeflected, indicating that the nucleus is small and concentrated.


Is it wrong for a mans bulge to show?

In most cultures, this is indeed considered both impolite and a telling sign of a person's unawarenss of the usefullness of a good tailor.


Which is better helium or argon?

Argon is better. Helium can't compare to it, not in terms of cost, usefullness, availability, personality, attractive appearance, intelligence, or ambition.