# IF within a LOOP

• 10-16-2001
robingleby
IF within a LOOP

I am trying VERY hard to get the program (below) to work, but I cannot get the IF statement to work.

The program should print a CALEY table, so when the 'b' variable gets to 9 it resets to 1. However, The only place I can put it in the program comes up with an EXPRESSION SYNTAX error.

I REALLY hope you can help.

(I highlighted the problem line for your ease).

Thank-you, Rob.
#include <stdio.h>

#include <conio.h>

int main (void)

{

int a = 1;

int b = 1;

int c = 1;

int d = 1;

clrscr();

printf("Cayley Table\n\r");

while (a<=8)

{

b=d;

for (c=1; c<=8;)

printf("%d",b) ,b=b+1, if (b>8)b=1 ,c=c+1;

printf("\n");

d=d+1;

a=a+1;

}

getch();

return 0;

}
• 10-16-2001
Troll_King
Code:

```#include<stdio.h> int main() {         int x = 3;         int y = 5;         printf("%s", x > y ? "yes" : "no");         return 0; }```
Example of the conditional operator. Does this help?
• 10-16-2001
kermi3
ok...

what are you trying to get this to do here? I'm having trouble seeing what you want.
• 10-16-2001
Salem
> printf("%d",b) ,b=b+1, if (b>8)b=1 ,c=c+1;
You need to write this using some ; to separate statements, and {} to group statements

Code:

```printf("%d",b); b=b+1; if (b>8) {   b=1;   c=c+1; }```
At a guess....
• 10-16-2001
kermi3
That looks right to me...i think....
• 10-16-2001
DanMan
>>>printf("%d",b) ,b=b+1, if (b>8)b=1 ,c=c+1;

I see a HUGE problem with that line. This program shouldn't even compile (I'm guess it doesn't, and that's your problem). You've got to separate statements with ;'s, not ,'s. Also, you should break your code up into sections (by using brackets) to make it more readable). Properly formatted and terminated, that line should be:

printf("%d",b);
b=b+1;
if ( b > 8 )
b=1;
c=c+1; //This line is NOT part of the condition, it will be executed no matter what the if statement evaluates to.

Hope that helps...