SlideShare a Scribd company logo
1 of 20
C# Tutorial
Part 11: Control Statements
www.sirykt.blogspot.com
C# if-else
• In C# programming, the if statement is used to test the condition. There
are various types of if statements in C#.
• if statement
• if-else statement
• nested if statement
• if-else-if ladder
• C# IF Statement
• The C# if statement tests the condition. It is executed if condition is
true.
• Syntax:
• if(condition)
• { //code to be executed }
C# If Example
• using System;
• public class IfExample
• {
• public static void Main(string[] args)
• {
• int num = 10;
• if (num % 2 == 0)
• {
• Console.WriteLine("It is even number");
• }
• }
• }
C# IF-else Statement
• The C# if-else statement also tests the condition. It
executes the if block if condition is true otherwise else
block is executed.
• Syntax:
• if(condition){
• //code if condition is true
• }
• else{
• //code if condition is false
• }
C# IF-else-if ladder Statement
• The C# if-else-if ladder statement executes one condition from multiple
statements.
• Syntax:
• if(condition1){
• //code to be executed if condition1 is true
• }else if(condition2){
• //code to be executed if condition2 is true
• }
• else if(condition3){
• //code to be executed if condition3 is true
• }
• ...
• else{
• //code to be executed if all the conditions are false
• }
• 1.for Loop statement
• 2.foreach loop
• 3.while loop
• 4.do while
• The C# for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use
for loop than while or do-while loops.
• The C# for loop is same as C/C++. We can initialize variable, check
condition and increment/decrement value.
• A group of similar category objects is called as collections.
especially foreach loop is provided for collections
• Syntax:
• for(initialization; condition; incr/decr)
• {
• //code to be executed
• }
foreach
• A group of similar category objects is called as collections. especially
foreach loop is provided for collections
• syntax:
• foreach(control c in this controls)
• { statements; }
• foreach(var item in collections)
• { statements; }
• foreach(object obj in collections)
• { statements; }
Example on foreach loop:
• 1.take one form
• 2.add no of textboxes & take one button.
• 3.click on the button how many textboxes are there we can apply same color
• private void button1_Click(object sender, EventArgs e)
• {
• foreach (Control c in this.Controls)
• {
• // MessageBox.Show(c.GetType().Name);
• if (c.GetType().Name == "TextBox")
• {
• c.BackColor = Color.Red;
• }
• }
• }
While loop
• In C#, while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed, it is recommended to use while loop than for loop.
• syntax:
• while(condition)
• {statements; }
• Example:
• int i = 10;
• while (i<=100)
• {
• Console.WriteLine(i.ToString());
• i++;
• }
• Console.ReadLine();
• }
Do while
• The C# do-while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.
• The C# do-while loop is executed at least once because condition is checked after loop
body.
• syntax:
• do
• { statements; }
• while(condition);
• example:
• static void Main(string[] args)
• { int i = 10;
• do
• { Console.WriteLine("the value i is:" + i.ToString());
• i++;
• } while (i <= 100);
Switch(expression)
• The C# switch statement executes one statement from multiple conditions. It is
like if-else-if ladder statement in C#.
•
{
• case 0:statements;
• break;
• case 1:statements;
• break;
• case 2:statements;
• break;
• default:statements;
• break;
• }
Example on switch:
• static void Main(string[] args)
• { char grade;
• Console.WriteLine("enter grade");
• grade = Convert.ToChar(Console.ReadLine());
• switch (grade)
• { case 'A': Console.WriteLine("excellent marks");
• Console.ReadLine();
• break;
• case 'B': Console.WriteLine("good marks");
• Console.ReadLine();
• break;
• case 'C': Console.WriteLine("average marks");
• Console.ReadLine();
• break;
• case 'D': Console.WriteLine("Fail");
• Console.ReadLine();
• break;
• default: Console.WriteLine("enter A,B,C,D");
• Console.ReadLine();
• break; } } }
C# Break Statement
• The C# break is used to break loop or switch
statement. It breaks the current flow of the
program at the given condition. In case of inner
loop, it breaks only inner loop.
• Syntax:
• jump-statement;
• break;
• using System;
• public class BreakExample
• {
• public static void Main(string[] args)
• {
• for (int i = 1; i <= 10; i++)
• {
• if (i == 5)
• {
• break;
• }
• Console.WriteLine(i);
• }
• }
• }
C# Continue Statement
• The C# continue statement is used to continue loop.
It continues the current flow of the program and
skips the remaining code at specified condition. In
case of inner loop, it continues only inner loop.
• Syntax:
• jump-statement;
• continue;
• using System;
• public class ContinueExample
• {
• public static void Main(string[] args)
• {
• for(int i=1;i<=10;i++){
• if(i==5){
• continue;
• }
• Console.WriteLine(i);
• }
• }
• }
Goto Statement
• The C# goto statement is also known jump statement. It is used to transfer control to the other part
of the program. It unconditionally jumps to the specified label.
• It can be used to transfer control from deeply nested loop or switch case label.
• Currently, it is avoided to use goto statement in C# because it makes the program complex.
• C# Goto Statement Example
• Let's see the simple example of goto statement in C#.
• using System;
• public class GotoExample
• { public static void Main(string[] args)
• { ineligible:
• Console.WriteLine("You are not eligible to vote!");
• Console.WriteLine("Enter your age:n");
• int age = Convert.ToInt32(Console.ReadLine());
• if (age < 18)
• { goto ineligible; }
• else
• { Console.WriteLine("You are eligible to vote!");
• } } }
SIRYKT
Sharing Knowledge is Learning
For more updates
For more visit our website www.sirykt.blogspot.com

More Related Content

What's hot

Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Muhammad Tahir Bashir
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming LanguageMahantesh Devoor
 
While loop
While loopWhile loop
While loopFeras_83
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioAndrey Karpov
 
C++ decision making
C++ decision makingC++ decision making
C++ decision makingZohaib Ahmed
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentalscbcunc
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 

What's hot (20)

Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
While loop
While loopWhile loop
While loop
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
C language
C languageC language
C language
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Checking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-StudioChecking Clang 11 with PVS-Studio
Checking Clang 11 with PVS-Studio
 
Control statements
Control statementsControl statements
Control statements
 
The Loops
The LoopsThe Loops
The Loops
 
LISP:Loops In Lisp
LISP:Loops In LispLISP:Loops In Lisp
LISP:Loops In Lisp
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentals
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
 

Similar to 10control statement in c#

Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)jewelyngrace
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & LoopsAkhil Kaushik
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsEng Teong Cheah
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#Rohit Rao
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slidesluqman bawany
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdfBasirKhan21
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statementsVladislav Hadzhiyski
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 

Similar to 10control statement in c# (20)

Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Unit II.pptx
Unit II.pptxUnit II.pptx
Unit II.pptx
 
Control structure
Control structureControl structure
Control structure
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Looping statements
Looping statementsLooping statements
Looping statements
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
Programming loop
Programming loopProgramming loop
Programming loop
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
Loops in c ii
Loops in c iiLoops in c ii
Loops in c ii
 
Loops in c ii
Loops in c iiLoops in c ii
Loops in c ii
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
 
C loops
C loopsC loops
C loops
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 

More from Sireesh K (20)

Cn10
Cn10Cn10
Cn10
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
What is mvc
What is mvcWhat is mvc
What is mvc
 
31c
31c31c
31c
 
31cs
31cs31cs
31cs
 
45c
45c45c
45c
 
44c
44c44c
44c
 
43c
43c43c
43c
 
42c
42c42c
42c
 
41c
41c41c
41c
 
40c
40c40c
40c
 
39c
39c39c
39c
 
38c
38c38c
38c
 
37c
37c37c
37c
 
35c
35c35c
35c
 
34c
34c34c
34c
 
33c
33c33c
33c
 
30c
30c30c
30c
 
29c
29c29c
29c
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

10control statement in c#

  • 1. C# Tutorial Part 11: Control Statements www.sirykt.blogspot.com
  • 2. C# if-else • In C# programming, the if statement is used to test the condition. There are various types of if statements in C#. • if statement • if-else statement • nested if statement • if-else-if ladder • C# IF Statement • The C# if statement tests the condition. It is executed if condition is true. • Syntax: • if(condition) • { //code to be executed }
  • 3. C# If Example • using System; • public class IfExample • { • public static void Main(string[] args) • { • int num = 10; • if (num % 2 == 0) • { • Console.WriteLine("It is even number"); • } • } • }
  • 4. C# IF-else Statement • The C# if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed. • Syntax: • if(condition){ • //code if condition is true • } • else{ • //code if condition is false • }
  • 5. C# IF-else-if ladder Statement • The C# if-else-if ladder statement executes one condition from multiple statements. • Syntax: • if(condition1){ • //code to be executed if condition1 is true • }else if(condition2){ • //code to be executed if condition2 is true • } • else if(condition3){ • //code to be executed if condition3 is true • } • ... • else{ • //code to be executed if all the conditions are false • }
  • 6. • 1.for Loop statement • 2.foreach loop • 3.while loop • 4.do while
  • 7. • The C# for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop than while or do-while loops. • The C# for loop is same as C/C++. We can initialize variable, check condition and increment/decrement value. • A group of similar category objects is called as collections. especially foreach loop is provided for collections • Syntax: • for(initialization; condition; incr/decr) • { • //code to be executed • }
  • 8. foreach • A group of similar category objects is called as collections. especially foreach loop is provided for collections • syntax: • foreach(control c in this controls) • { statements; } • foreach(var item in collections) • { statements; } • foreach(object obj in collections) • { statements; }
  • 9. Example on foreach loop: • 1.take one form • 2.add no of textboxes & take one button. • 3.click on the button how many textboxes are there we can apply same color • private void button1_Click(object sender, EventArgs e) • { • foreach (Control c in this.Controls) • { • // MessageBox.Show(c.GetType().Name); • if (c.GetType().Name == "TextBox") • { • c.BackColor = Color.Red; • } • } • }
  • 10. While loop • In C#, while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop than for loop. • syntax: • while(condition) • {statements; } • Example: • int i = 10; • while (i<=100) • { • Console.WriteLine(i.ToString()); • i++; • } • Console.ReadLine(); • }
  • 11. Do while • The C# do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop. • The C# do-while loop is executed at least once because condition is checked after loop body. • syntax: • do • { statements; } • while(condition); • example: • static void Main(string[] args) • { int i = 10; • do • { Console.WriteLine("the value i is:" + i.ToString()); • i++; • } while (i <= 100);
  • 12. Switch(expression) • The C# switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C#. • { • case 0:statements; • break; • case 1:statements; • break; • case 2:statements; • break; • default:statements; • break; • }
  • 13. Example on switch: • static void Main(string[] args) • { char grade; • Console.WriteLine("enter grade"); • grade = Convert.ToChar(Console.ReadLine()); • switch (grade) • { case 'A': Console.WriteLine("excellent marks"); • Console.ReadLine(); • break; • case 'B': Console.WriteLine("good marks"); • Console.ReadLine(); • break; • case 'C': Console.WriteLine("average marks"); • Console.ReadLine(); • break; • case 'D': Console.WriteLine("Fail"); • Console.ReadLine(); • break; • default: Console.WriteLine("enter A,B,C,D"); • Console.ReadLine(); • break; } } }
  • 14. C# Break Statement • The C# break is used to break loop or switch statement. It breaks the current flow of the program at the given condition. In case of inner loop, it breaks only inner loop. • Syntax: • jump-statement; • break;
  • 15. • using System; • public class BreakExample • { • public static void Main(string[] args) • { • for (int i = 1; i <= 10; i++) • { • if (i == 5) • { • break; • } • Console.WriteLine(i); • } • } • }
  • 16. C# Continue Statement • The C# continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. In case of inner loop, it continues only inner loop. • Syntax: • jump-statement; • continue;
  • 17. • using System; • public class ContinueExample • { • public static void Main(string[] args) • { • for(int i=1;i<=10;i++){ • if(i==5){ • continue; • } • Console.WriteLine(i); • } • } • }
  • 18. Goto Statement • The C# goto statement is also known jump statement. It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label. • It can be used to transfer control from deeply nested loop or switch case label. • Currently, it is avoided to use goto statement in C# because it makes the program complex. • C# Goto Statement Example • Let's see the simple example of goto statement in C#. • using System; • public class GotoExample • { public static void Main(string[] args) • { ineligible: • Console.WriteLine("You are not eligible to vote!"); • Console.WriteLine("Enter your age:n"); • int age = Convert.ToInt32(Console.ReadLine()); • if (age < 18) • { goto ineligible; } • else • { Console.WriteLine("You are eligible to vote!"); • } } }
  • 20. For more updates For more visit our website www.sirykt.blogspot.com