Thread: For Loop to While Loop

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    For Loop to While Loop

    This is the code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    for(int a=1; a<=10; a++)
    {
    for(int b=1; b<=a; b++)
    cout<<"*";
    cout<<"\n";
    }
    }
    The code is actually done, cause we are really required to use "for" but I just want to see it done with "while", cause I prefer "while" rather than "for".

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Whilst you can (almost) mung any kind of loop into any other kind of loop, why not just learn to use the most appropriate kind of loop for the task at hand?

    Sure, you can write
    for ( a ; b ; c ) d;
    as
    a; while ( b ) { d ; c };

    That is, until you try to add break; and continue; statements, and then it gets a hell of a lot harder for no good reason.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Noted. But can you do it in while loop?

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int a=0;
    int b;
    while(a++<10)
    {
    b=1;
    while(b++<=a)
    cout<<"*";
    cout<<"\n";
    }
    return 0; //should use return explicitly
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I believe I just showed you what to do...
    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
    Join Date
    Jul 2008
    Posts
    91
    @herWter: Thank you
    @Salem: Sorry, but its kind of vague,but I can see your point now. Thanks

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Restating what Salem said, because we're just not going to do your homework for you...

    A for loop works as follows:

    Code:
    for (set initial condition; set loop-terminating condition; end-of-loop operation)
    {
        do work;
    }
    Breaks down rather simply into a while loop:

    Code:
    set initial condition;
    while (loop-terminating condition is not met)
    {
        do work;
        do end-of-loop operation;
    }
    Some example loops:
    Code:
    // Infinite for loop
    for(;;) {}
    
    // Infinite while loop
    while(1) {}
    
    // Infinite do loop
    do {} while (1);
    
    // Unintentionally infinite while loop
    const char * const p = "Please remember to update with respect to your loop-terminating condition";
    const char *q = p;
    while (*q != '\0')
    {
        std::cout << *q;
    }
    
    // Above, fixed one way
    const char * const p = "Please remember to update with respect to your loop-terminating condition";
    const char *q = p;
    while (*q != '\0')
    {
        std::cout << *q;
        ++q;
    }
    
    // And a different way
    const char * const p = "Please remember to update with respect to your loop-terminating condition";
    const char *q = p;
    while (*q != '\0')
    {
        std::cout << *q++;
    }
    
    // And another, different way
    const char * const p = "Please remember to update with respect to your loop-terminating condition";
    const char *q = p;
    while (*q++ != '\0')
    {
        std::cout << *q;
    }

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by freddyvorhees View Post
    @herWter: Thank you
    @Salem: Sorry, but its kind of vague,but I can see your point now. Thanks
    Sigh...another enabler.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Thanks for the help, but what I asked is not really our homework. This is what were supposed to do
    Code:
    ICST102 : Programming I
    ---------------------------------------------------------------
    GRADED PROGRAMMING EXERCISE 4 
    TITLE : Control Structures
    DATE  : 5 August 2008
    ---------------------------------------------------------------
    Perfect score: 100 pts
    25 pts each
    ---------------------------------------------------------------
    Submission instructions: Upload all your solutions (main.cpp)
    in the designated directory in your webdrive account.
    
    Point your web browser (IE or Firefox) to the following URL:
    
              http://csr.adnu.edu.ph/webdrive/
    
    For this exercise, upload your main.cpp files in the correct
    subdirectory or subfolder inside folder LAB04.
    
    ---------------------------------------------------------------
    Programming Problems
    ---------------------------------------------------------------
    
    Project Name : Prob4.13         
    C++ File     : main.cpp
    p. 173, #4.13
    
    Project Name : Prob4.14
    C++ File     : main.cpp
    p. 173, #4.14
    
    Project Name : Prob5.8
    C++ File     : main.cpp
    p. 230, #5.8
    
    Project Name : Prob5.12
    C++ File     : main.cpp
    p. 230, #5.12
    
    5.12 Write a program that uses for statements to print the following patterns separately, one below the other. 
    Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form 
    cout << '*'; (this causes the asterisks to print side by side). [Hint: The last two patterns require that each 
    line begin with an appropriate number of blanks. Extra credit: Combine your code from the four separate 
    problems into a single program that prints all four patterns side by side by making clever use of nested for loops.
    I seems that when I try to use "while" Im misplacing the braces.

    I just want to learn more.
    I hope I still have your trust.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM