Loop

This is a discussion on Loop within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> main() { int menu; printf("1.Test1\n"); printf("\n2.Test2\n"); printf("\n3.Exit\n"); printf("\nEnter Selection: "); scanf("%d", &menu); if(menu == 1) ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    Loop

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
    	int menu;
    	printf("1.Test1\n");
    	printf("\n2.Test2\n");
    	printf("\n3.Exit\n");
    	printf("\nEnter Selection: ");
    
    	scanf("%d", &menu);
    
    	if(menu == 1)
    	{
    		system("cls");
    		printf("Test1\n");
    	}
    	else
    		if(menu == 2)
    		{
    			system("cls");
    			printf("Test2\n");
    		}
    		else
    			if(menu == 3)
    				exit(0);
    			
    }
    Hi all
    i want to make a loop for my program.
    i want it keep looping until the user key in 3.
    how can i make that loop ?

    thx in advance

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    using a while loop.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
       int menu;
       printf("1.Test1\n");
       printf("\n2.Test2\n");
       printf("\n3.Exit\n");
       printf("\nEnter Selection: ");
    
       scanf("%d", &menu);
    
       while (menu!=3)
       {
           switch(menu)
           {
               case 1:
                  system("cls");
                  printf("Test1\n");
                  break;
               case 2:
                  system("cls");
                  printf("Test2\n");
                  break;
            }
             scanf("%d", &menu);	
        }
    			
       return 0;
    }
    Last edited by Quantum1024; 05-11-2005 at 10:03 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    Are there any other method beside using while loop ??


    thx in advance

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    a do-while loop ;p

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are three types of loops:
    Code:
    while( condition )
    {
        do all of this
    }
    This has the potential to not ever be executed, if the condition evaluates to false initially.

    Code:
    do
    {
        do all of this
    } while( condition );
    This loop will always execute the contents of the body of this loop at least once, even if condition evaluates false when it reaches there.

    Code:
    for( initializers; condition; usually-[ de ][ in ]crementing )
    {
        do all of this
    }
    This loop could also potentially never execute the body of the loop. In this loop, any or all of the elements of this loop may be left out. ( Meaning, you can not use the initialization portion, the check, or any incrementing / decrementing. They're all optional. )

    These are the three standard methods of looping.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    but goto's are the best


























    (jk)

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    ok. i get it.
    Thanks a lot quzah.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sand_man
    but goto's are the best
    That's why I said these are the three standard methods of looping.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    *phew*
    I thought I was going to get flamed for that drunken post

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-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21