C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2010, 10:50 AM   #1
Registered User
 
Join Date: Mar 2010
Posts: 17
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;
}
aadil7 is offline   Reply With Quote
Old 03-14-2010, 11:13 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 03-14-2010, 01:36 PM   #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.
__________________
Macselent Corp

Last edited by UltraKing227; 03-14-2010 at 01:39 PM.
UltraKing227 is offline   Reply With Quote
Old 03-14-2010, 01:42 PM   #4
Registered User
 
Join Date: Mar 2010
Posts: 17
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
aadil7 is offline   Reply With Quote
Old 03-14-2010, 01:52 PM   #5
Registered User
 
Join Date: Mar 2010
Posts: 17
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;
}
aadil7 is offline   Reply With Quote
Old 03-14-2010, 01:55 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 10,163
A do-while loop always executes at least once, since the condition is not checked until the end of the loop.
tabstop is offline   Reply With Quote
Old 03-14-2010, 05:11 PM   #7
Registered User
 
Join Date: Mar 2010
Posts: 17
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?
aadil7 is offline   Reply With Quote
Old 03-14-2010, 05:20 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 10,163
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.
tabstop is offline   Reply With Quote
Old 03-15-2010, 01:31 AM   #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.
__________________
Macselent Corp
UltraKing227 is offline   Reply With Quote
Old 03-15-2010, 01:41 AM   #10
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,556
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?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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