Question: Question on output? I always have a hard time understand the out puts of these types of problems. Can some explain in a simple way to try and remember? What would the out put of this be and explain to me how. Please....\n\nint x = 1;\n\nwhile   (x  <  5)\n    x++ ;\n    Console.Write(x);

Answer: From the Console.Write(x) command, I'm assuming your using C# as your programming language of choice.\n\nIn any case, all of these answers are wrong.\n\nthe while loop only applies to the x++; statement. NOT the Console.Write(x); statement.\n\nwhen the loop begins x will be equal to 1.  the loop will increment x until x is no longer less than 5.  x will be equal to 5 when the loop exits.\n\nwhen the Console.Write(x); statement executes the number 5 will be printed to the screen.\n\n-------\n\nto get x to be printed to the screen multiple times, the code would need to be modified to this:\n\nint x = 1;\nwhile(x < 5)\n{\nx++;\nConsole.Write(x);\n}\n\nthe output of the program will be "2345"\n\n-----\n\nto get "2, 3, 4, 5", the Console.Write(x); statement would have to change dramatically.  it would have to some how know when to put the comas and the spaces\n\n------\n\nto get 2, 3, 4 and 5 printed on seperate lines, the Console.Write(x); statement would have to change to Console.WriteLine(x);\n\n------\n\nhope that helps
This question from the Yahoo Q&A forum is categorized in the topic: Computers & Internet