while statement

Apr 21, 2013 at 6:59am
I'm confused with this one particular while statement. Can someone explain when will the loop be terminated and if possible, some example?

1
2
found = false;
while (current != NULL && !found)



and also, I'm confused which statement is included in the while loop. Here is the full code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
void list::insert(const int& newItem)
{
     nodeType *current;
     nodeType *trailCurrent;
     nodeType *newNode;
     bool found;
     newNode = new nodeType;
     newNode->info = newItem;
     newNode->link = NULL;

     if (first == NULL)  
     {
         first = newNode;
         last = newNode;
         count++;
     }
     else 
     {
         current = first;
         found = false;

        


	 while (current != NULL && !found) 
            if (current->info >= newItem)
                found = true;
            else
            {
                trailCurrent = current; 
                current = current->link; 
            }




         if (current == first)
         {
             newNode->link = first;
             first = newNode;
             count++;
         }
         else
         {
             trailCurrent->link = newNode;
             newNode->link = current;

             if (current == NULL)
                 last = newNode;

             count++;
         }
     }
}
Last edited on Apr 21, 2013 at 7:12am
Apr 21, 2013 at 7:06am
There can be said nothing. The loop as can be terminated and can be not terminated. You did not show the compound statement of the loop.
Apr 21, 2013 at 7:07am
I edited the OP. Thanks
Apr 21, 2013 at 7:13am
Here is the loop

1
2
3
4
5
6
7
8
	 while (current != NULL && !found) 
            if (current->info >= newItem)
                found = true;
            else
            {
                trailCurrent = current; 
                current = current->link; 
            }


It will be terminated either when there will be a node with field info that has a value greater than or equal to newItem or when there will not be such a node.
Apr 21, 2013 at 7:37am
Oh ok, I get it now. Thanks for the help!
Last edited on Apr 21, 2013 at 7:59am
Topic archived. No new replies allowed.