Thread: if else

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Angry if else

    how can you use an if statement to go back to the beginning of your code.

    Im trying to give the user the option to restart the program without exiting the program.

    For example, the output on the screen will be:

    Would you like to continue using this program? [y/n]

    so the how should the code read????

    Code:
                   if (c == 'y'|| c == 'Y')
    		{
                      ?????
                         ??
                      ?????
    		}
    
    		else
    		printf("\n\nThe program is now finished. Thank you!");
    Please help, this is wrecking my head
    Last edited by Salem; 11-01-2007 at 03:04 PM. Reason: Fix position of code tags

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Normally you'd use a loop:
    Code:
    do
    {
        Do stuff
        Ask user if they'd like to go again
    }while( c == 'y' || c == 'Y' );
    printf("\n\nThe program is now finished. Thank you!");
    Something like that. ...and the [code][/code] tags don't go around everything in your post, just the code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I think that you can do this
    Code:
    if(c=='y'||c=='Y') 
    {
    main();
     }
    but it is a bad programming practice....
    Last edited by sreeramu; 11-01-2007 at 08:12 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by sreeramu View Post
    I think that you can do this
    Code:
    if(c=='y'||c=='Y')
    {
    mail();
    }
    Did you mean main()? Recursive calls to main are strongly discouraged, see here.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can also use a goto statement, although goto is generally considered a sloppy hack in most cases and is almost never preferable over other techniques like loops...

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I know that but i am telling him that this way is also possible...
    You can use
    Code:
    for(;;)
    {
    .........
    }
    and
    while(1)
    {
    .........
    }

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I think that goto is also a bad programming practice....

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by sreeramu View Post
    I think that goto is also a bad programming practice....
    In most cases, you're right. Although there are a few situations (such as deeply nested loops) where it can actually make the code clearer.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    1
    friend......first during initial stage of the program, store status of environment in a buffer and when it is needed go back to that position.....following code explains it..

    Code:
    #include <stdio.h>
    #include <setjmp.h>
    
    
    jmp_buf task;
    
    void child();
    
    int main()
    {
    	int i;
    	i=setjmp(task);
    	if(i==0)
    	{
    		printf("hello");
    		child();
    	}
    	else
    		printf("world");
    	return 0;
    }
    
    
    void child()
    {
    	longjmp(task,1);
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I know goto is a bad programming practice, but will it hurt just to use it to go back to the beginning of the program?

    after declaring your variables, put an identifier (e.g. beginning).
    Code:
    beginning:
    then make an if like the one you posted in the beginning, with a goto statement.
    Code:
    if (c == 'y'|| c == 'Y')
    	goto: beginning;
    else
    	printf("\n\nThe program is now finished. Thank you!");

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The proper solution was given by hk_mp5kpdw in post 2.

    Whatever you do, don't call main like that to restart the program. It's a bad practice, and it will lead to a stack overflow error if the game is restarted enough. The fact that the function is main has nothing to do with it; A recursive function should always have a guaranteed end to the recursion.

    Goto will work, but when deciding between using goto and a loop, a loop is always better. It's easier to read, and easier to change.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I'm not disagreeing with you. But I'm curious. Why not use a goto statement in an obvious case like this? I use loops in the middle of programs. But it can be more complicated there.

    What's the problem with a goto in an obvious case like this? isn't it more efficient? and it is more readable (at least I feel it is)

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Abda92 View Post
    isn't it more efficient?
    no they're both just 'jmp's.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    For a simple case like this, goto is not warranted. Post #2 is the best way for your program.

    Efficiency is irrelevant in this program unless you're using a computer from the 70's. Also, since computers today use branch prediction algorithms, a loop is easier for them to predict and optimize than a goto which can jump anywhere in the code.

Popular pages Recent additions subscribe to a feed