answersLogoWhite

0


Best Answer

Yes. A while statement ends in a statement...

while (expression) statement

...and that statement can be a null statement, a single statement, or a block of statements. In the case of the block of statements, there is also a set of braces surrounding them...

while (expression);

while (expression) statement;

while (expression) {

statement1;

statement2;

...

statementN;

}

In the case where the body of the statement is null, there is no body. This is often done while taking advantage of side effects. For instance, to copy a string you could use...

char *strcpy (char *pszDestination, char *pszSource) {

char *pszTemp = pszDestination;

while ((*pszDestination++ = *pszSource++) != '\0');

return pszTemp;

}

...this works because the post-increment (++) operator has higher precedence than the dereference (*) operator, and because the assignment (=) operator has the value of the assignment, which is compared using the not equal (!=) operator against the string terminator null.

Note, carefully, the inner parentheses. They are needed because != has higher precedence than =, and you want it the other way around. Also, some compilers will let you eliminate the != '\0' terms and the inner parentheses, but that is not portable, and most compilers will warn you about assignment in a conditional expression.

In the case of a single statement you could use...

i= -1;

while (++i < argc) printf ("%d %s\n", i, argv[i]);

...here the while statement also ends in a semicolon.

The case of the block of statements is not shown, because it seems to be understood from the context of the question.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can a while statement end in a semicolon?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is null statement?

in C: a semicolon in itself. Examples:1. while (*to++ = *from++);2. { goto END; ... END:; }


What statement marks the end of a do while loop?

It's the semicolon after the while ()part:i= 0;do printf ("%d %s\n", i, argv[i]); while (++i


Can you write plus at end of c statement?

No. That would be a syntax error. Only a right semicolon (;) can go at the end of a statement.


Where you have to put semicolon in c programming?

For clasesses it defines from which class to inherit. :: means area of visibility in certain name space.


What is statement terminator?

Semicolon, in some languages. Line-end in some others.


Every complete statement ends with a?

Any teacher will expect you to answer by saying a semicolon (;), but this is not strictly true. First of all, the definition of a "line of code" varies from teacher to teacher and textbook to textbook. Second, even the Java Language Specification lists several types of Java statements which do not need to end in a semicolon.In general, a complete Java statement will end in either of semicolon or a closing block brace.


Which symbol is used as a statement terminator in C?

semicolon ';' (Not applicable for block-statements)


What is a statement that has only a semicolon standing alone?

A single semicolon standing alone does not form a complete statement.


What happens when you place a semicolon after a statement?

When a statement is followed by a semicolon, this means that there is going to be another statement following the first statement which is related to it closely enough that it should not become a separate sentence.


Why the semicoloumn is used in c program?

Why semicolon? Tradition.What does it do? Terminates a single statement, eg:i+= 3; /* expression is a single statement */{ i= 3; --j; } /* no semicolon after the compound statement */


When no semicolon is used after printf statement what will be the output?

A semi-colon denotes the end of a statement. If you omit the semi-colon, the statement will extend into the next expression and this will result in a compiler error because a semi-colon will have been expected at the end of the first expression.


Why we can't use semicolon at the end of if ststement?

The computer language has a grammar for the syntax. Not all computer languages using ; to end a statement. The if-statements DO end with an ; (except when a &lt;compound statement&gt;) in C#, C, PHP, and Java (and many others). In fact, most of &lt;statement&gt; end with ; in those languages, and &lt;if-statement&gt; is just one of the derived &lt;statement&gt;. However, for statements like: if (1 == 2) {} else {}, the {} is a &lt;compound statement&gt; which does not end with a ; syntactically.