Thread: When using multiple While loops go to previous while loop without using 'goto'.

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    Exclamation When using multiple While loops go to previous while loop without using 'goto'.

    Hello members!!!

    I am just starting programming in C language. I had a single semester course in C about 10 years ago, when I was in college.


    I apologise in advance if this is a really newb question (or) if this question has already been asked and answered(I haven't been able to find it).
    Trust me, I have tried all sorts of searches using google, and on this forum and on StackOverflow but have failed to find the solution to my problem.

    ###########################################
    ###########################################


    Here is my problem:

    I have a programme that has multiple while loops, one for each condition. I want the loops to execute based on whether the condition is true or not.

    The problem I am facing is that My code is progressing through the loops from top to bottom just fine. It is going to the next loop if the next loop's condition is being satisfied and is even repeating the current loop if the current loop's condition persists, but is not going to the previous loop and executing that loop even when the condition for the previous loop is being satisfied. The programme just quits.


    P.S.

    I know the same output that I want can be achieved using an 'if' loop and SubRoutines. But I want to use the while loop.

    I also know that I can use 'goto' and make my programme work flawlessly.

    I absolutely LOVE 'goto'.
    I personally feel it's one of the best things in programming(I guess that's proof that I'm a newbie). But I've heard from most of the programmers online and even in most books, the use of goto is shunned.

    So, I'm trying to get rid of my habit of using goto and I wanna know how to get my stuff to work.
    Code:
    void main()
        {
            printf("please type two variables, a and b\n\n");
            int VarA=0;
            int VarB=0;
            scanf("%d %d", &VarA, &VarB);
            getchar();
            int SuM=0;
            SuM=VarA+VarB;
                while (SuM>25)
                    {
                        printf("The sum of numbers is greater than 25\n\n");
                        printf("please type two variables, a and b\n\n");
                        scanf("%d %d", &VarA, &VarB);
                        getchar();
                        SuM=VarA+VarB;
                    }
                while (SuM<25)
                    {
                        printf("The sum of numbers is lesser than 25\n\n");
                        printf("please type two variables, a and b\n\n");
                        scanf("%d %d", &VarA, &VarB);
                        getchar();
                        SuM=VarA+VarB;
                    }
                while (SuM==25)
                    {
                        printf("The sum of numbers is EQUAL to 25\n\n");
                        printf("please type two variables, a and b\n\n");
                        scanf("%d %d", &VarA, &VarB);
                        getchar();
                        SuM=VarA+VarB;
                    }
        }

    My code is working just fine if my variables are "greater than> lesser than> equal to" in that order.

    However, if I assign it variables that are first lesser than 25, and then in the next cycle, assign variables whose sum is greater than 25, it fails.
    It also works if my variables are such that the sum is less than 25 and then equal 25 to in that order but if i first give it 10&&15 and later in the next cycle if I give it any two variables whose sum!=25, that's it.. CRASH!!!


    Please Help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    Well if you insist on a bunch of bad ideas....
    Code:
    int main() //!!yes, really, int - for sure.
    {
            printf("please type two variables, a and b\n\n");
            int VarA=0;
            int VarB=0;
            while ( scanf("%d %d", &VarA, &VarB) == 2 ) {
                int SuM=VarA+VarB;
                while (SuM>25)
                    {
                        printf("The sum of numbers is greater than 25\n\n");
                        break;  // make a while into an if
                    }
                while (SuM<25)
                    {
                        printf("The sum of numbers is lesser than 25\n\n");
                        break;  // make a while into an if
                    }
                while (SuM==25)
                    {
                        printf("The sum of numbers is EQUAL to 25\n\n");
                        break;  // make a while into an if
                    }
            }
            return 0;
    }
    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.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Hi, Salem. Thanks for replying. Your Code worked. It worked just the way I wanted. Thanks a lot.

    I was actually thinking of doing the code mentioned at the bottom, when your post came.

    Question: What exactly does this do??
    [Code2]
    while ( scanf("%d %d", &VarA, &VarB) == 2 )
    [/Code2]


    As far as I can understand, its checking if the number of scanned variables is 2.
    Is that right?? or am I wrong(Please correct me if I am)??

    Another thing, The return 0; that you have given, why return that to main?? I can't see any reason why that should be there. If there is a reason, pray tell.
    Generally, I use return when I call a subroutineA and I want to tell the calling function(either main() or some other Subroutine from within which I called the former subroutineA) whether the subroutineA has passed or failed. I will return 1 for failed and 0 for passed or 2,3,4 if there are that many subcases.



    This is the code that I was planning on.
    Code:
    #include<stdio.h>
    
    void main()
        {
            printf("please type two variables, a and b\n\n");
            int VarA=0;
            int VarB=0;
            scanf("%d %d", &VarA, &VarB);
            getchar();
            int SuM=0;
            SuM=VarA+VarB;
            int i=1;
                while(i==1)
                    {        
                        while (SuM>25)
                            {
                                printf("The sum of numbers is greater than 25\n\n");
                                printf("please type two variables, a and b\n\n");
                                scanf("%d %d", &VarA, &VarB);
                                getchar();
                                SuM=VarA+VarB;
                            }
                        while (SuM<25)
                            {
                                printf("The sum of numbers is lesser than 25\n\n");
                                printf("please type two variables, a and b\n\n");
                                scanf("%d %d", &VarA, &VarB);
                                getchar();
                                SuM=VarA+VarB;
                            }
                        while (SuM==25)
                            {
                                printf("The sum of numbers is EQUAL to 25\n\n");
                                printf("please type two variables, a and b\n\n");
                                scanf("%d %d", &VarA, &VarB);
                                getchar();
                                SuM=VarA+VarB;
                            }
                    }
        }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    > As far as I can understand, its checking if the number of scanned variables is 2.
    That is correct.


    > Another thing, The return 0; that you have given, why return that to main?? I can't see any reason why that should be there.
    Because void main programmers have their very own circle of hell.

    The return 0; is there to tell the OS / Shell / calling program that your code was successful.
    It might not seem like it now (and you may not see the reason), but not knowing a reason isn't an excuse for persisting with void main when you've been told otherwise.

    The real reason will become painfully obvious if you get to the point of having to figure out whether a sub-program was successful or not, only to discover that the programmer of said sub-program was some void-main-ing dweeb who just made your life hell. And yes, I am speaking from personal experience.
    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
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Hahaha. Okay. Thanks a lot Salem. You were very helpful

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    7
    I absolutely LOVE 'goto'.
    I personally feel it's one of the best things in programming(I guess that's proof that I'm a newbie). But I've heard from most of the programmers online and even in most books, the use of goto is shunned.
    I've read and heard the same thing about goto. I've read a bit of the source code of a few programs in GNU coreutils and in Suckless and I came across goto statements. So, I think it's not necessarily shunned but it's overuse and unecessary use is. Shunned for beginners as well.

    Also if you're interested and you launch your program from a shell you can check its return status with

    $?
    Last edited by skynet99; 4 Weeks Ago at 05:02 PM.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Thank you, Skynet99. I'll keep that in mind for when I need to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-10-2017, 06:27 AM
  2. Is this loop construct only available using goto's???
    By Muster Mark in forum C Programming
    Replies: 2
    Last Post: 04-01-2011, 03:48 AM
  3. Help with a goto loop
    By Zewu in forum C++ Programming
    Replies: 7
    Last Post: 02-25-2002, 03:08 PM
  4. goto loop;
    By Okiesmokie in forum C++ Programming
    Replies: 8
    Last Post: 01-07-2002, 03:22 PM

Tags for this Thread