GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Aug-2005, 05:27
zodisign zodisign is offline
New Member
 
Join Date: Aug 2005
Posts: 8
zodisign is on a distinguished road

please help me convert this code to while loop


i have a code for multiplication table using for loops but i don't know how to convert it to while loops.please help me convert it to while loop statement.thank you.
CPP / C++ / C Code:
void main()
	 {
		  int r,c,number,size;
		  printf("Enter a number:");
		  scanf("%d",&size);

		  for (c = 0; c <= size; c++)
				printf("%3d", c);
		  printf("\n");
		  for (r = 1; r <= size; r++) {
				printf("%3d", r);
				for (c = 1; c <= size; c++)
					 printf("%3d", r*c);
				printf("\n");
		  }
	 }
Last edited by LuciWiz : 15-Aug-2005 at 03:32. Reason: edited title
  #2  
Old 14-Aug-2005, 06:38
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by zodisign
... but i don't know how to convert it to while loops...

CPP / C++ / C Code:
  for (c = 1; c <= size; c++)
    printf("%3d", r*c);
  
  printf("\n");

In order to use this loop as a conditional while statement you need to,
  • Set your pre loop condition ( c = 1 )
  • Give your while a continue or stop condition ( c <= size )
  • Prepare for the next loop stop/continue ( c++ )

So the loops you want to create will look something like,
CPP / C++ / C Code:
// you got your size variable
// set your pre condition here
while ( c <= size ) {
  // do what you need to do here
  // prepare for the next loop here
}
// continue on with the program

For the sake of arguement, lets say that the size was 2. When you get to the while... line it asks,
Is c less than or equal to size? If yes, enter the loop. If no, skip it by and get on with the program. When inside the loop, you do what you wanted to do, in your case print out some information. Than you increase your variable that you are checking against size. Lather, Rinse, Repeat...

That should give you an idea of how the construct works. HTH

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guidelines for posting requests for help - UPDATED! WaltP C++ Forum 1 22-Aug-2008 00:07
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 21:53
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 20:56
[Header]Classifying the FOR loop mithunjacob C++ Forum 0 20-Jun-2004 16:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.