Some times we need to exit out of the loop with out performing next iteration. SO in such case we make use of break and continue statements.
Break statement:
- break statement is used in order to exit from the loop.
- It Terminates a statement sequence in a switch statement.
- Used to skip all the subsequent instructions and shift the cursor position back to the loop.
- Here the compiler tries to iterate the loop when ever the condition is satisfied .
/*....................... sum of +ve numbers..........................*/
using System;
class Break_Continue
{
static void Main(string [] arg)
{
int num,sum,i;
for(sum=num=i=0;i<5;i++)
{
Console.WriteLine("Enter the +ve number:\t");
num=Convert.ToInt32(Console.ReadLine());
if(num<=0) //............checks for +ve numbers.
continue;
sum=sum+num; //..............adds only +ve numbers.
}
Console.WriteLine("The sum of +ve numbers:\t"+sum); //..... Displays result of +ve numbers.
}
}
Labels: C# Programming
0 Comments:
Subscribe to:
Post Comments (Atom)