English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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....

int x = 1;

while (x < 5)
x++ ;
Console.Write(x);

2006-11-26 14:57:38 · 5 answers · asked by Christina 2 in Computers & Internet Programming & Design

5 answers

From the Console.Write(x) command, I'm assuming your using C# as your programming language of choice.

In any case, all of these answers are wrong.

the while loop only applies to the x++; statement. NOT the Console.Write(x); statement.

when 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.

when the Console.Write(x); statement executes the number 5 will be printed to the screen.

-------

to get x to be printed to the screen multiple times, the code would need to be modified to this:

int x = 1;
while(x < 5)
{
x++;
Console.Write(x);
}

the output of the program will be "2345"

-----

to 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

------

to get 2, 3, 4 and 5 printed on seperate lines, the Console.Write(x); statement would have to change to Console.WriteLine(x);

------

hope that helps

2006-11-26 15:50:12 · answer #1 · answered by Anonymous · 0 0

Sometimes it is easier to write out exactly what the code is doing.

when you enter the loop x = 1.

Then x gets incremented by 1(the ++ modifier) so x = 2, and written to the screen. So the first pass through the loop you see "2".

The next time through the loop x = 3 and gets written to the screen directly after the previous char (assuming the Write function doesn't add a line to the output) so you see "23"

The same thing happens for x = 4 then you have "234"
The same thing happens for x = 5 then you have "2345"

And now 5 is NOT < 5 so your program is done.

2006-11-26 15:04:42 · answer #2 · answered by jthomas0412 1 · 0 1

this outputs 2,3 and 4

x is initialized to 1, then the while loop is true (1 < 5) then immediately x is incremented so it is now 2, output 2 and continue. it works then same until x is reached 5. This time the loop is existed before it gets printed.

2006-11-26 15:07:56 · answer #3 · answered by ERIASM 1 · 0 1

the out put will be 1, 2, 3,4
look (x<5) the x++ add's 1 to the X. what can x be when it add and make the statement true.
(1<5) true
(2<5) true
(3<5) true
(4<5) true
(5<5) false the loop is broken

2006-11-26 15:01:15 · answer #4 · answered by Best Helper 4 · 0 1

Hi. Your example uses a 'while' loop counter. This means that for all the time that x is less than 5 the loop will continue. In your case it writes the value of x to your console (usually the monitor).

2006-11-26 15:03:42 · answer #5 · answered by Cirric 7 · 0 1

fedest.com, questions and answers