CFanatic

Go Back   CFanatic > Programming > C# Programming

Join CFanatic Forum Now

Do While loop syntax topic posted under C# Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 04-29-2007, 12:39 AM
Junior Member
 
Join Date: Apr 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
codeunlock is on a distinguished road
| More
Question Do While loop syntax

Quick question, what is the point for forcing the semicolon at the end of
the while statement? See example below:

Code:
x = 0;
do
{
  x = x + 1;
}while (x < 3);
What's the point of having the semicolon after the (x < 3)? Why can't the
compiler figure out that's the end of the while statement without the need
for the semicolon?

Thank you.
Reply With Quote
  #2  
Old 04-29-2007, 08:31 AM
shabbir's Avatar
Administrator
 
Join Date: Sep 2006
Posts: 1,158
Thanks: 48
Thanked 62 Times in 43 Posts
shabbir has a spectacular aura aboutshabbir has a spectacular aura about
| More
Re: Do While loop syntax

Thats a very interesting question and thought of answering the question as thats how its been designed but then thought about it and I came to this.

All the lines in the code gets converted to assembly or some other intermediate langauge. Now when a while loop start line is something like
Code:
while (x < 3)
but if we have the same thing in the do while loop i.e. without a semicolon there can be potential problems in while statements inside the do while loop. Compiler may become dependent on the braces { } to know if its start / End which is not also guarantee in statement having only one statements and so we dont have braces.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:01 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board