Thread: C programming (for loops)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    C programming (for loops)

    Code:
    #include<stdio.h>
    int main(){
    int i=1;
    for(i=0;i=-1;i=1) {
             printf("%d ",i);
    if(i!=1) break;
        }
    return 0;
    }
    


    The output for this is: -1. Can some explain to me why?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    for(i=0;i=-1;i=1) {
    = is for assignment, == is for comparison. Now, think about the anatomy of a for loop:
    Code:
    for (initial; loop condition; loop increment)
    Every time through the loop, you have to check (execute) the loop condition to see whether you should keep looping or stop.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. christmas tree in c programming using loops.
    By poojaraghoo in forum C Programming
    Replies: 3
    Last Post: 10-09-2011, 02:29 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. C++ Programming Quiz - Loops
    By Sirion in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2010, 04:07 PM
  4. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  5. help with loops
    By lpaolini436 in forum C Programming
    Replies: 8
    Last Post: 11-10-2006, 01:51 PM

Tags for this Thread