SlideShare a Scribd company logo
1 of 16
C# LOOPS STATEMENTS
There may be a situation, when you need to execute a block of code several number
of times. In general, the statements are executed sequentially: The first statement in a
function is executed first, followed by the second, and so on.
A loop statement allows us to execute a statement or a group of statements multiple
times
do
{
statement;
}
while(condition)
For(start;stop;step)
{
statement;
}
while(condition)
{
statement;
}
For(Initial Value; Condition; Steps)
{
statements;
}
The For loop statement is used in C# to execute a code
multiple times
FOR LOOP
private void button1_Click(object sender, EventArgs e)
{
for(int i=0;i<=4;i++)
{
MessageBox.Show("hello");
}
}
EXAMPLE 1
User for loop to show hello 5 times in a messagebox
private void button1_Click(object sender, EventArgs e)
{
for(int i=1;i<=10;i++)
{
MessageBox.Show(i.ToString());
}
}
Use for loop to show from 1 to 10 in a messagebox
EXAMPLE 2
private void button1_Click(object sender, EventArgs e)
{
for(int i=1;i<=100;i++)
{
listBox1.Items.Add(i.ToString());
}
}
EXAMPLE 3
Use for loop to insert numbers from 1 to 100 to a list box
private void button1_Click(object sender, EventArgs e)
{
for(char i='A';i<='Z';i++)
{
listBox1.Items.Add(i);
}
}
EXAMPLE 4
Use for loop to insert upper case alphabet to a list box
private void button1_Click(object sender, EventArgs e)
{
for(int i=100;i>=0;i--)
{
listBox1.Items.Add(i.ToString());
}
}
EXAMPLE 5
Use for loop to insert numbers from 100 to 1 to a list box
private void button1_Click(object sender, EventArgs e)
{
for (int i=1;i<=100;i++)
{
if(i%2==0)
{
listBox1.Items.Add(i.ToString());
}
}
}
EXAMPLE 6
Use for loop to insert even numbers from 1 to 100 to a list
box
private void button1_Click(object sender, EventArgs e)
{
for (int i=0;i<=100;i++)
{
if(i%5==0)
{
listBox1.Items.Add(i.ToString());
}
}
}
EXAMPLE 7
Using for loop insert {0,5,10,15,20,15,30,35,…..100}to
listbox
private void button1_Click(object sender, EventArgs e)
{
for (int i=0;i<=100;i++)
{
listBox1.Items.Add(i.ToString());
}
}
while(condition)
{
statement(s);
}
The while statement continually executes a block of statements until
a specified expression evaluates to false . The expression is
evaluated each time the loop is encountered and the evaluation result
is true, the loop body statements are executed.
WHILE LOOP
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
while(i<=4)
{
MessageBox.Show("hello");
i++;
}
}
EXAMPLE 8
Use while loop to show hello 5 times
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
while(i<=10)
{
if (i % 2 == 0)
{
listBox1.Items.Add(i.ToString());
}
i++;
}
}
EXAMPLE 9
Use while loop to add even number 1:10 to list box
Do
{
statement(s);
}
while(condition)
The Do while statement continually executes a block of statements
until a specified expression evaluates to false . The expression is
evaluated each time the loop is encountered and the evaluation result
is true, the loop body statements are executed.
DO WHILE LOOP
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
do
{
MessageBox.Show("hello");
i++;
}
while (i <= 4);
}
EXAMPLE 10
User Do whileto show hello 5 times in a messagebox

More Related Content

What's hot

次世代PHPフレームワーク Symfony2
次世代PHPフレームワーク Symfony2次世代PHPフレームワーク Symfony2
次世代PHPフレームワーク Symfony2Masao Maeda
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)Syed Umair
 
csc 208
csc 208csc 208
csc 208priska
 
Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)SHAMJITH KM
 
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...SAKSHISINGH486
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markersMicheal Ogundero
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointerGem WeBlog
 
Debugger Of Turbo C
Debugger Of Turbo CDebugger Of Turbo C
Debugger Of Turbo Cmohit2501
 
StackArray stack3
StackArray stack3StackArray stack3
StackArray stack3Rajendran
 
Ejercicios En Gambas
Ejercicios En GambasEjercicios En Gambas
Ejercicios En Gambasmafermen7
 

What's hot (20)

次世代PHPフレームワーク Symfony2
次世代PHPフレームワーク Symfony2次世代PHPフレームワーク Symfony2
次世代PHPフレームワーク Symfony2
 
week-18x
week-18xweek-18x
week-18x
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
week-4x
week-4xweek-4x
week-4x
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
csc 208
csc 208csc 208
csc 208
 
Matlab code for secant method
Matlab code for secant methodMatlab code for secant method
Matlab code for secant method
 
Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)
 
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointer
 
Debugger Of Turbo C
Debugger Of Turbo CDebugger Of Turbo C
Debugger Of Turbo C
 
Cinfo
CinfoCinfo
Cinfo
 
CalculateLoanPayments
CalculateLoanPaymentsCalculateLoanPayments
CalculateLoanPayments
 
Ruby mine referencecard
Ruby mine referencecardRuby mine referencecard
Ruby mine referencecard
 
StackArray stack3
StackArray stack3StackArray stack3
StackArray stack3
 
Switch statement mcq
Switch statement  mcqSwitch statement  mcq
Switch statement mcq
 
Ejercicios En Gambas
Ejercicios En GambasEjercicios En Gambas
Ejercicios En Gambas
 

Similar to Vs c# lecture7 2

Similar to Vs c# lecture7 2 (20)

Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Property Based Testing
Property Based TestingProperty Based Testing
Property Based Testing
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
 
C# Loops
C# LoopsC# Loops
C# Loops
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
3. control statement
3. control statement3. control statement
3. control statement
 
C#
C#C#
C#
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
07 flow control
07   flow control07   flow control
07 flow control
 
Loops
LoopsLoops
Loops
 
Java presentation
Java presentationJava presentation
Java presentation
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
Loops c++
Loops c++Loops c++
Loops c++
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 

More from Saman M. Almufti (20)

Lecture 7- domain name
Lecture  7- domain nameLecture  7- domain name
Lecture 7- domain name
 
Vp lecture 11 ararat
Vp lecture 11 araratVp lecture 11 ararat
Vp lecture 11 ararat
 
Vp lecture 10 ararat
Vp lecture 10 araratVp lecture 10 ararat
Vp lecture 10 ararat
 
Vp lecture 12 ararat
Vp lecture 12 araratVp lecture 12 ararat
Vp lecture 12 ararat
 
Lecture 6- http
Lecture  6- httpLecture  6- http
Lecture 6- http
 
Lecture 5- url-dns
Lecture  5- url-dnsLecture  5- url-dns
Lecture 5- url-dns
 
Vp lecture 7 ararat
Vp lecture 7 araratVp lecture 7 ararat
Vp lecture 7 ararat
 
Lecture 4- ip
Lecture  4- ipLecture  4- ip
Lecture 4- ip
 
Vp lecture 6 ararat
Vp lecture 6 araratVp lecture 6 ararat
Vp lecture 6 ararat
 
Vp lecture 5 ararat
Vp lecture 5 araratVp lecture 5 ararat
Vp lecture 5 ararat
 
Lecture 3- tcp-ip
Lecture  3- tcp-ipLecture  3- tcp-ip
Lecture 3- tcp-ip
 
Vp lecture 4 ararat
Vp lecture 4 araratVp lecture 4 ararat
Vp lecture 4 ararat
 
Vp lecture 3 ararat
Vp lecture 3 araratVp lecture 3 ararat
Vp lecture 3 ararat
 
Lecture 2- terminology
Lecture  2- terminologyLecture  2- terminology
Lecture 2- terminology
 
Vp lecture 2 ararat
Vp lecture 2 araratVp lecture 2 ararat
Vp lecture 2 ararat
 
Vp lecture1 ararat
Vp lecture1 araratVp lecture1 ararat
Vp lecture1 ararat
 
Lecture 1- introduction
Lecture  1- introductionLecture  1- introduction
Lecture 1- introduction
 
Vs c# lecture12
Vs c# lecture12Vs c# lecture12
Vs c# lecture12
 
Vs c# lecture11
Vs c# lecture11Vs c# lecture11
Vs c# lecture11
 
Vs c# lecture10
Vs c# lecture10Vs c# lecture10
Vs c# lecture10
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 

Vs c# lecture7 2

  • 1.
  • 2.
  • 3. C# LOOPS STATEMENTS There may be a situation, when you need to execute a block of code several number of times. In general, the statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. A loop statement allows us to execute a statement or a group of statements multiple times do { statement; } while(condition) For(start;stop;step) { statement; } while(condition) { statement; }
  • 4. For(Initial Value; Condition; Steps) { statements; } The For loop statement is used in C# to execute a code multiple times FOR LOOP
  • 5. private void button1_Click(object sender, EventArgs e) { for(int i=0;i<=4;i++) { MessageBox.Show("hello"); } } EXAMPLE 1 User for loop to show hello 5 times in a messagebox
  • 6. private void button1_Click(object sender, EventArgs e) { for(int i=1;i<=10;i++) { MessageBox.Show(i.ToString()); } } Use for loop to show from 1 to 10 in a messagebox EXAMPLE 2
  • 7. private void button1_Click(object sender, EventArgs e) { for(int i=1;i<=100;i++) { listBox1.Items.Add(i.ToString()); } } EXAMPLE 3 Use for loop to insert numbers from 1 to 100 to a list box
  • 8. private void button1_Click(object sender, EventArgs e) { for(char i='A';i<='Z';i++) { listBox1.Items.Add(i); } } EXAMPLE 4 Use for loop to insert upper case alphabet to a list box
  • 9. private void button1_Click(object sender, EventArgs e) { for(int i=100;i>=0;i--) { listBox1.Items.Add(i.ToString()); } } EXAMPLE 5 Use for loop to insert numbers from 100 to 1 to a list box
  • 10. private void button1_Click(object sender, EventArgs e) { for (int i=1;i<=100;i++) { if(i%2==0) { listBox1.Items.Add(i.ToString()); } } } EXAMPLE 6 Use for loop to insert even numbers from 1 to 100 to a list box
  • 11. private void button1_Click(object sender, EventArgs e) { for (int i=0;i<=100;i++) { if(i%5==0) { listBox1.Items.Add(i.ToString()); } } } EXAMPLE 7 Using for loop insert {0,5,10,15,20,15,30,35,…..100}to listbox private void button1_Click(object sender, EventArgs e) { for (int i=0;i<=100;i++) { listBox1.Items.Add(i.ToString()); } }
  • 12. while(condition) { statement(s); } The while statement continually executes a block of statements until a specified expression evaluates to false . The expression is evaluated each time the loop is encountered and the evaluation result is true, the loop body statements are executed. WHILE LOOP
  • 13. private void button1_Click(object sender, EventArgs e) { int i = 0; while(i<=4) { MessageBox.Show("hello"); i++; } } EXAMPLE 8 Use while loop to show hello 5 times
  • 14. private void button1_Click(object sender, EventArgs e) { int i = 1; while(i<=10) { if (i % 2 == 0) { listBox1.Items.Add(i.ToString()); } i++; } } EXAMPLE 9 Use while loop to add even number 1:10 to list box
  • 15. Do { statement(s); } while(condition) The Do while statement continually executes a block of statements until a specified expression evaluates to false . The expression is evaluated each time the loop is encountered and the evaluation result is true, the loop body statements are executed. DO WHILE LOOP
  • 16. private void button1_Click(object sender, EventArgs e) { int i = 0; do { MessageBox.Show("hello"); i++; } while (i <= 4); } EXAMPLE 10 User Do whileto show hello 5 times in a messagebox