Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For Loops & Homework

    I'm a first year university student who is taking a c++ programming class. In all honesty, I have no idea what I am doing, but I'm not here to ask you to tell me what to do.

    We had the ever popular palindrome assignment given to us the other day, and I find myself stuck with truely understanding for loops, and how to use if statements without 'else'. I've been flagging in my program to see where I've gone wrong, however, I seem to think it's in my for loop that all hell has broken loose.

    Here is the part where I seem to be stuck, and was wondering if anyone could explain to me (in general terms) the meaning behind the for loop. From my knowledge, my 'i' represents the first char in the string, while 'n' represents the last? Is this correct..?

    for (int i = 0; i <n; i++)
    {
    if (s[i] != s[n-1]) //&& (s[i+1] != s[n-1-i]))
    {
    pal = false;

    }

    if ((s[i] == s[n-1]) && (s[i-1] == s[n-1-i]))
    {
    pal = true;
    }
    }

    return pal;

  • #2
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah! I found my problem. The program works, but I was still wondering if anyone could possibly send me a good tutorial to explanation of for loops, or explain them better?

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I'm assuming n is being set to the length of your string, and as you see you're initializing i to 0. So you're checking the first character with the last character, and the second character to the second to last character and so forth, until you get to the end of the string.

    I would say post any specific question you have about loops, in general it's a pretty broad topic. I did do a google on c++ loops as you might find something that will help you along.
    http://www.google.com/search?hl=en&q...=Google+Search

    Good luck;

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well in general a loop is a way of executing lines of code contained within repeatedly. A for loop is a count controlled loop. That means it executes a specific number of times. The for loop takes 3 parameters. The starting value, the ending value and the amount to increment.

    Code:
    int i,
    
    for (i = 1; i <= 5; i++) {
         printf("i = %i\n",i);
    }
    So what we have inside the loop statement is i which is set to 1 initially. After the first iteration of the loop it doesn't look at that first parameter again. After that it continues to look at the second two parameters. So with each iteration of the loop it compares i to see if it is less than or equal to 5. If that is true it continues to loop. The i++ is the amount we increment. With each iteration of the loop we add 1 to i. Remember that i++ is just a shortcut way of writing i = i +1; You could have also put i+=2 which increments by 2 each time. It is a shortcut way of writing i = i+2;

    So back to the example with each iteration of this loop i gets incremented by 1. And with each iteration of the loop i is compared to see if it is less than or equal to 5. Once i increments to 6 the loop will stop because i is no longer less than or equal to 5. Thus the loop will execute whatever is inside of it 5 times since we start at 1 and stop at 5.
    Last edited by Spookster; 11-23-2005 at 10:28 PM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •