Like Tree4Likes
  • 1 Post By Elysia
  • 3 Post By Salem

Difference between for and while loop

This is a discussion on Difference between for and while loop within the C++ Programming forums, part of the General Programming Boards category; Hi, Can anybody please explain the Difference between for and while loop? Thanks Ankit...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Code:
    for (a; b; c) x;
    <===========>
    a;
    while (b)
    {
        x;
        c;
    }
    Elkvis likes this.
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    iMalc, laserlight and ankiit like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 06: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, 10:09 AM
  5. Replies: 1
    Last Post: 01-10-2003, 09:08 PM

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