Thread: beginner help loops

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    Thumbs up beginner help loops

    hi
    well ive only just started c programming and i thought the best place to start was hello world so i just thought i would build on that but i seem to have hit a brick wall and i wondered if anyone could help

    heres how far ive got so far and basically im getting stuck on if the user enters 0
    i want the program to loop around back to the start but i have no clue on how to do that!
    ive tried a few different loops and also tried using it in the IF statements, but no give
    when i execute this code and enter 0 it continually repeats "x (0) is null please try again" and does not loop back, instead i have to stop the whole program, i guess it kinda like crashes

    thanks loads (in advance)

    Code:
    #include <stdio.h>
    #include "simpleio.h"
    
    int main()
    {
    	int x, i;
    	printf("Hello World \n");
    	printf("Enter an integer \n");
    	x = getInt();
    	printf("Value of X = %d \n",x);
    	printf("x is stored at: %p\n",&x);
    	
    	for(i=0;x==0;i++)
    	{
    		printf("x (%d) is null please try again\n",x); i++;
    	}
    
    	if (x>1 && x<10)
    	{
    		printf("x (%d) is less than 10\n",x);
    	}
    	
    	else
    	{
    		printf("x (%d) is more than 10\n",x);
    	}
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    declare stuff
    for( input = getinput(); input != 0; input = getinput() )
    {
        ...stuff to repeat...
    }
    Or...
    Code:
    declare stuff
    do
    {
        ...stuff to repeat...
        input = getinput();
    } while( input != 0 );
    Or...
    Code:
    declare stuff
    while( input != 0 )
    {
        ...stuff to repeat...
        input = getinput();
    }

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

  3. #3
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    some description of the most common loops:


    FOR loop:

    a FOR loop is used when you want to go from, lets say, number one
    to ten. thus looping ten times, but it cant be used to see if number one
    is equal to zero or not.

    example:

    Code:
    int i;
    for (i = 0; i < 10; i++)
    {
     printf("Hello, World!\n");
    }
    WHILE loop:

    a common loop used for checking stuff, like if number one has been pressed
    or not. its also the best to use for your problem. example:

    Code:
    int input;
    while(input != 0)
    {
     input = getInt();
    }
    -------
    Some final notes:
    you can exit a loop by the break; command. example:

    Code:
     int i;
    for (i = 0; i < 10; i++)
    {
     printf("Hello, World!\n");
     if (i == 5)
     {
     break;
     }
    }
    if you run the code, you will notice that it doesnt print hello,world ten
    times anymore. i hoped this helped, if it didnt, feel free to ask.
    Last edited by UltraKing227; 03-14-2010 at 01:39 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by UltraKing227 View Post
    some description of the most common loops:


    FOR loop:

    a FOR loop is used when you want to go from, lets say, number one
    to ten. thus looping ten times, but it cant be used to see if number one
    is equal to zero or not.

    example:

    Code:
    int i;
    for (i = 0; i < 10; i++)
    {
     printf("Hello, World!\n");
    }
    WHILE loop:

    a common loop used for checking stuff, like if number one has been pressed
    or not. its also the best to use for your problem. example:

    Code:
    int input;
    while(input != 0)
    {
     input = getInt();
    }
    -------
    Some final notes:
    you can exit a loop by the break; command. example:

    Code:
     int i;
    for (i = 0; i < 10; i++)
    {
     printf("Hello, World!\n");
     if (i == 5)
     {
     break;
     }
    }
    if you run the code, you will notice that it doesnt print hello,world ten
    times anymore. i hoped this helped, if it didnt, feel free to ask.
    thanks for the reply guys

    how do you suggest i do this then because if the user enters '0' i want the program to loop to the start so the user starts from the start

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok i seem to have got around that issue

    but i am getting another error now
    when i enter a number which is less than 10 it executes the if statement but also loops around
    i do not know how this happened :S

    Code:
    #include <stdio.h>
    #include "simpleio.h"
    
    int main()
    {
    	int x, i;
    	printf("Hello World \n");
    	printf("Enter an integer \n");
    	x = getInt();
    	printf("Value of X = %d \n",x);
    	printf("x is stored at: %p\n",&x);
    	
    	do
    	{
        	printf("Hello World \n");
    	printf("Enter an integer \n");
    	x = getInt();
    	printf("Value of X = %d \n",x);
    	printf("x is stored at: %p\n",&x);
    	} 
    	while( x == 0 );
    
    	if (x>=1 && x<=10)
    	{
    		printf("x (%d) is less than 10\n",x);
    	}
    	
    	else
    	{
    		printf("x (%d) is more than 10\n",x);
    	}
    	return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A do-while loop always executes at least once, since the condition is not checked until the end of the loop.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by tabstop View Post
    A do-while loop always executes at least once, since the condition is not checked until the end of the loop.
    umm sorry that i dont know but how do i check the condition?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by aadil7 View Post
    umm sorry that i dont know but how do i check the condition?
    You enclose it in parentheses after the word "while", as in say
    Code:
    while (x == 0)
    You may be surprised at how much better your program runs if you delete lines seven through eleven.

  9. #9
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    or, you may do it like this:

    Code:
    if (x == 0)
    { 
     	do
    	{
        	printf("Hello World \n");
    	printf("Enter an integer \n");
    	x = getInt();
    	printf("Value of X = %d \n",x);
    	printf("x is stored at: %p\n",&x);
    	} 
    	while( x == 0 );
    }
    the above code checks if X is zero or not, if it is then it starts a loop.
    otherwise, it just goes ahead. i hope this helped.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by UltraKing227 View Post
    or, you may do it like this:

    Code:
    if (x == 0)
    { 
     	do
    	{
    	} 
    	while( x == 0 );
    }
    how is it better when just using while loop?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True Beginner - Pointer/Array For loops
    By KyussRyn in forum C Programming
    Replies: 7
    Last Post: 03-04-2009, 03:53 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM