Thread: why if with break skips false vals here?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    why if with break skips false vals here?

    Hi, guys, I have been working on this short program, it is a simple conversion program from feet/inches to metric system. Everything seems fine, apart from one if statement. There are two, the same if blocks, the only difference is the values in the condition and the sign. Why the first if skips the whole 1-11 inches and then the table prints startign from 5 feet, instead of 4'6"???
    Code:
    #define FEET_TO_CM 30.48
    #define INCH_TO_CM 2.54
    
    float conversion(int, int);
    
    
    main()
    	{
    	int feet, inch;
    	float converted = 0;
    
    
    	for( feet = 4; feet <= 6; feet++ )
    		{ 
    
    		for( inch = 0; inch <=11; inch++ )
    			{
    /* that's the two ifs that I am talking about */
    
    			if( feet == 4 && inch <= 5 )
    				break;
    				
    			
    			if( feet == 6 && inch >= 6 )
    				break;
    				
    			converted = conversion(feet, inch);
    
    			printf("%2d\'%3d\"  %7.2fcm\n", 
                                                            feet, inch, converted);
    
    			}
    		}
    
    	return 0;
    	}
    
    
    float conversion( int feet, int inch )
    	{
    
    	return (FEET_TO_CM * feet + INCH_TO_CM * inch);
    
    	}
    Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're looking to use continue; instead of break;. break stops the loop and continue just restarts the loop at the next iteration.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks! I knew it was something so simple...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  2. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  5. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM