问题
I´m currently working on my semester work and this is a piece of my code. As you can see there is a for loop with some if statements where I am working with a structure. I was thinking of converting this for loop onto a while loop but I´m not quite sure how. Maybe someone may give me a hint?
for(X = 1; X <= 100; X++)
{
if(structure[X].number == -1)
{
structure[X].number = number;
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
if(structure[X].number == number)
{
if(structure[X].first_info == -1)
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
}
回答1:
You can do this to convert to while
loop. But I would suggest to search and understand the loop
structures yourself.
X=0; /* for arrays indexing always starts at 0 */
while(X<100)
{
if(structure[X].number == -1)
{
structure[X].number = number;
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
if(structure[X].number == number)
{
if(structure[X].first_info == -1) /* adding braces */
{
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
}
X++;
}
回答2:
Converting the for
loop to a while
loop is easy, but the for
loop is a much safer construct as it groups the initialization, test and increment of the index variable in one clear place.
Incidentally, your code is misindented, and the nested if
statements may be inconsistent:
if(structure[X].number == number)
{
if(structure[X].first_info == -1)
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
When properly reformated, it becomes:
if (structure[X].number == number) {
if (structure[X].first_info == -1)
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
Is this what you intended? Be more careful with indentation, it helps avoid silly mistakes.
Here is the converted code, but be aware that contrary to a for
statement, a continue
statement would by-pass the X++
update expression. Also worth noting is the surprising use of a 1 based index value for X
. Index values are 0 based in C and array size can be checked with a <
comparison instead of <=
.
X = 1;
while (X <= 100) {
if (structure[X].number == -1) {
structure[X].number = number;
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
if (structure[X].number == number) {
if (structure[X].first_info == -1)
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
X++;
}
If structure
has 100 elements, the code must be changed to:
X = 0;
while (X < 100) {
if (structure[X].number == -1) {
structure[X].number = number;
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
if (structure[X].number == number) {
if (structure[X].first_info == -1)
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
X++;
}
回答3:
All basic loops have three components:
- an initializer (an initial value assigned to a variable)
- a condition (the value we are checking for to stay in the loop)
- a modifier (something that modifies our variable)
With a for loop you notice all three of those here:
for(X = 1; X <= 100; X++)
The first component in the parentheses is an initializer (X=1), the next component is the condition (X <= 100), and the last component is the modifier (X++).
We can use the exact same components with a while loop. We just place them differently:
int x = 1; //our intializer
while (x <= 100){ //our condition
if(structure[X].number == -1)
{
structure[X].number = number;
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
if(structure[X].number == number)
{
if(structure[X].first_info == -1){
structure[X].first_info = position;
}
structure[X].second_info = position;
break;
}
x++; //our modifier
}
With that said, arrays in C always start at index 0. I kept your code the same as you had it, but generally whether you are using a for loop or while loop you will want to change your initializer and condition as follows:
- your initializer should likely be x = 0 (not x = 1)
- your condition should likely be either x < 100 or x <= 99 (not x <= 100)
This is because an array of 100 items will have indexes 0 to 99.
Two more points:
When counting through an indexed array like this the code is usually easier to read as a for loop rather than a while loop. Is there a reason that you were wanting to use a while loop instead?
Based on your indentation it was unclear what should be included in the last if clause. I have added braces to your last if clause to make it easier to read, but maybe other lines should have been included in the braces as well?
回答4:
The for
loop construct groups the index variable initialization, loop conditional, and index increment steps of the loop into one statement. So in order to convert a for
loop into a while
loop, you just need to unwrap this grouping into effectively three separate statements. In general, you can transform any for
loop into a while
loop using the following algorithm. Take a for
loop like so:
for(A;B;C) { /* where A,B,C are valid C statements (e.g., X=0), not variables */
/* operational code */
}
To the following while
loop using the same statements within the A, B, C placeholders:
A;
while(B) {
/* operational code */
C;
}
With this algorithm, you should be able to convert any for
loop into a equivalent while
loop.
回答5:
I think this one is pretty simple. Maybe I misunderstood your situation, if then forgive me :)
My approach is like this:
var X = 1;
while (X<=100)
{
if(structure[X].number == -1)
{
structure[X].number = number;
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
if(structure[X].number == number)
{
if(structure[X].first_info == -1)
structure[X].first_info = position;
structure[X].second_info = position;
break;
}
X++;
}
回答6:
Existing code can be converted into while as below
int x =1;
while(x!=100)
{
//Your code here
x++;
}
来源:https://stackoverflow.com/questions/49211160/converting-for-loop-to-while-loop