Thread: Difference between for and while loop

  1. #1
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16

    Difference between for and while loop

    Hi,

    Can anybody please explain the Difference between for and while loop?

    Thanks
    Ankit

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    for (a; b; c) x;
    <===========>
    a;
    while (b)
    {
        x;
        c;
    }
    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.

  3. #3
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Quote Originally Posted by Elysia View Post
    Code:
    for (a; b; c) x;
    <===========>
    a;
    while (b)
    {
        x;
        c;
    }
    Hi,

    is there any technical difference apart from the structure of the two loops as i can guess that the condition is the same, though iteration is a part of the loop body in while loop whereas the declaration/initialization is done before the while loop.

    Thanks

    Ankit

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are only syntactically different.
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    They're not entirely interchangeable.

    In a for loop, 'b' can be empty whereas a while loop must always have an expression.

    The continue keyword works differently as well, where 'c' is evaluated in the for loop, but skipped in the while loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Quote Originally Posted by Salem View Post
    They're not entirely interchangeable.

    In a for loop, 'b' can be empty whereas a while loop must always have an expression.

    The continue keyword works differently as well, where 'c' is evaluated in the for loop, but skipped in the while loop.
    Hi,


    Thanks a lot for the information.

    where would one like to use a while loop instead of the for loop.

    thanks
    ankit

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by ankiit View Post
    Hi,


    Thanks a lot for the information.

    where would one like to use a while loop instead of the for loop.

    thanks
    ankit
    Where it would be logically simpler to structure some code like that...
    For example, when you want to run the loop as long as some condition (or a boolean value) is true, but has no counting (like from 0 to 100) going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. Replies: 3
    Last Post: 03-14-2006, 11:09 AM
  5. Replies: 1
    Last Post: 01-10-2003, 10:08 PM