Just sharing something

I was (am) working on a function that consists of an infinate while loop, which consists of a switch statement, and the following piece of code crossed my mind. Luckily I talked myself out of it, but thought I would share this desperate piece of code that no one should ever ever see in a program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int myfunction ()
{
  while(true) // infinate loop
  {
    loop:

    switch(i)
    {
      case 1:
        ... // normal code here

        break; // go to next iteration of loop
        continue; // in case break didn't work
        goto loop; // helm not responsive
        return -1; // we're going down
        exit(); // abandon ship! abandon ship!

      case 2:
        ... // here we go again
    }
  }
}


It would work, but boy that would be a headache to read.
I have often used while(true). It's got a valid use when you do not have a known condition for how many times you will need to execute the loop.
Would this pass compiler scrutiny - or would the compiler
say that lines 13,14,15,16 is unreachable code?
Yea, it should pass compiler scrutiny.. just know that lines 13 through 16 would never be reached. (or should never be reached)

it would just be an incredible waste of time to do that with every case, and make it very confusing when sharing the code with other programmers.
but yea, I am using the while(true) in my code.. since it's not the main function but rather another function, I am just using return statements to exit. Here is what I have thus far. Note, it's not complete yet.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
int getid(I_List &temp)
{
   enum tokSwitch { search, list, add, edit };
   tokSwitch cstatus, lstatus; // current status and last status
   string input; // to get inputs as needed
   int iindex; // item index to be passed to edit item

   cstatus = search; // default to search

   if (temp.I_count() < 10)
      cstatus = list; // unless there are less than 20 (arbitrary number) items

   while (true) // infinate loop
   {
      switch (cstatus) // what we do depends on cstatus
      {
         case search:
         {
            // prompt user with options and ask for search string
            cout << endl << "You can enter a partial name,";
            cout << " '-c' to cancel, or '-l' to list all items." << endl;
            cout << "Enter search string: ";

            getline (cin, input); // get input from user

            if (I_tolower(input.substr(0,2)) == "-c") // if user chose cancel
               return -1; // return -1 to calling function

            if (I_tolower(input.substr(0,2)) == "-l") // if user chose list
            {
               cstatus = list; // change to list status
               break; // go to next iteration of loop
            }

            vector<int> sindex; // to store the indexes from searching
            int i = temp.I_getindex(input, 0); // get first index

            while (i != -1) // while indexes are found
            {
               sindex.push_back(i); // add to our search results
               i = temp.I_getindex(input, i+1); // and get next result
            }

            if (sindex.size() == 0) // if there are no results
               cout << endl << "No results found." << endl;

            else // results found
            {
               cout << endl << sindex.size() << " result(s) found." << endl;

               for (int i = 0; i < sindex.size(); i++) // list results
                  cout << i + 1 << ". " << temp.I_getitem(sindex.at(i)).name << endl;

               cout << endl << "Enter the menu number for the item you want, or" << endl;
            }

            // give options to user
            cout << "Type '-c' to cancel, '-l' to list all items, '-a' to add an item" << endl;

            if (sindex.size() > 0) // if results, give option to edit one of them
               cout << "or '-e #' to edit one of the above items." << endl;

            cout << endl << "Enter option: ";
            getline (cin, input); // get user input

            if (isdigit(input.at(0))) // if we have a number
            {
               int i = atoi (input.c_str()); // convert to integer
               if (i-1 < sindex.size()) // if it's a valid number
                  return temp.I_getitem(sindex.at(i-1)).id; // return the ID

               cout << endl << "Invalid Number!" << endl; // invalid number
               break; // go to next iteration of loop
            }

            if (I_tolower(input.substr(0,2)) == "-c") // if user chose cancel
               break; // at this point, just go to next iteration of loop

            if (I_tolower(input.substr(0,2)) == "-l") // if user chose to list
            {
               cstatus = list; // change status to list
               break; // and go to next iteration of loop
            }

            if (I_tolower(input.substr(0,2)) == "-a") // if user chose to add
            {
               lstatus = search; // set last status to search
               cstatus = add; // set current status to add
               break; // go to next iteration of loop
            }

            if (I_tolower(input.substr(0,2)) == "-e") // if user chose to edit
            {
               if ((input.size() > 3) && (isdigit(input.at(3)))) // if '-e #'
               {
                  int i = atoi (input.substr(3).c_str()); // convert to integer
                  if (i-1 < sindex.size()) // if number is valid
                  {
                     iindex = sindex.at(i-1); // set item index to item to edit

                     lstatus = search; // set last status to search
                     cstatus = edit; // set current status to edit

                     break; // go to next iteration of loop
                  }
               }
            }

            cout << endl << "Invalid Option!" << endl;
            break; // just go to next iteration of loop
         }
         case list:
         {
            int i = temp.I_count(); // get size of list

            cout << "There are " << i << " items in the list." << endl;

            if (i > 20) // if count is higher than 20
               i = (i / 2) + (i % 2); // split it in half

            for (int j = 0; j < i; j++)
            {
               cout << j << ". " << temp.I_getitem(j).name;

               if ((j+i) < temp.I_count()) // second column
               {
                  for (int k = temp.I_getitem(j).name.size(); k < 38; k++)
                    cout << " "; // pad with spaces

                  cout << j + i << ". " << temp.I_getitem(j+i).name;
               }

               cout << endl; // and add endl to each line
            }
            // promt user and get input etc..
            return -1;
         }
         case add:
         {
            cout << "Not implemented yet." << endl;
            return -1;
         }
         case edit:
         {
            cout << "Not implemented yet." << endl;
            return -1;
         }
      }
   }
}

I would've thought newer compilers would complain about the unreachable code too. I am fairly sure mine would (GCC 3.9.5 and 4+).

but that code is completely a waste of time, not point putting it because your break will work. There is no "maybe" it won't; otherwise, maybe your if statement will get skipped etc... nothing would correctly.... It'd be like running Windows Vista.. hehe
i've not tried it, it just makes logical sence..

The reason the thought crossed my mind, was I was comparing which would be better to use, break or continue. since in this paticular case, they both are performing the same function.
Apparently they are similar in functionality, but logically they are not.
Let me put it this way, when it encounters a break in the switch block, it tells to break and get out of switch block and then obviously nothing more found to execute in the while block, so it goes to next iteration of the while loop (unless found in a false state).
And continue, does not apply to switch block in fact it is for the while loop. So you are forcing it to next iteration from switch block itself. The outcome is same, but you are skipping one step is that getting out of switch and checking the rest of the code in the while loop/block.

To prove it, just add a simple printf or cout statement immediately after the switch block and check it out with only continue statement in the switch block. In the case of 'continue', it would not print that output statement.

Hope you got my point. Good luck :)
Last edited on
yea.. which is why i said "in this paticular case".. since there are no instructions after the switch block, the break is doing the same thing that continue would do..

I do realize the difference, though, where break exits the switch and continue jumps to the next iteration of the while loop.
Topic archived. No new replies allowed.