Quick question

This is a discussion on Quick question within the C++ Programming forums, part of the General Programming Boards category; i just started learning c++ and i was reading the tutorials and im on loops and and there was an ...

  1. #1
    Unseen
    Join Date
    Apr 2005
    Location
    ohio
    Posts
    7

    Quick question

    i just started learning c++ and i was reading the tutorials and im on loops and and there was an example of loops the second yellow box and i dont understand what its saying...i dont want to move on until i do so if someone could explain it a little better than the tutorial that would be great im only 15 so go easy on me. im sure this is really simple im just slow thanks

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'll assume you're referring to the example of a for loop, but in future, post the code you're referring to, and you'll definitely get a better response with more specific questions.

    Loops are just tools to make it easier on you when you have a bunch of very similar tasks that need to be repeated over. Like if you have a list of numbers, and you need to add 1 to each number - you would use a loop.

    For loops are structured like this:

    Code:
    for (starting values; test conditions; increments)
    {
       // Code
    }
    The most important part of the loop is the first line - the word 'for' followed by a set of parentheses. Inside the parentheses are 3 sections, separated by semicolons (as shown).

    The first section is your starting values. In most loops, you will need a counter or some number to help you control the loop (keep track of how many time the loop has cycled, etc...). In this sections you can declare new variable, assign values to variables, etc... If you need to initialize mroe than one variable, just put your statements between commas, like this:
    Code:
    for(int i = 0, j = 9; ...
    In the second section you have your test conditions. You need to tell your loop when to stop repeating the action you tell it to do. Let's say that the variable we declared earlier, ' i , (i is commonly used as a counter in loops), is being used as a counter. We want the loop to execute 10 times, so we set i equal to 0 at the start, and tell the loop to keep executing as long as i is less than 10. In your second section, put all the conditions (separated) by commas that must be true for the loop to keep executing. So in our example, i must be less than 10.

    Code:
    for ( int i = 0, j = 9; i < 10; ...
    Now if you're counting the number of times the loop has executed, you want the counter variable to go up by one each time. In the third section, you can list all the changes you want made to variables. I don't know if you've learned this yet, but in C++, since adding 1 to a number is a very common thing, you can just write "i++" and i will go up by one.

    Code:
    for ( int i = 0, j= 9; i < 10; i++)
    The variable j really has no purpose in our loop, it's just there to demonstrate the use of commas. If your loop is just repeating one line of code, you don't need to use braces, but if it's a section of code, you need to separate it from the rest of your program.

    Code:
    for ( int i = 0, j= 9; i < 10; i++)
    {
    	// Put your code here
    }
    When you learn about arrays, loops will become very useful to you. If needed, you can use the i and j variable inside your code. One final piece of advice: make sure you DO NOT put a semicolon between your closing parenthesis and your opening brace. That's a common mistake, and it will cause your loop do to pretty much nothing.

    I hope that helps.

  3. #3
    Unseen
    Join Date
    Apr 2005
    Location
    ohio
    Posts
    7
    yeh it helped thanks alot...way more explantion than the tutorial gave me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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