tem
tem

Reputation: 185

How to return a continue from a while

Hard to describe what I want to express, but I confuse for this problem very long time in my real project. My real project is too complicated to ask here, so I make a sample code like following to specify the suitation.

bool checkQ(int a, int b) {
    if (a < b)
        return true;
    else if (a == b)
        return false;
    else/*This is last else.*/
    {
        cout << "I cannot process a even number." << endl;
        return false;
    }
}

vector<int> fun(vector<int> vec) {
    vector<int> result;
    int die = 29;
    for (int i : vec) {
        do {
            i+=2;
            result.push_back(i);
        } while (checkQ(i,die));
    }

    return result;
}

int main()
{
    vector<int> loop_times{1,2,3};
    vector<vector<int>> vec_result;
    for/*his is outer for*/ (int i : loop_times) {
        vector<int> vec{ 23,25,26,25 };
        vector<int> tem_vec = fun(vec);
        vec_result.push_back(tem_vec);
    }

    for (vector<int> j : vec_result)
    {
        for (int k : j)
            cout << k << "  ";
        cout << endl;
    }
    return 0;
}

This is the output of the sample code

I cannot process a even number.
I cannot process a even number.
I cannot process a even number.

25  27  29  27  29  28  30  27  29

25  27  29  27  29  28  30  27  29

25  27  29  27  29  28  30  27  29

As you see, my function fun cannot to process a even element. I just can return a false to stop process the 30. But actually I hope to return a continue for the outer for in fun. I just don't know how to implement such programe. I think I can change the return false; into a goto function checkQ to do it like

bool checkQ(int a, int b) {
    if (a < b)
        return true;
    else if (a == b)
        return false;
    else/*This is last else.*/
    {
        cout << "I cannot process a even number." << endl;
        goto tag;
    }
}

vector<int> fun(vector<int> vec) {
    vector<int> result;
    int die = 29;
    for (int i : vec) {
        do {
            i+=2;
            result.push_back(i);
        } while (checkQ(i,die));
    }

    return result;
}

int main()
{
    vector<int> loop_times{1,2,3};
    vector<vector<int>> vec_result;
    for/*his is outer for*/ (int i : loop_times) {
        vector<int> vec{ 23,25,26,25 };
        vector<int> tem_vec = fun(vec);
        vec_result.push_back(tem_vec);
    tag:continue;
    }

    for (vector<int> j : vec_result)
    {
        for (int k : j)
            cout << k << "  ";
        cout << endl;
    }
    return 0;
}

As you see, I make many grammar error in above code. Any way, I hope the actually output is

I cannot process a even number.
I cannot process a even number.
I cannot process a even number.

25  27  29  27  29  28  30

25  27  29  27  29  28  30

25  27  29  27  29  28  30

Anybody can help me out?

Upvotes: 4

Views: 205

Answers (2)

Johan
Johan

Reputation: 3871

Encountering an even number in checkQ is an exceptional condition and for this i would use an exception. This allows you choose on which level you want to handle the problem without introducing additional return values and passing them around until you reach the location where the problem is handled.

#include <iostream>
#include <vector>

using namespace std;

struct EvenNumberException
{
};

bool checkQ(int a, int b)
{
  if (a < b)
    return true;
  else if (a == b)
    return false;
  else /*This is last else.*/
  {
    throw EvenNumberException{};
  }
}

vector<int> fun(vector<int> vec)
{
  vector<int> result;
  int die = 29;
  try
  {
    for (int i : vec)
    {
      do
      {
        i += 2;
        result.push_back(i);
      } while (checkQ(i, die));
    }
  }
  catch (EvenNumberException)
  {
    /* no cleanup necessary */
  }
  return result;
}

int main()
{
  vector<int> loop_times{ 1, 2, 3 };
  vector<vector<int> > vec_result;
    for/*his is outer for*/ (int i : loop_times)
    {
      vector<int> vec{ 23, 25, 26, 25 };
      vector<int> tem_vec = fun(vec);
      vec_result.push_back(tem_vec);
    }

    for (vector<int> j : vec_result)
    {
      for (int k : j)
        cout << k << "  ";
      cout << endl;
    }
    return 0;
}

Upvotes: 1

delta
delta

Reputation: 3818

based on the result of checkQ, there are three different actions, but you only provide two return value.

#include <bits/stdc++.h>
using namespace std;

enum CHECKQ_RC {
    GOOD,
    OK,
    NEED_TO_QUIT
};

CHECKQ_RC checkQ(int a, int b) {
    if (a < b)
        return GOOD;
    else if (a == b)
        return OK;
    else/*This is last else.*/
    {
        cout << "I cannot process a even number." << endl;
        return NEED_TO_QUIT;
    }
}

vector<int> fun(vector<int> vec) {
    vector<int> result;
    int die = 29;
    for (int i : vec) {
        int j;
        do {
            i+=2;
            result.push_back(i);
            j = checkQ(i, die);
            if (GOOD != j) break;
        } while (true);
        if (j == NEED_TO_QUIT) break;
    }

    return result;
}


int main()
{
    vector<int> loop_times{1,2,3};
    vector<vector<int>> vec_result;
    for/*his is outer for*/ (int i : loop_times) {
        vector<int> vec{ 23,25,26,25 };
        vector<int> tem_vec = fun(vec);
        vec_result.push_back(tem_vec);
    }

    for (vector<int> j : vec_result)
    {
        for (int k : j)
            cout << k << "  ";
        cout << endl;
    }
    return 0;
}

// without checkQ
vector<int> fun(vector<int> vec) {
    vector<int> result;
    int die = 29;
    for (int i : vec) {
        do {
            i+=2;
            result.push_back(i);
        } while (i < die);
        if (i > die) break;
    }

    return result;
}

Upvotes: 1

Related Questions