In this post we will discuss on loops
A loop statement allows us to execute a statement or group of statements multiple times.c# provides different types of loops.
If and Else statement :
One of the single most important statements in every programming language is the if statement.With an if-statement, we make a logical decision based on it. In an if-statement, we test expressions. These evaluate to true or false.
Syntax :
if(condition) // if condition satisfies loop executes
{
//something
}
else
{
else
{
//do something // if condition fails else loop executes
}
Example
using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
int number;
Console.WriteLine("Please enter a number between 0 and 10:");
number = int.Parse(Console.ReadLine());
if (number > 10)
Console.WriteLine("Hey! The number should be 10 or less!");
else
if (number < 0)
Console.WriteLine("Hey! The number should be 0 or more!");
else
Console.WriteLine("Good job!");
Console.ReadLine();
}
}
}
Switch Statement :
Multiple if else can be replaced by switch .
It is used when there is multiple if condition in a program. It also includes a default value in Default statements. If no any case matches then Default statements executes and run the code.
Example:
using System;
namespace arrays {
class Program {
static void Main(string[] args) {
int Number = int.Parse(Console.ReadLine());
switch (Number) {
case 10:
Console.WriteLine("Number is 10");
break; //will break the switch statement
case 9:
Console.WriteLine("Number is 9");
break;
case 8:
Console.WriteLine("Number is 8");
break;
default:
Console.WriteLine("Number is {0}", Number);
break;
}
}
}
}
Program with single case for multiple values using switch
Example
using System;
namespace arrays {
class Program {
static void Main(string[] args) {
int Number = int.Parse(Console.ReadLine());
switch (Number) {
case 10: //creates a single case for multiple values
case 9:
case 8:
Console.WriteLine("Number is {0}", Number);
break;
default:
Console.WriteLine("Number is not 10,9,8", Number);
break;
}
Console.ReadLine();
}
}
}
Goto :
Goto used in switch case will goto particular label or to the particular case statement.You create label at anywhere in program then can pass the execution control via the goto statements.
It can help us understand branches and control flow.
Example
using System;
namespace goto_statement {
class Program {
static void Main(string[] args) {
string name;
label: //creating label with colon(:)
Console.WriteLine("Enter your name:");
name = Console.ReadLine();
Console.WriteLine("Welcome {0}", name);
goto label; //jump to label statement
}
}
}
While:
The while loop simply executes a block of code as long as the condition you give it is true.
It checks the condition first and there should be increment variable.
Example:
using System;
namespace While_Loop {
class Program {
static void Main(string[] args) {
int num1, res, i;
Console.WriteLine("Enter a number");
num1 = Convert.ToInt32(Console.ReadLine());
i = 1; //Initialization
//Check whether condition matches or not
while (i <= 10) {
res = num1 * i;
Console.WriteLine("{0} x {1} = {2}", num1, i, res);
i++; //Increment by one
}
Console.ReadLine();
}
}
}
Do...While Loop.:
Unlike while loops, which test the loop condition at the start of the loop, the do...while loop checks its condition at the end of the loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
Example:
using System;
namespace Loops {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
Console.WriteLine("value of a: {0}", a);
a = a + 1;
}
while (a < 20);
Console.ReadLine();
}
}
}
foreach
A typical foreach loop that displays the contents of an array of integers
It doesn’t includes initialization, termination and increment/decrement characteristics. It uses collection to take value one by one and then processes them.
Syntax:
foreach(string name in arr) {
}
Example
using System;
class Program {
static void Main() {
int[] Numbers = new int[3];
Numbers[0] = 1;
Numbers[1] = 2;
Numbers[2] = 3;
// ... Loop with the foreach keyword.
foreach(int i in Numbers) {
Console.WriteLine(i);
}
}
}
0 comments:
Post a Comment