This loop won’t terminate if I add an OR conditional statement. If one is false, then it should terminate.
//global var
int x = 100;
char *n= malloc (64);
void add(void)
{
do
{
printf("Would you like to add 1? (y/n) ");
fgets(n, 64, stdin);
//removes newline
n[strlen(n)-1] = '\0';
x++;
}
//if I add || (x!=100) it keeps looping me even if I press "n"
//otherwise without it it works fine
while((strncmp(n, "n", 1) != 0) || x!=100 );
free(n);
}
At the bottom of your loop, you’re doing
x++. Once you hit the while condition,x == 101, so your loop never terminates,xnever equals 100 when the condition is being checked.Perhaps you wanted:
Which would terminated the loop if either of the two conditions is false.
&&is the ‘logical and’ operator,||is the ‘logical or’ operator. To help you keep track, you can use a Truth Table to help you sort out the details. Logical and is the same as logical conjunction and logical or is the same as logical disjunction.