I need help understanding the do-while loop in this code. Im confused in how this do- while works because what i understand is the statement is executed then the expression is evaluated if nonzero and executes whats in while(). But its a loop so for example if i type Jack Smith should in it print multiple J's ? Plz help to understand the logic

int main()
{
    char init,ch;int i;
    printf("Enter phrase:");
    for(i=0;i < 5; i++)
        init=getchar(); 
             
        
      while((ch=getchar())!=' ')
           ;
        
        while((ch=getchar())==' ')
            ;
        
        do
           {
           printf("%c",ch);
           }
        
        while((ch=getchar())!='\n');
        {	
        //printf(" %c.\n",init);/
        }
    return 0;
    }
}

Tip: you only need to use ONE while loop.

getchar() reads chars from the standard input buffer. If there's nothing there, it will wait until you do enter some input. It does not limit how much input you are able to type. It will wait until it gets the signal of an ENTER or RETURN keypress, but it will only read one character. If you want to save the return, you must give it an int type, not a char type.

int ch = getchar(); /* right */
char ch = getchar(); /* wrong */

Now let's analyze one of your while loops

while((ch=getchar())!=' ')
;

For as long as the content of ch is not a space, continue looping and getting one character at a time from standard input buffer. That's the logic of that loop.

As for the for loop, it is there to repeat the ordeal five times.

What happens if there's no input in the buffer? That's for you to think about.

The do-while is a while that starts with the code and then checks the boolean expresion. In this case the do-while is beeing used to print the remainding characteres of the input.

The first while could be better like this:

while(getchar()!=' ');

And you aren't using

char init

then you could leave the 'for' like this:

for(int i=0;i<5;i++)
    getchar();

The do-while is a while that starts with the code and then checks the boolean expresion. In this case the do-while is beeing used to print the remainding characteres of the input.

The first while could be better like this:

while(getchar()!=' ');

And you aren't using

char init

then you could leave the 'for' like this:

for(int i=0;i<5;i++)
    getchar();
while((ch=getchar())!='\n');
        {	
        printf(" %c.\n",init);/
        }
    return 0;
    }
}

to be clear those it print every time its true?, because getchar() checks every character until false but i need to understand the logic behind it.

to be clear those it print every time its true?, because getchar() checks every character until false [...]

Isn't easier to think it in Christian?

It will make a call to printf() for as long as ch is not a '\n'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.