Thread: Loop statements

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Loop statements

    How can i repeat a loop based on the number inputted by the user?
    Anyone help me!!!

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by viira View Post
    How can i repeat a loop based on the number inputted by the user?
    Anyone help me!!!
    Of course yes, did you try it?Could you show us what you tried?

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    Code:
            int num=0;
    	int value=0;
    
    
    	    printf("HOW MANY INTEGER VALUES WOULD YOU LIKE INPUT?");
    	    scanf_s("%d",&num);
    	do 
    	{
    		printf("Value:");
    		scanf_s("%d",&value);
    		
    	}while(value!=num);
    
    /*this is what i did but this is not right, supposedly the output must be like this:
    How many integer values would you like to input? 5   <-------then the range of value i need to input will be only five
    value:5
    value:8
    value:4
    value:9
    value:2
    (and must automatically terminate)
    
    it's a bit confusing but i wonder you could help, please =( */

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    What you have done is say, "loop until the input number (value) does not equal the first number (num)".

    Why not decrement the value of "num" every loop and loop until it equals 0?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,512
    I'd actually recommend a "for()" loop for this purpose, since it is well suited for what you're trying to do (variable update and conditional check "built in"). However, a "while()" is also perfectly acceptable, if used correctly.

    For, While and Do While Loops in C - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking out of a While loop with Nested If statements
    By tmac619619 in forum C Programming
    Replies: 4
    Last Post: 03-14-2014, 01:34 PM
  2. For Loop Skipping Statements.
    By rswest in forum C Programming
    Replies: 3
    Last Post: 01-27-2013, 07:12 AM
  3. HW Help - Switch and Loop statements
    By Pl2lNCE in forum C Programming
    Replies: 1
    Last Post: 01-22-2013, 04:16 PM
  4. Execution of statements in a loop condition
    By 127.0.0.1 in forum C Programming
    Replies: 4
    Last Post: 05-10-2011, 06:12 AM
  5. Loop and Case statements
    By Nexcompac in forum C Programming
    Replies: 6
    Last Post: 06-26-2008, 04:11 AM

Tags for this Thread