คำสั่ง Continue ทำให้การวนซ้ำนั้นข้ามส่วนอื่นๆ ของร่างกายและทดสอบเงื่อนไขซ้ำทันทีก่อนที่จะทำซ้ำ
คำสั่ง Continue ใน C# ทำงานเหมือนกับคำสั่ง break อย่างไรก็ตาม แทนที่จะบังคับให้ยุติ ให้บังคับให้มีการวนซ้ำรอบถัดไป โดยข้ามโค้ดใดๆ ที่อยู่ระหว่างนั้น
สำหรับ while loop คำสั่ง Continue ทำให้การควบคุมโปรแกรมผ่านไปยังการทดสอบแบบมีเงื่อนไข
ต่อไปนี้เป็นรหัสที่สมบูรณ์เพื่อใช้คำสั่งดำเนินการต่อในขณะที่วนรอบ
ตัวอย่าง
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
/* loop execution */
while (a < 20) {
if (a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
Console.WriteLine("value of a: {0}", a);
a++;
}
Console.ReadLine();
}
}
} ผลลัพธ์
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19