A little confused with while and for loops
by jsbjsb001 from LinuxQuestions.org on (#4WX0Q)
So, basically I've written a couple of little programs to compare two hardcoded strings in two separate arrays. One version of it using a while loop, which works as expected and prints the right message - being either the strings match or they don't. I've also tried to do the same thing using a for loop, and while it does work; basically I'm a little confused about how it's working... basically I only want the message that says whether or not the strings are equal or not to be printed once - after the loop has finished comparing the arrays. Rather than each time it compares a character, while I've finally managed to get it to do that after much rooting around; I'm little confused as to how the for loop is working compared to the while loop.
So basically my questions are: How does it "remember" that each array element matched the other array's elements when the if statement to print the appropriate message is outside of the loops (both for and the while loops - I did that because I only wanted the message to be printed once, not on each cycle through the loops) ? Also, why does it work without the curly braces for the while loop, but it doesn't seem to work properly without the curly braces for the for loop, even if there's nothing inside it's curly braces ? I'm at least a little baffled here.
And while I don't mean to be rude; please just respond to the questions I've asked above rather than suggesting any "better ways" of comparing strings. I realize there is likely far better ways (like using pointers), but I'm just trying to better understand how the while and particularly the for loop work. So I can get a clear picture, so I CAN use them with things like pointers without constantly getting confused as to what exactly they are actually doing. I plan on doing some "pointer practice" later on, and I'll start a separate thread about that if need be. So I kindly ask that we just focus our attentions on the while and for loops for now, because I've found it's much easier to just focus on one thing at a time (in this case yes, the while loop isn't a for loop, I know, but they are still both loops).
To be clear: if people want to offer suggestions for "doing it better" without throwing in concepts I've not employed already, and without saying "just use such and such function in the C library", then feel free to. I just ask people to bear in mind that the whole point of me doing this is to try and better understand how loops work, **not** to write the absolute best string comparing program you'll be ever likely to come across. I'll also kindly ask that any responses be absolutely clear - even if you have to treat me like a complete idiot. I'll forgive you ;)
Here's the "while loop" version:
Code:#include <stdio.h>
int main(void) {
int i = 0;
const char arrayCompareWith[] = "test";
const char arrayCompare[] = "test";
while ( arrayCompareWith[i] == arrayCompare[i] && arrayCompareWith[i] != '\0' && arrayCompare[i] != '\0' ) //{
++i;
// printf("i = %d arrayCompareWith[i] = %c arrayCompare[i] = %c ? %s\n", i, arrayCompareWith[i], arrayCompare[i], arrayCompareWith[i] == arrayCompare[i] ? "True" : "False");
// }
if ( arrayCompareWith[i] == arrayCompare[i] ) {
puts("Strings equal");
}
else {
puts("Strings not equal");
}
return 0;
}Here's the "for loop" version:
Code:#include <stdio.h>
#include <stdbool.h>
int main(void) {
const char string1[] = "Hello";
const char string2[] = "Hello";
// bool strEqual;
int i;
for( i = 0; string1[i] != '\0' && string2[i] != '\0'; ++i ) {
// printf("i = %d string1[i] = %c string2[i] = %c ? %s\n", i, string1[i], string2[i], string1[i] == string2[i] ? "True" : "False");
// if ( string1[i] == string2[i] ) {
// strEqual = true;
// }
// else {
// strEqual = false;
// break;
// }
}
// if (strEqual) {
if ( string1[i] == string2[i] ) {
puts("Strings equal");
}
else {
puts("Strings not equal");
}
return 0;
}(and yes, I tried using a bool variable before commenting it out - as it didn't solve the "partial match" problem I was having. As it kept finding partial matches if every character except the very last character in the second array (string2[]) was the same - that's why I commented it out)
Thanks in advance for any help.


So basically my questions are: How does it "remember" that each array element matched the other array's elements when the if statement to print the appropriate message is outside of the loops (both for and the while loops - I did that because I only wanted the message to be printed once, not on each cycle through the loops) ? Also, why does it work without the curly braces for the while loop, but it doesn't seem to work properly without the curly braces for the for loop, even if there's nothing inside it's curly braces ? I'm at least a little baffled here.
And while I don't mean to be rude; please just respond to the questions I've asked above rather than suggesting any "better ways" of comparing strings. I realize there is likely far better ways (like using pointers), but I'm just trying to better understand how the while and particularly the for loop work. So I can get a clear picture, so I CAN use them with things like pointers without constantly getting confused as to what exactly they are actually doing. I plan on doing some "pointer practice" later on, and I'll start a separate thread about that if need be. So I kindly ask that we just focus our attentions on the while and for loops for now, because I've found it's much easier to just focus on one thing at a time (in this case yes, the while loop isn't a for loop, I know, but they are still both loops).
To be clear: if people want to offer suggestions for "doing it better" without throwing in concepts I've not employed already, and without saying "just use such and such function in the C library", then feel free to. I just ask people to bear in mind that the whole point of me doing this is to try and better understand how loops work, **not** to write the absolute best string comparing program you'll be ever likely to come across. I'll also kindly ask that any responses be absolutely clear - even if you have to treat me like a complete idiot. I'll forgive you ;)
Here's the "while loop" version:
Code:#include <stdio.h>
int main(void) {
int i = 0;
const char arrayCompareWith[] = "test";
const char arrayCompare[] = "test";
while ( arrayCompareWith[i] == arrayCompare[i] && arrayCompareWith[i] != '\0' && arrayCompare[i] != '\0' ) //{
++i;
// printf("i = %d arrayCompareWith[i] = %c arrayCompare[i] = %c ? %s\n", i, arrayCompareWith[i], arrayCompare[i], arrayCompareWith[i] == arrayCompare[i] ? "True" : "False");
// }
if ( arrayCompareWith[i] == arrayCompare[i] ) {
puts("Strings equal");
}
else {
puts("Strings not equal");
}
return 0;
}Here's the "for loop" version:
Code:#include <stdio.h>
#include <stdbool.h>
int main(void) {
const char string1[] = "Hello";
const char string2[] = "Hello";
// bool strEqual;
int i;
for( i = 0; string1[i] != '\0' && string2[i] != '\0'; ++i ) {
// printf("i = %d string1[i] = %c string2[i] = %c ? %s\n", i, string1[i], string2[i], string1[i] == string2[i] ? "True" : "False");
// if ( string1[i] == string2[i] ) {
// strEqual = true;
// }
// else {
// strEqual = false;
// break;
// }
}
// if (strEqual) {
if ( string1[i] == string2[i] ) {
puts("Strings equal");
}
else {
puts("Strings not equal");
}
return 0;
}(and yes, I tried using a bool variable before commenting it out - as it didn't solve the "partial match" problem I was having. As it kept finding partial matches if every character except the very last character in the second array (string2[]) was the same - that's why I commented it out)
Thanks in advance for any help.