Can i use break and continue at the same time?

This is a discussion on Can i use break and continue at the same time? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main (void) { int num1; while (num1) { printf("Please enter an integer:\n"); scanf("%d", &num1); if((num1>0)&&(num1<=100)) printf("%d\n", num1); ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    11

    Can i use break and continue at the same time?

    Code:
    #include<stdio.h>
    
    int main (void)
    
    {
    
        int num1;
        
    	
      	 while (num1) 
      	 {
    	    printf("Please enter an integer:\n");
    	    scanf("%d", &num1);  
    	   if((num1>0)&&(num1<=100))
    	   	printf("%d\n", num1);
    	   	continue;
    	  	if(num1<0)
    	   	printf("Invalid entry. loop will terminate, Bye!");
    	   	break;
    	  	if(num1==0)
    		break;
    		}   
    	   
    	  
    	return 0;
    }
    There, that is my code, why does whenever I type in a negative value(i.e. -1, -22, -3, -555, etc.) the code still continues to ask to enter an integer and do not breaks, but when i enter 0 it breaks? Help what is wrong with my codes?. Thanks for help.

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    You're missing a lot of brackets here...what is it you're trying to do? Loop until the user enters a positive integer?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You wouldnt have this problem if you used braces on every block of code (ifs, loops). Tracing your code, if the number you enter in is between 0 and 100 then it prints the number if so. The next statement executed (whether the if is true or false), is "continue"... so no other statements in the loop are executed, as it always "continues" and skips to the next iteration here. If you want to print the number and continue on the condition then you need braces, like
    Code:
    	   if((num1>0)&&(num1<=100))
           {
    	   	printf("%d\n", num1);
    	   	continue;
           }
    Once you fix that youll realize the other if statement has a similar problem.

    Also
    Code:
        int num1;
        
    	
      	 while (num1)
    Your using 'num1' without initializing it. Your compiler is probably giving you this warning. Because of this, you should probably use a 'do-while' loop instead, or simply initialize it before the while loop.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    what I should do is to continually ask for an integer if a number greater than 0 is printed if 100<=x>0 the number will be printed then asks for an integer again, if its greater than 100 it will just ask for an integer. If the user enters 0 it will break, if one enters a negative value then it will print "Invalid entry. loop will terminate, Bye!" and breaks. BTW I would like to thank you in my first post for answering my concerns and being such a great help. Wah. What is wrong with my brackets?(Judging from my name it is really clear that I really have a lot to learn).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    You seriously need to fix that indentation. Do that first.
    And yes, you can use break and continue together.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. continue and break!
    By Nectron in forum C++ Programming
    Replies: 4
    Last Post: 07-04-2003, 09:01 PM

Tags for this Thread


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