Xamarin: Branching and Looping
-
Upload
eng-teong-cheah -
Category
Technology
-
view
166 -
download
1
Transcript of Xamarin: Branching and Looping
Branching and Looping
C# branching and looping depends upon Boolean algebra. Before we understand looping, we need to understand Boolean algebra. Boolean means true or false. A condition can either be true or false.
Bool result = 1<2;
Console.WriteLine(result);
Here we can see 1 is less than 2 which is definitely true so result will be
assigned a value as true.
Jumping from one code block to other:
Jumping from one code block to another is done by goto statement. We can label a code block and jump to that code block by using that label.
start:
bool result = 1< 2;
Console.WriteLine(result);
goto start;
Console.ReadKey();
Here code will never reach last statement as it will jump again and again to
start label.
Branching
Branching can be done using if else construct or ternary operator or Switch statement.- If else construct is used when we have more than one condition’s.- Ternary operator is used when we have single condition evaluating to true or false.- Switch case is use when evaluation to multiple condition is done.
Using if else to calculate input is even or odd.
int result;
Console.WriteLine("Enter input variable");
result = Convert.ToInt32(Console.ReadLine())% 2;
if (result ==0) //condition
{
//if result is even
Console.WriteLine("input is even");
}
else
{
//if result isodd
Console.WriteLine("input is odd");
}
Console.ReadKey();
Using ternary operator? to calculate input is even or odd:General Syntax is: (condition) ? trueresult : falseresult
int result;
Console.WriteLine("Enter input variable");
result = Convert.ToInt32(Console.ReadLine());
string output =result == 0 ? "even" : "odd";
Console.WriteLine("input is {0}",output);
Console.ReadKey();
Using switch operator to calculate input is even or odd:
int result;
Console.WriteLine("Enter input variable");
result = Convert.ToInt32(Console.ReadLine());
switch(result % 2) //condition
{
case 0:
Console.WriteLine("input is even");
break;
case 1:
Console.WriteLine("input is odd");
break;
default:
break;
}
Console.ReadKey();
Break statement is compulsory in C# switch statement.
Looping in C#
Looping can be done in C# in 4 ways. Four ways are as follows:- do while loop
- while loop
- for loop
- foreach loop
do while loop
do while loops are used when first value can be printed without any operation on it.For example: When we need to output a statement balance after getting money from ATM.
// printing 1 to 10 using do while loop
int i=1;
do
{
Console.WriteLine(i++);
}
while (i <=10);
while loop
while loop is used when a loop continues to work till a condition evaluated to true.For example: Working with Reader object from database.
// printing 1 to 10 using while loop
int j = 1;
while (j <= 10)
{
Console.WriteLine(j++);
}
for loop
for loop can be used when we know no of iteration needs to be done.
// printing 1 to 10 using for loop
for (int k = 1; k <= 10; k++)
{
Console.WriteLine(k);
}
Console.ReadKey();
foreach loop
foreach loop in C# is used to loop through a collection.
//program to print 1 to 10 using foreach loop
int[] arr = new int[] { 1, 2, 3,4, 5, 6, 7, 8, 9, 10 };
foreach (int item in arr)
{
Console.WriteLine(item);
}
Console.ReadKey();