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.

Edited 5 Years Ago by Aia: Correction

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.

Edited 5 Years Ago by alex1050: n/a

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'

Edited 5 Years Ago by Aia: n/a

This article has been dead for over six months. Start a new discussion instead.