With your original code, the total output is 0 3 8 15 24. The 0 is the result of the first run. Since count starts off equal to 1 then it is incremented so count = 2 and the expression result is 0 (2 * (2 - 2)), the next increment makes count = 3 so the expression will return 3 (3 * (3 - 2)). And so on.
1) count = 1;
2) loop while count <= 5;
3) count = count + 1; // the value of count is 2
4) val1 = count - 2; // the value of val1 is 0
5) val2 = count x val1; // the value of val2 is 0
6) print val2
7) goto #2
it should be 3 as 3 is the result after second iteration of the loop body. In the second iteration value of count is already 2 when the condition statement is processed. So when entering the loop body, the value of count will get incremented to 3 as count++ is used. And the result is displayed after calculating (count(count - 2)) which is logically equivalent to (3(3-2)) which equals 3.
He he he. Actually, the OP's confusion is valid. After the state while( count++<=5), count's value will already be 2, thus cout<<count*(count-2)<<" "; will be 2(2-2)==>2(0)==>0. It's like the sum(A1:A5) function in excel returning different result compared to A1+A2+A3+A4+A5.
If you're very much sure there's nothing to check in your codes, then there might be something wrong with your compiler.
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.