[C#]while文にbreakで二重ループを抜けるサンプル

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
 while(条件式1) {
while(条件式2) {
if(条件)
// xxxx
break;
}
if(条件){
//xxxx
break;
}
}
 while(条件式1) { while(条件式2) { if(条件){ // xxxx break; } } if(条件){ //xxxx break; } }
 while(条件式1) {
   while(条件式2) {
     if(条件){
       // xxxx
        break;
       }
   }
   if(条件){
   //xxxx
   break;
   }
}

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 13;
Boolean flg = false;
while (true)
{
while (true)
{
if (i == 16)
{
flg = true;
//breakは内側のwhile文のみ抜ける
break;
}
Console.WriteLine(i);
i++;
}
if (flg == true)
{ //break文で外側のwhile文を抜ける
break;
}
}
Console.ReadKey();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int i = 13; Boolean flg = false; while (true) { while (true) { if (i == 16) { flg = true; //breakは内側のwhile文のみ抜ける break; } Console.WriteLine(i); i++; } if (flg == true) { //break文で外側のwhile文を抜ける break; } } Console.ReadKey(); } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 13;
            Boolean flg = false;
            while (true)
            {
                while (true)
                {
                    if (i == 16)
                    {
                        flg = true;
                        //breakは内側のwhile文のみ抜ける
                        break;
                    }
                    Console.WriteLine(i); 
                    i++;
                }
                if (flg == true)
                { //break文で外側のwhile文を抜ける
                    break;
                }
            }
            Console.ReadKey(); 
        }
    }
}

結果
13
14
15

C#

Posted by arkgame