...

View Full Version : For Loops & Homework

Lupercal
11-23-2005, 08:14 PM
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;

:rolleyes:

Lupercal
11-23-2005, 08:20 PM
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?

Brandoe85
11-23-2005, 08:42 PM
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.

Good luck;

Spookster
11-23-2005, 10:24 PM
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.

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.