Executing For Loop

This is a discussion on Executing For Loop within the C Programming forums, part of the General Programming Boards category; Code: // program asks user to enter a number //the program then prints how many digits user entered. //code works. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Executing For Loop

    Code:
    // program asks user to enter a number
    //the program then prints how many digits user entered.
    //code works.
    //what I don't understand is how the for loop works.
    //can someone explain each step the for loop takes up until it prints out the answer. thx
    
    
    #include <stdio.h>
    int main(void)
    
    {
    
     int n,i,x;
      
     printf("Please enter a positive integer: ");
     scanf("%d",&n);
     x=n;   //dummy variable
     
            for ( i=0; n>0; i++) //for loop    
                 {  n=n/10;  } 	//reduces the number "n"
     			  
     	 printf("Number %d is %d digits long.\n",x,i);
     			  
     return(0);
     }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    BE the loop... Sit down with a pencil and paper, write down the value of each variable for each time through the loop... exit when the exit condition is met...

    You'll figure it out...

    (And yes, that's how the big guns do it.)

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    If i enter number 17.
    n=17/10=1.7 which satisfies the for loop
    so therefore (i=1; n>0;i++)
    does the for loop execute again? or does it stop there.
    because int n is declared as an integer does the answer 1.7 round to 2? or does it round down to 1?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mshel130 View Post
    If i enter number 17.
    n=17/10=1.7 which satisfies the for loop
    so therefore (i=1; n>0;i++)
    does the for loop execute again? or does it stop there.
    because int n is declared as an integer does the answer 1.7 round to 2? or does it round down to 1?
    You have integer division. So what you actually have is:

    n = 17
    n / 10 = 1

    Integer division does not round, it truncates.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Put a printf statement in the loop that outputs the values of n and i. Then you can see what those values are and how they change each iteration of the loop. That should help you understand what is going on.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Executing an ELF
    By abachler in forum Linux Programming
    Replies: 3
    Last Post: 01-20-2009, 02:14 PM
  2. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  3. execl not executing?
    By talz13 in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2004, 08:08 PM
  4. Executing in DOS...
    By Jperensky in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 02:33 AM
  5. executing
    By xlordt in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 07:56 AM

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