SlideShare a Scribd company logo
1 of 13
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

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

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 

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 .