Adak, thanks for the explanation, if I understand you correctly, the IF will deal with variables and see if the input meets the requirments of the varaiable and determining what action to do if the statement is true or false(Boolean), the WHILE or the ELSE determines what the next step is if the requirments arent meet in the if statement, my next question is can you use a ELSE or While statement if you dont have an IF? and when do I want to use FOR statments?
If() statements measure the variable, against some value or variable. If your car gets > 25 mpg, then your car's mileage might be judged as "good":
Code:
if(mpg > 25)
printf("Mileage is good\n");
else
printf("Mileage is not so good");
so the "else" part handles logic when the if() part is not being evaluated to true. The while() loops have nothing to do with if() or else statements, directly. They can be combined, and frequently are:
Code:
while(1) { //starting an "endless" loop, so it needs break logic
if(mpg > 40) {
printf("\nGreat mileage!);
break;
} //but it's bad logic
}
Now tell me what happens in this loop, if the car has less than or equal to 40 miles per gallon?
if() statements may be used to test ANY variable, against either other variables, or (like in the mileage example), a given value.
A typical while() loop is used to read in lines of text from a file. You never know if a file has no lines (an empty file), or lots of lines, so a while() loop is perfect for this. It just keeps looping until the file's contents have all been read:
Code:
while((fgets(bufferName, sizeof(bufferName), filePointerName)) != NULL) {
//code to handle the data received from the file, goes here
}
Note that you will ALWAYS want to try and read AT LEAST ONE line of data from a file - even if it's empty. So it sounds like this would be a candidate for a do while() loop - that's really what the do while() loop is for, but a while() loop can make it even more clear and shorter, as well.
Which is one reason why while() loops are more popular in C code, than do while() loops are.
Code:
do {
fileOK=0;
if((fgets(bufferName, sizeof(bufferName), filePointerName)) != NULL) {
//code to handle the data from the file goes here
fileOK=1;
}
}while(fileOK);
The above do while loop will work fine, but it just can't be quite as clear as the earlier while() loop.
You could do the above file reading with a for loop, but it's not as intuitive to do so. Any while() loop or do while() loop, can be re-written as a for loop, (and the reverse is also true), but the resulting code is just not as clear and simple.
Consider these two loops from Quicksort: (a famous sorting algorithm)
Code:
while(array[left] <= pivot) left++;
while(array[right] >= pivot) right--;
Here, the value of left may need to remain unchanged, IF the value of the array[left] is > the value of the pivot, on the very first comparison. Same for the value of right.. It's VERY clear just what is being done here, with both these simple while loops. You COULD make these loops work by using a for loop, but why would you want to?
Make clarity a goal in your code, after accuracy (which is the #1 goal, always).