开发者

C# continue/goto Basic query

I have this kind of code:

main()
{
   for(i=0;i<100;i++)
   {
   if(cond1)
       func(1); //Just some l开发者_JAVA百科ogics are present here
   if (cond2)
      if(cond3 | cond4)
          func(2);
and so on....
   }
}

void func(int)
{
   do somthing;
   if cond5
      continue;// this is for the FOR loop in main() & I know this doesnt make sense.
}

So, Depending on some IF condition in the function 'func', I want to 'continue' the FOR loop present in main(). How to achieve this? Thanks in advance...


  1. Change your func function return type to bool so it will return true if condition is satisfied and false otherwise.
  2. In your for loop check func return value. If it is try - call continue. Otherwise - do nothing.

    void main()
        {
           for(i=0;i<100;i++)
           {
            if(cond1)
               if (func(1))
                   continue;//Just some logics are present here
           if (cond2)
              if(cond3 | cond4)
                  if (func(2))
                      continue;
            and so on....
           }
        }
    bool func(int)
    {
        do somthing;
        bool bRes = false;
        if cond5
            bRes = true;// this is for the FOR loop in main() & I know this doesnt make sense.
        // ....
        return bRes;
    }
    


Return bool from your function and continue on false. Using your example:

main()
{
   for(i=0;i<100;i++)
   {
   if(cond1)
       func(1); //Just some logics are present here
   if (cond2)
      if(cond3 | cond4)
          if (!func(2))
             continue;
and so on....
   }
}

bool func(int)
{
   do somthing;
   if cond5
      return false;
   return true
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜