Jump Statements in C#.Net
Jump Statements in C#
In C#, Jump statements are used to transfer
control from one point to another point in the program due to some specified
code while executing the program. There
are below keywords in the Jump Statements:
·
goto
·
break
·
continue
·
return
· throw
goto:
This statement is used to transfer control to
the labeled statement in the program. The label is the valid identifier and
placed just before the statement from where the control is transferred.
break:
The break statement is used to terminate the
loop or statement in which it present. After that, the control will pass to the
statements that present after the break tatement, if available.
continue:
This statement is used to skip over the
execution part of the loop on a certain condition. After that, it transfers the
control to the beginning of the loop. Basically, it skips its following
statements and continues with the next iteration of the loop.
return:
This statement terminates the execution of the
method and returns the control to the calling method. It returns an optional
value. If the type of method is void, then the return statement can be excluded.
throw:
This is used to create an object of any valid exception class with the help of new keyword manually. The valid exception must be derived from the Exception class.
while Loop
WAP, o display multiplication table for given number.
using System;
namespace loop
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number:
");
int a =
Convert.ToInt32(Console.ReadLine());
int i = 1; //initialization
while (i <= 10) //condition
{
Console.WriteLine(a + " *
" + i + " = " + i*a);
i++; //increment
}
Console.ReadLine();
}
}
}
---------------------------------------------------------------------
do while Loop
WAP, to print even natural numbers from 1 to 10.
using System;
namespace loop
{
class Program
{
static void Main(string[] args)
{
int a = 1; //initialization
do //start of loop
{
if (a % 2 == 0)
Console.WriteLine(a);
a++; //increment
} while (a <= 10); //condition
Console.ReadLine();
}
}
}
------------------------------------------------
foreach Loop
WAP, to read vowels from an array of characters and display it using foreach loop
using System;
namespace loop
{
class Program
{
static void Main(string[] args)
{
char[] vowels = { 'a', 'e', 'i',
'o', 'u'};
foreach (char v in vowels)
{
Console.WriteLine(v);
}
Console.ReadLine();
}
}
}
---------------------------------------
break jump statement demo program
using System;
class JDemo{
// Main Method
static public void Main()
{
// Jump Statements is printed only 2
times
// because of break statement
for (int i = 1; i < 4; i++)
{
if (i == 3)
break;
Console.WriteLine("Jump
Statements");
}
}
}
continue jump statement demo program
using System;
class JSDemo{
// Main Method
public static void Main()
{
// This will skip 4 to print
for (int i = 1; i <= 10; i++) {
// if the value of i becomes 4 then
// it will skip 4 and send the
// transfer to the for loop and
// continue with 5
if (i == 4)
continue;
Console.WriteLine(i);
}
}
}
using System;
class JPDemo {
// taking null in the string
static string sub = null;
// method to display subject name
static void displaysubject(string sub1)
{
if (sub1 == null)
throw new NullReferenceException("Exception Message");
}
// Main Method
static void Main(string[] args)
{
// using try catch block to
// handle the Exception
try
{
// calling the static method
displaysubject(sub);
}
catch(Exception exp)
{
Console.WriteLine(exp.Message );
}
}}
Comments
Post a Comment