Thread: Learning loops

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Learning loops

    Hello. In my C++ class we are now in the chapter of loops and I have began to start getting lost. I have been doing fine until now, and now starting to get worried.

    Last chapter we had to write a program that ask the user to enter a number and told them if it was even or odd. This chapter the instructor has told us to modify the program and use a while loop so that the program loops a total of 3 times.

    The program is here
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      
      int number;
      cout << "Enter an integer: ";
      cin >> number;
    
      if (number % 2 == 0)
        cout << number << " is even." << endl;
    
      if (number % 2 != 0)
        cout << number << " is odd." << endl;
      
     
      return 0;
    }
    I am very confused on how to use a loop. We went over the formats of the while loop and what they do but never used one in a actually program. I'm really even sure where to put the loop. Do I put it underneath the if statements or erase the if statements and use the loop here.

    I know the format of a while loop is:
    while (expression)
    {
    statement;
    }

    However, I am not sure how to fully use it. If someone could explain or show me how to use a while loop for this program so that it loops 3 times, I would greatly appreciate it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, let's try something first. Do you have a plan on how to accomplish this task? That is, do you have a list of steps in mind to achieve this?
    Such as, enter a number, see if number is even, etc.
    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
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    getting it to use loop 3 times is no problem...but I can't see why you would want to
    unless your teacher wants you to get 3 separate inputs from the user.
    look at your book..look at the examples. And follow the steps, hopefully it should help.
    You ended that sentence with a preposition...Bastard!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is an exercise of creating proper loops. That is all.
    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
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by Elysia View Post
    Well, let's try something first. Do you have a plan on how to accomplish this task? That is, do you have a list of steps in mind to achieve this?
    Such as, enter a number, see if number is even, etc.
    My problem is I don't even know where to begin. In class we have learned how to use a loop but we never actually looked at one in a program like this

    The instructor used simple simple programs to demonstrate the use of a while loop.

    For instance a program like this.

    Code:
    int number = 1;
    
    while (number <= 5)
    
    {
    cout << "Hello." << endl;
    number++;
    }
    Just to show how it would print out Hello 5 times, by keep adding one every time it was repeated. However, I don't know how I would take that even/odd program and make it loop 3 times or even loop at all for that matter.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As always, before beginning with anything, have a plan in mind.
    So, to accomplish something like looping 3 times doing something, how would you do it? Not in code, but in logical thinking.
    That is usually a good exercise.

    Try formulating logic for your example, as well. Maybe it will all click. I believe you need a simple push in the right direction.
    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.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    the same concept man..
    while(number<=input )
    {
    if(..)
    else if..
    number++
    }
    You ended that sentence with a preposition...Bastard!

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by Elysia View Post
    As always, before beginning with anything, have a plan in mind.
    So, to accomplish something like looping 3 times doing something, how would you do it? Not in code, but in logical thinking.
    That is usually a good exercise.

    Try formulating logic for your example, as well. Maybe it will all click. I believe you need a simple push in the right direction.
    Well for the the even/odd program I assume I must need a loop this is like:
    Code:
    something = 0;
    while (something < 3)
    {
    something++;
    }
    So that it runs, then adds one, runs adds one, then stops for a total of 3 times. I just cant seem to wrap my mind around how I would implement this into the program.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well then, how about you actually formulating the logic required for your program, like I suggested?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and loops
    By pmooney12 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 10:38 PM
  2. problem with while loops
    By MyntiFresh in forum C++ Programming
    Replies: 35
    Last Post: 07-09-2005, 03:36 PM
  3. College and Learning
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 11-15-2004, 07:48 PM
  4. learning to code
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-30-2001, 08:49 AM
  5. Looking for good beginner tutorials in learning windows api
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 10-30-2001, 06:42 AM