#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
for(ch=getche(); ch!='q'; ch=getche());
printf("Found the q");
return 0;
}
I can't understand how the for loop here works . I can understand the initialization and the conditional test . But don't get why I have to use getche function again in increment portions .
It may help to rewrite this
forloop into an equivalentwhileloop:becomes
So you repeatedly get a new character by calling
getche, until the character you get is equal to'q'.Without the second
ch=getche(), you would get only a single character and compare this to'q'over and over again (leading to an infinite loop if it isn't'q').