Home > Software design >  My program won't stop executing the if statement yet the condition is not met
My program won't stop executing the if statement yet the condition is not met

Time:11-01

It's supposed to be a multiplication table from 1 to 6 on the x axis and 1 to 5 on the y axis, so I have this if statement that is supposed to jump to the nezt line once the multiplication reaches 6 but it keeps on executing even though I reset the condition that is to be met within it.

#include <stdio.h>
#include <stdlib.h>
int main(){
int mult = 1;
int check = 0;
int res;
while(mult != 6){


        if (check <= 6){
            res = mult * check;
            printf("%d ",res);
            check   ;
        }
        if (check > 6 ){
            printf("\n ");
            check = 0;
        }

}}

CodePudding user response:

The if statement makes you execute or not a block.

For instance, in your code:

if (check <= 6){
    res = mult * check;
    printf("%d ",res);
    check   ;
}
if (check > 6 ){
    printf("\n ");
    int check = 0;
}

The first block will be executed when check <= 6, the second one will be executed with check > 6... but the condition in the while loop is mult != 5... and you are never modifying mult, so the condition is always true.

So, in addition to uZuMaKioBaRuTo's remark on your check variable, you also need to increment mult:

if (check > 6 ){
    printf("\n ");
    check = 0;
    mult  ;
}
  • Related