Top Banner
Branching and Looping Eng Teong Cheah Microsoft MVP Windows Development
17

Xamarin: Branching and Looping

Feb 08, 2017

Download

Technology

Eng Teong Cheah
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Xamarin: Branching and Looping

Branching and Looping

Eng Teong Cheah

Microsoft MVP Windows Development

Page 2: 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.

Page 3: Xamarin: Branching and Looping

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.

Page 4: Xamarin: Branching and Looping

Branching

Page 5: Xamarin: Branching and Looping

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.

Page 6: Xamarin: Branching and Looping

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();

Page 7: Xamarin: Branching and Looping

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();

Page 8: Xamarin: Branching and Looping

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.

Page 9: Xamarin: Branching and Looping

Looping in C#

Page 10: Xamarin: Branching and Looping

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

Page 11: Xamarin: Branching and Looping

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);

Page 12: Xamarin: Branching and Looping

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++);

}

Page 13: Xamarin: Branching and Looping

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();

Page 14: Xamarin: Branching and Looping

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();

Page 15: Xamarin: Branching and Looping

Demo

Page 16: Xamarin: Branching and Looping

Get Started TodayXamarin.com

Page 17: Xamarin: Branching and Looping

•Website:

• http://techschool.com/

•Get Started:

• http://xamarin.com

Reference