def timesten10.times{ |t| puts yield t }end
timesten do |x|x * 2end# will print2468 ... and so on
timesten do |x|breakx * 2end# won't print anything. The break jumps out of the timesten function entirely, and the call to `puts` inside it gets skipped
timesten do |x|break 5x * 2end# This is the same as above. it's "returning" 5, but nobody is catching it. If you did a = timesten... then a would get assigned to 5
timesten do |x|next 5x * 2end# this would print555 ... and so on, because 'next 5' skips the 'x * 2' and 'returns' 5.
// break statementfor (int i = 0; i < 5; i++) {if (i == 3) {break; // It will force to come out from the loop}
lblDisplay.Text = lblDisplay.Text + i + "[Printed] ";}
以下是输出:
0[打印] 1[打印] 2[打印]
所以3[打印]和4[打印]不会显示,因为i==3时有中断
//continue statementfor (int i = 0; i < 5; i++) {if (i == 3) {continue; // It will take the control to start point of loop}
lblDisplay.Text = lblDisplay.Text + i + "[Printed] ";}
'VBFor i=0 To 10If i=5 then Exit For '= break in C#;'Do Something for i<5next
For i=0 To 10If i=5 then Continue For '= continue in C#'Do Something for i<>5...Next
static void Main(string[] args){var numbers = new List<int>();
while (numbers.Count < 5){
Console.WriteLine("Enter 5 uniqe numbers:");var number = Convert.ToInt32(Console.ReadLine());
if (numbers.Contains(number)){Console.WriteLine("You have already entered" + number);continue;}
numbers.Add(number);}
numbers.Sort();
foreach(var number in numbers){Console.WriteLine(number);}
}
{var sum = 0;while (true){Console.Write("Enter a number (or 'ok' to exit): ");var input = Console.ReadLine();
if (input.ToLower() == "ok")break;
sum += Convert.ToInt32(input);}Console.WriteLine("Sum of all numbers is: " + sum);}