SlideShare a Scribd company logo
Branching and Looping
Eng Teong Cheah
Microsoft MVP Windows Development
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
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 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();
Demo
Get Started Today
Xamarin.com
•Website:
• http://techschool.com/
•Get Started:
• http://xamarin.com
Reference

More Related Content

PPTX
Exception Handling in C++
PPTX
PPTX
Conditionalstatement
PPS
Java Exception handling
PDF
Exception handling
PPTX
Control structure of c
PPT
Java and internet fundamentals.
Exception Handling in C++
Conditionalstatement
Java Exception handling
Exception handling
Control structure of c
Java and internet fundamentals.

What's hot (20)

PDF
Methods in Java
PPSX
python Function
PPTX
Exception handling in java
PPT
JAVA APPLET BASICS
PPTX
The Loops
PPTX
While , For , Do-While Loop
ODP
Multithreading In Java
PPTX
Loops Basics
PPTX
Conditions In C# C-Sharp
PPT
Kirchhoff's Laws
PPS
Introduction to class in java
DOC
Java interview questions
PPTX
Type casting in c programming
PPT
Java Threads
PPT
Exception Handling in JAVA
PPTX
C++ programming function
PDF
Creating your own exception
PPSX
Files in c++
PPTX
Electrical Circuit - Lecture#2
PPTX
JavaFX Presentation
Methods in Java
python Function
Exception handling in java
JAVA APPLET BASICS
The Loops
While , For , Do-While Loop
Multithreading In Java
Loops Basics
Conditions In C# C-Sharp
Kirchhoff's Laws
Introduction to class in java
Java interview questions
Type casting in c programming
Java Threads
Exception Handling in JAVA
C++ programming function
Creating your own exception
Files in c++
Electrical Circuit - Lecture#2
JavaFX Presentation
Ad

Viewers also liked (7)

PDF
Xamarin: Namespace and Classes
PPTX
Introduction of C#
PDF
Xamarin: C# Methods
PDF
Xamarin: Inheritance and Polymorphism
PDF
Learn C# Programming - Data Types & Type Conversion
PPTX
Xamarin - First Application
PDF
Learn C# programming - Program Structure & Basic Syntax
Xamarin: Namespace and Classes
Introduction of C#
Xamarin: C# Methods
Xamarin: Inheritance and Polymorphism
Learn C# Programming - Data Types & Type Conversion
Xamarin - First Application
Learn C# programming - Program Structure & Basic Syntax
Ad

Similar to Xamarin: Branching and Looping (20)

PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PPTX
Loop control statements
PPTX
Flow Control (C#)
PPTX
Introduction to programming in C++ : Loop Structure.pptx
PPT
04a intro while
PDF
[C++][a] tutorial 2
PDF
C++ Course - Lesson 1
PPTX
C++ decision making
PDF
Loop and while Loop
PPTX
Managing input and output operations & Decision making and branching and looping
PPTX
C language 2
PDF
csharp repitition structures
PDF
Chapter 3
PPT
Lecture 1
PPT
Lecture 1
PPTX
Looping statements
PPT
Programs in C based on looping statements
PPTX
Chapter i c#(console application and programming)
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
Loop control statements
Flow Control (C#)
Introduction to programming in C++ : Loop Structure.pptx
04a intro while
[C++][a] tutorial 2
C++ Course - Lesson 1
C++ decision making
Loop and while Loop
Managing input and output operations & Decision making and branching and looping
C language 2
csharp repitition structures
Chapter 3
Lecture 1
Lecture 1
Looping statements
Programs in C based on looping statements
Chapter i c#(console application and programming)

More from Eng Teong Cheah (20)

PDF
Modern Cross-Platform Apps with .NET MAUI
PDF
Efficiently Removing Duplicates from a Sorted Array
PDF
Monitoring Models
PDF
Responsible Machine Learning
PDF
Training Optimal Models
PDF
Deploying Models
PDF
Machine Learning Workflows
PDF
Working with Compute
PDF
Working with Data
PDF
Experiments & TrainingModels
PDF
Automated Machine Learning
PDF
Getting Started with Azure Machine Learning
PDF
Hacking Containers - Container Storage
PDF
Hacking Containers - Looking at Cgroups
PDF
Hacking Containers - Linux Containers
PDF
Data Security - Storage Security
PDF
Application Security- App security
PDF
Application Security - Key Vault
PDF
Compute Security - Container Security
PDF
Compute Security - Host Security
Modern Cross-Platform Apps with .NET MAUI
Efficiently Removing Duplicates from a Sorted Array
Monitoring Models
Responsible Machine Learning
Training Optimal Models
Deploying Models
Machine Learning Workflows
Working with Compute
Working with Data
Experiments & TrainingModels
Automated Machine Learning
Getting Started with Azure Machine Learning
Hacking Containers - Container Storage
Hacking Containers - Looking at Cgroups
Hacking Containers - Linux Containers
Data Security - Storage Security
Application Security- App security
Application Security - Key Vault
Compute Security - Container Security
Compute Security - Host Security

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction

Xamarin: Branching and Looping

  • 1. Branching and Looping Eng Teong Cheah Microsoft MVP Windows Development
  • 2. 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.
  • 3. 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.
  • 5. 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.
  • 6. 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();
  • 7. 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();
  • 8. 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.
  • 10. 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
  • 11. 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);
  • 12. 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++); }
  • 13. 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();
  • 14. 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();
  • 15. Demo