2009-11-08 3 views
0

J'ai ce genre de code:C# continuer/goto requête de base

main() 
{ 
    for(i=0;i<100;i++) 
    { 
    if(cond1) 
     func(1); //Just some logics 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. 
} 

Ainsi, selon une condition IF dans la fonction « func », je veux « continuer » FOR actuelle boucle principale (). Comment y parvenir? Merci d'avance ...

Répondre

1

Retournez bool de votre fonction et continuez sur faux. En utilisant votre exemple:

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 
} 
3
  1. Changer votre fonction type de fonction de retour à bool il retourne vrai si la condition est satisfaite et false sinon.
  2. Dans votre boucle for, vérifiez la valeur de retour. Si c'est essayer - appelez continuer. Sinon, ne faites rien.

    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; 
    } 
    
+0

Bonne réponse mais vous aurait pu ajouter un texte sur la façon dont vous avez changé (amélioré) la programflow. –

+0

Merci pour le conseil. – Oleg