Program to demonstrate the use of goto statement in C#
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; class Program { static void Main(string[] args) { int i,tot=0; for (i = 0; i <= 20; i++) { if (i % 2 == 0) {} else { goto cal; } cal: tot = tot + i; } Console.WriteLine("Sum of odd numbers from 0 to 20: "+tot); Console.ReadKey(); } } |
Output:- Sum of odd numbers from 0 to 20: 100 The goto statement is used as a jump statement that skips some of the some that we don’t want to execute on some condition….