473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

One of those trick questions

without running it what will this code do. It compiles correctly.

private void X()
{
int i = 0;
{
i++;
MessageBox.Show(i.ToString());
} while(i < 10);
MessageBox.Show("Done");
}
Nov 17 '05 #1
7 4499
Hello,
The loop should look like as follows:

do
{
i++;
MessageBox.Show(i.ToString());
}while(i < 10);

OR:

while(i < 10)
{
i++;
MessageBox.Show(i.ToString());
}

HTH. Cheers
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
Hello,
The loop should look like as follows:

do
{
i++;
MessageBox.Show(i.ToString());
}while(i < 10);

OR:

while(i < 10)
{
i++;
MessageBox.Show(i.ToString());
}

HTH. Cheers
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
OK as a guess without compiling...
i is set to 0.
i is incremented to 1.
Message box shown with "1".
while loop counts to 9.
Message box shown with "Done".

Michael C wrote:
without running it what will this code do. It compiles correctly.

private void X()
{
int i = 0;
{
i++;
MessageBox.Show(i.ToString());
} while(i < 10);
MessageBox.Show("Done");
}


Nov 17 '05 #4
Hello,
No, MessageBox will show 1, then application will freeze as 'i' never
gets incremented after the while statement (there is a semi-colon after
'while' and 'do' is absent). And it'll never display "Done" :)
Bye!!

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
On Fri, 20 May 2005 16:31:02 +1000, Michael C wrote:
without running it what will this code do. It compiles correctly.

private void X()
{
int i = 0;
{
i++;
MessageBox.Show(i.ToString());
} while(i < 10);
MessageBox.Show("Done");
}


Without compiling and running, my guess is

i gets set to 0
i gets incremented to 1
A message box is displayed showing 1
i < 10 gets evaluated to true, but since you close the loop immediately
with a semicolon, there is no behavioral impact
A message box is displayed showing Done

For a while loop, the loop code follows while. You could also use
do...while, in which case the do goes at the beginning.
--
Tom Porterfield
Nov 17 '05 #6

"Tom Porterfield" <tp******@mvps.org> wrote in message
news:93***************@tpportermvps.org...
On Fri, 20 May 2005 16:31:02 +1000, Michael C wrote:
without running it what will this code do. It compiles correctly.

private void X()
{
int i = 0;
{
i++;
MessageBox.Show(i.ToString());
} while(i < 10);
MessageBox.Show("Done");
}


Without compiling and running, my guess is

i gets set to 0
i gets incremented to 1
A message box is displayed showing 1
i < 10 gets evaluated to true, but since you close the loop immediately
with a semicolon, there is no behavioral impact
A message box is displayed showing Done


Not at all, the while loop never ends as i is never incremented, you just
peg the CPU in an endless loop.

Willy.
Nov 17 '05 #7
On Fri, 20 May 2005 13:04:42 +0200, Willy Denoyette [MVP] wrote:
Not at all, the while loop never ends as i is never incremented, you just
peg the CPU in an endless loop.


Quite right. Thanks.
--
Tom Porterfield
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Brewer | last post by:
WARNING: The following is NOT all Pythonic. However, it is Python. This is just fun--nothing I'm going to put into production. I don't have any questions or problems--just thought I'd share. ...
15
by: Dan | last post by:
Is there a python equivalent of this trick in C? Logic_Test ? True_Result : False_Result Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
5
by: Javier Estrada | last post by:
I'm using a user-defined diagnostics stream and custom manipulators that work with it template<class charT, class traits = std::char_traits<charT> class basic_diagsstream : public...
0
by: Tim | last post by:
Dear Developers, Firstly, I'm not sure where this post should go so I apologise if this is in the wrong group or area. I'm currently interviewing for a vb.net developer who doesn't mind...
2
by: Peter Oliphant | last post by:
Sometimes it's hard to get straight when passing or storing or returning an instance of a class whether you are still dealing with the original object or a copy. For example, the '=' operator used...
5
by: Marty | last post by:
Hi, I've read that the if/else statement, or get/set accessor, can be compiled inline by the C# compiler if they are small enough. Have you seen any documentation that 'summarized' all thoses...
24
by: Piyush Agarwal | last post by:
void main { int x; if(x) printf("Hello"); else printf("World"); } What value can be given to x such that the output is "Hello World"?
14
by: petermichaux | last post by:
Hi, In the Yahoo! UI event.js file I see the following quite a bit for (var i=0,len=unloadListeners.length; i<len; ++i) { when I always just write for (var i=0; i<unloadListeners.length;...
14
by: Luna Moon | last post by:
Dear all, Can C++/STL/Boost do the vectorized calculation as those in Matlab? For example, in the following code, what I really want to do is to send in a vector of u's. All other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.