Thread: Explanation and its kinda cool

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Explanation and its kinda cool

    Ok I was reading that Sams book about C and whatever, they were talking about the != so I used it in an 'if else' statement, then I decided to try a 'do while' statement and when I compile it it looks weird, I can't explain it, can you guys tell me why it is doing this(code wise)and also why when you execute the program why it looks like it is over lapping.


    Code:
    #include <stdio.h>
    
    int userNum;
    int x=1;
    
    int main(void)
    {
    	printf("Please enter the number 1: ");
    	scanf("%d", &userNum);
    
    	{
    	if(userNum != x)
    	
    		printf("Hey! I told you to type the number 1 not %d\n\n", userNum);
    
    		else
    			printf("Thank you for followind my instructions\n\n");
    	}
    
    	do
    	{
    		printf("This is the do part");
    	}
    	
    	while(userNum == x);
    	
    	
    
    	return 0;
    }
    Thanks.

    P.S. I know why it is repeating cause I don't have a 'variable--' thing in it but why does it look so odd when you execute it?

    Also how do you make it stop by using the -- thing, I tried while(userNum ==x; x--); but it wouldn't work, can someone help, this is my first attempt at these statement things.
    Last edited by CAP; 07-24-2002 at 12:24 AM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not sure what your code is supposed to do, so here's a version of it that keeps looping all the time the user enters the desired number. Hope this helps you understand.
    Code:
    #include <stdio.h>
    
    int userNum;
    int x = 1;
    
    int main(void)
    {
        do
        {
            printf("Please enter the number 1: ");
            scanf("%d", &userNum);
    
            if (userNum != x)
                printf("Hey! I told you to type the number 1 not %d\n\n", userNum);
            else
                printf("Thank you for followind my instructions\n\n");
    
            printf("This is the do part\n");
        } while (userNum == x);
    
        return(0);
    }
    Personally, I try not to use do-while loops, especially if there's a lot of code in them. It makes things harder to read, imho.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Thats ok Hammer, I don't know if I will try and use do while a lot in any code that I write, like I said though the book was giving examples so I tried it out as a test, I don't design these programs to be all that interesting because I am the only one that sees them, it is just for me to learn by but thanks.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    Do while loops and while loops serve a great purpose, especially when a routine must be called over and over again. I find them very useful.

    Be carefull though, a do while loop will work the routine and test the condition after the first iteration, where a while loop will test the condition first, then go into the routine.

    Fun stuff to get to know and use.

  5. #5
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ya, I actually knew about the do this and then check with while to see if you have to do it again thing but thanks anyways, I will try out more loops as I go along.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed