Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having problems explaining this code and I would really like a detailed explanation of how it works.
C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x=-3, y=0, k,j;

    for( k=j=-3 ; x= x+(k<j),++j ; y+=2);
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);

    getch();
}

By the way, the answers are x=-1 y=4 k=-3 j=0.

What I have tried:

I tried to solve it systematically but I keep getting stuck at the increment phase of the problem (++j) and my answers end up different from the run program
Posted
Updated 11-Apr-18 20:46pm
v3
Comments
GKP1992 12-Apr-18 1:45am    
Are you sure this is the correct body of the for loop? There isn't a proper condition.
Hyeladi Bassi 12-Apr-18 1:48am    
yes, that's the correct body it was a question given to students about two years ago in my school but the explanation was never given just the answer
CPallini 12-Apr-18 3:21am    
It is truly awkward code, I don't see the point of coding like that (I don't even see the point of trying to understand it).

That is nasty code!
It's always a bad idea to have a construct like this:
C++
for (...);
printf(...);
because is't very difficult to see the ";" at the end of the for loop - so it looks to a casual eye like you wrote this:
C++
for(...)
   {
   printf(...);
   }
Which would have very different results. In fact, modern compilers will complain or at least warn you about it because it does cause a lot of confusion.
So start by restructuring your code:
C++
int main()
{
    int x = -3, y = 0, k, j;

    for( k = j = -3 ; ++j ; y += 2)
       {
       x = x + (k < j);
       }
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);
    return 0;
}
Now it's easier to read and a lot more obvious what is going on.
1) Preset x to -3, y to 0.
2) Preset k and j to -3.
3) Enter the loop.
3.1) Compare k and j.
3.1.1) If k is less than j, add one to x (Remember, in C the result of any logical operation is zero for false, or one for true, so k < j returns 0 or 1)
3.1.2) Otherwise, add zero to x.
3.2 Add 2 to y
3.3 Add one to j, and if that value is nonzero, loop back round to (3).
4) Print the results.

See how much easier it is to read and understand when you write it out properly?
It's still nasty code though: I'd prefer to see either this:
C++
int main()
{
    int x = -3, y = 0, k, j;

    for( k = j = -3 ; ++j ; y += 2)
       {
       if (k < j)
          {
          x++;
          }
       }
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);
    return 0;
}
Or this:
C++
int main()
{
    int x = -3, y = 0, k, j;

    for( k = j = -3 ; ++j ; y += 2)
       {
       x += (k < j) ? 1 : 0;
       }
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);
    return 0;
}
Where the values you are playing with are more explicit.
 
Share this answer
 
To understand that code you need to learn about the for loop and the prefix increment operator.

This for loop is a bit messy. Normally is in the head only the check and counter logic and the in the body.

C++
int main()
{

 for( int y = 0 ; y < 5; y+=2) //normally you check the counter
 {
   j++;
   x= x+(k<j);// strange should be x + 1 (true) ???
   if( j > y ) //alternative exit
   {
     break;
   }
 }
 
  printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);

    getch();
}
This demonstration code may led to other results than your code.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900