一、簡介
1.break語句:循環-循環中斷並停止,退出當前循環;
流程圖:

2.continue:循環-循環下一次迭代繼續執行。
流程圖:

執行過程:立即結果本次循環,判斷循環條件,如果成立,則進入下一次循環,否則退出本次循環。
舉例:我編寫的代碼時候,上個廁所,回來繼續編寫代碼。
二、實例
練習1:輸出“hello world”
第一種:break
while (true)
{
Console.WriteLine("Hello World");
break ;
}
Console.ReadKey();
輸出結果

第二種:continue
while (true)
{
Console.WriteLine("Hello World");
continue;
}
Console.ReadKey();
輸出結果

練習2:用 while continue實現計算1到100(含)之間的除了能被7整除之外所有整數的和。
int sum = 0;
int i = 1;
while (i<=100)
{
if (i%7==0)
{
i++;
continue;
}
sum += i;
i++;
}
Console.WriteLine(sum);
Console.ReadKey();
輸出結果

練習3:找出100內所有的素數
//找出100內所有的素數
//素數/質數:只能被1和這個數字本身整除的數字
//2 3 4 5 6 7
//7 7%1 7%2 7%3 7%4 7%5 7%6 7%7 6%2
for (int i = 2; i <= 100; i++)
{
bool b = true;
for (int j = 2; j <i; j++)
{
//除盡了說明不是質數 也就沒有再往下繼續取余的必要了
if (i % j == 0)
{
b = false;
break;
}
}
if (b)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
//6 6%2 6%3
輸出結果

