SlideShare a Scribd company logo
LOOPS USED IN C#.NET
Loops are used to execute one or more statements multiple times until a
specified condition is fulfilled. There are many loops in C# such as for
loop , while loop , do while loop etc.
Details about these are given as follows:
1] FOR LOOP IN C#
The for loop executes one or more statements multiple times as long as
the loop condition is satisfied. If the loop condition is true , the body of
the for loop is executed. Otherwise , the control flow jumps to the next
statement after the for loop .
DIAGRAM
As seen from the above diagram, first the initialisation is done.
This declares and initialises the loop variables , if there are any.
Then the condition is evaluated . If it is true , loop body is
executed. If it is false , the control passes to the next statement
after the for loop body. After the loop body is executed, the
loop variables are updated. Then the condition is checked again
and the cycle continues .
SYNTAX AND OUTPUT OF FOR LOOP
The syntax of the for loop is given below:
for ( initialization, condition, increment )
{
// These statements will be executed if the condition evaluates to true
}
A program that demonstrates the for loop is as follows:
Source Code: Program that demonstrates for loop in C#
using System;
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
Console.WriteLine(“First 10 Natural Numbers are: “);
for (int i = 1; i <= 10; i++)
Console.WriteLine(i);
}
}
}
The output of the above program is as
follows:
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
The while loop continuously executes one or
more statements as long as the loop condition
is satisfied. If the loop condition is true, the
body of the for loop is executed. Otherwise, the
control flow jumps to the next statement after
the while loop.
A diagram that demonstrates the flow in the
while loop is given as follows:
2] WHILE LOOP IN C#
As seen from the above diagram, first the
condition is checked. If the condition is true, the
loop body is executed. If the condition is false,
control passes to the next statement after the
loop body. The while loop may never run if the
condition is false the first time it is tested. The
control skips the loop and goes directly to the next
statement.
• The syntax of the while loop is given as follows:
• while (condition) { // These statements will be executed if the
condition evaluates to true }
WHILE LOOP EXAMPLE AND OUTPUT
SYNTAX
using System; namespace LoopDemo
{ class Example
{ static void Main(string[] args) {
int i = 1;
Console.WriteLine
("First 10 Natural Numbers are: ");
while (i <= 10)
{ Console.WriteLine( i );
i++;
}
}
}
}
OUTPUT
The output of the above program is as follows:
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
3] DO WHILE LOOP IN C#
The do while loop executes one or
more statements multiple times as
long as the loop condition is
satisfied. It is similar to the while
loop but the while loop has the test
condition at the start of the loop
and the do while loop has the test
condition at the end of the loop. So
it executes at least once always.
A diagram that demonstrates the
flow in the do while loop is given as
follows:
As seen from the above diagram, the first time loop body is directly executed as the test
condition is at the bottom. The condition is checked. If it is true, the loop body is executed
again. If the condition is false, control passes to the next statement after the loop body.
SYNTAX
{ class Example
{ static void Main(string[] args) {
int i = 1;
Console.WriteLine("First 10 Natural Numbers are:
");
do
{
Console.WriteLine( i );
i++;
}
while (i <= 10); } } }
OUTPUT
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10
4] NESTED LOOPS IN C#
Nested loops are loop where one loop is situated
inside another. Nested loops can be created from
for loops, while loops and do while loops.
SYNTAX
namespace LoopDemo
{
class Example {
static void Main(string[] args) {
Console.WriteLine(“Nested for loop”);
for( int i = 0; i < 5; i++)
{
for(int j=0; j<=i; j++)
Console.Write(“*”);
Console.WriteLine();
}
}
}
}
OUTPUT
Nested for loop
*
**
***
****
*****
5] FOR EACH LOOP IN C#
• The foreach loop executes one or more statements for all the
elements in an instance of the type
Systems.Collection.Generic.IEnumerable<T> or
Systems.Collection.IEnumerable interface.
SYNTAX
{ static void Main(string[] args)
{
Console.WriteLine
("First 10 Natural Numbers are: ");
int[]
naturalNos = new int[]
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
foreach (int i in naturalNos)
{
Console.WriteLine(i);
}
}
}
}
OUTPUT
First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10

More Related Content

Similar to Loops in C.net.pptx

Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
Banasthali Vidyapith
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
Huda Alameen
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
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
Sowmya Jyothi
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
Sriman Sawarthia
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
Mohammed Khan
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
Rj Baculo
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
C language 2
C language 2C language 2
C language 2
Arafat Bin Reza
 
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
ofeliacanaria1
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
Emroz Sardar
 
Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
 
Loops
LoopsLoops
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
AnuragSrivastava272
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
JavvajiVenkat
 

Similar to Loops in C.net.pptx (20)

Loops c++
Loops c++Loops c++
Loops c++
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
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
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
C language 2
C language 2C language 2
C language 2
 
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Loops
LoopsLoops
Loops
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 

Recently uploaded

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 

Recently uploaded (20)

Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 

Loops in C.net.pptx

  • 1. LOOPS USED IN C#.NET Loops are used to execute one or more statements multiple times until a specified condition is fulfilled. There are many loops in C# such as for loop , while loop , do while loop etc. Details about these are given as follows:
  • 2. 1] FOR LOOP IN C# The for loop executes one or more statements multiple times as long as the loop condition is satisfied. If the loop condition is true , the body of the for loop is executed. Otherwise , the control flow jumps to the next statement after the for loop .
  • 3. DIAGRAM As seen from the above diagram, first the initialisation is done. This declares and initialises the loop variables , if there are any. Then the condition is evaluated . If it is true , loop body is executed. If it is false , the control passes to the next statement after the for loop body. After the loop body is executed, the loop variables are updated. Then the condition is checked again and the cycle continues .
  • 4. SYNTAX AND OUTPUT OF FOR LOOP The syntax of the for loop is given below: for ( initialization, condition, increment ) { // These statements will be executed if the condition evaluates to true } A program that demonstrates the for loop is as follows: Source Code: Program that demonstrates for loop in C# using System; namespace LoopDemo { class Example { static void Main(string[] args) { Console.WriteLine(“First 10 Natural Numbers are: “); for (int i = 1; i <= 10; i++) Console.WriteLine(i); } } } The output of the above program is as follows: First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10
  • 5. The while loop continuously executes one or more statements as long as the loop condition is satisfied. If the loop condition is true, the body of the for loop is executed. Otherwise, the control flow jumps to the next statement after the while loop. A diagram that demonstrates the flow in the while loop is given as follows: 2] WHILE LOOP IN C#
  • 6. As seen from the above diagram, first the condition is checked. If the condition is true, the loop body is executed. If the condition is false, control passes to the next statement after the loop body. The while loop may never run if the condition is false the first time it is tested. The control skips the loop and goes directly to the next statement. • The syntax of the while loop is given as follows: • while (condition) { // These statements will be executed if the condition evaluates to true }
  • 7. WHILE LOOP EXAMPLE AND OUTPUT SYNTAX using System; namespace LoopDemo { class Example { static void Main(string[] args) { int i = 1; Console.WriteLine ("First 10 Natural Numbers are: "); while (i <= 10) { Console.WriteLine( i ); i++; } } } } OUTPUT The output of the above program is as follows: First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10
  • 8. 3] DO WHILE LOOP IN C# The do while loop executes one or more statements multiple times as long as the loop condition is satisfied. It is similar to the while loop but the while loop has the test condition at the start of the loop and the do while loop has the test condition at the end of the loop. So it executes at least once always. A diagram that demonstrates the flow in the do while loop is given as follows:
  • 9. As seen from the above diagram, the first time loop body is directly executed as the test condition is at the bottom. The condition is checked. If it is true, the loop body is executed again. If the condition is false, control passes to the next statement after the loop body. SYNTAX { class Example { static void Main(string[] args) { int i = 1; Console.WriteLine("First 10 Natural Numbers are: "); do { Console.WriteLine( i ); i++; } while (i <= 10); } } } OUTPUT First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10
  • 10. 4] NESTED LOOPS IN C# Nested loops are loop where one loop is situated inside another. Nested loops can be created from for loops, while loops and do while loops.
  • 11. SYNTAX namespace LoopDemo { class Example { static void Main(string[] args) { Console.WriteLine(“Nested for loop”); for( int i = 0; i < 5; i++) { for(int j=0; j<=i; j++) Console.Write(“*”); Console.WriteLine(); } } } } OUTPUT Nested for loop * ** *** **** *****
  • 12. 5] FOR EACH LOOP IN C# • The foreach loop executes one or more statements for all the elements in an instance of the type Systems.Collection.Generic.IEnumerable<T> or Systems.Collection.IEnumerable interface.
  • 13. SYNTAX { static void Main(string[] args) { Console.WriteLine ("First 10 Natural Numbers are: "); int[] naturalNos = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; foreach (int i in naturalNos) { Console.WriteLine(i); } } } } OUTPUT First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10

Editor's Notes

  1. As seen from the above diagram, first the initialisation is done. This declares and initialises the loop variables , if there are any. Then the condition is evaluated . If it is true , loop body is executed. If it is false , the control passes to the next statement after the for loop body. After the loop body is executed, the loop variables are updated. Then the condition is checked again and the cycle continues .