Thread: IF within a LOOP

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    IF within a LOOP

    Please can you help me,

    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;

    }

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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?

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    ok...

    what are you trying to get this to do here? I'm having trouble seeing what you want.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    That looks right to me...i think....
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    >>>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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM