SlideShare a Scribd company logo
C# loops
Loops
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopb
aabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaa
bte/
+91 9746854752
Loops enable us to execute a code block repeatedly for any number of times that
we want
In C# there are 4 different variants for loops
The “while” loop
The “do” loop
The “for” loop
The “foreach” loop
The “while” loop
The while loop simply executes a block of code as long as the condition we give it
is true
When the program runs we will get a listing
of numbers, from 0 to 4.
The number is first defined as 0, and each
time the code in the loop is executed, it's
incremented by one. When the number
reaches 5 , the condition (number < 5)
evaluates to false and the loop gets exited.
The condition of the while loop is evaluated
before the control enters the code block
The “do” loop
The do loop evaluates the condition after the loop has executed, which makes
sure that the code block is always executed at least once
The output is the same - once the number is
more than 5, the loop is exited.
The “for” loop
The for loop is preferred when we know how many iterations we want, either
because we know the exact amount of iterations, or because we have a variable
containing the amount.
This produces the exact same
output, but the for loop is a bit more
compact. It consists of 3 parts - we
initialize a variable for counting, set up
a conditional statement to test it, and
increment the counter.
The first part, where we define the “i”
variable and set it to 0, is only
executed once, before the loop starts.
The last 2 parts are executed for each
iteration of the loop. The number is
incremented after a loop.
The “foreach” loop
The foreach loop operates on collections of items, for instance arrays or other
built-in list types.
The “foreach” loop – contind.
We create an instance of an ArrayList, and then we add some string items to it. We
use the foreach loop to run through each item, to output the data in the string
item to the console window.
It is always needed to tell the foreach loop which datatype we are expecting to
pull out of the collection. In case we have a list of various types, we may use the
object class instead of a specific class, to pull out each item as an object.
foreach loop is simpler than any of the other loops when we are looping in
collections.
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

PPTX
C# Loops
PPSX
Exception Handling
PPT
Packages and interfaces
PPTX
C# Delegates
PPS
Wrapper class
PDF
Java I/o streams
PPTX
String, string builder, string buffer
PPTX
Java swing
C# Loops
Exception Handling
Packages and interfaces
C# Delegates
Wrapper class
Java I/o streams
String, string builder, string buffer
Java swing

What's hot (20)

PDF
Arrays in Java
PPT
C# basics
PPTX
Stack using Array
PPTX
Function C programming
PPTX
classes and objects in C++
PPT
Java awt
PPTX
Pointers in c++
PPTX
Regular expressions in Python
PPTX
Methods in java
PPT
Thread model in java
PDF
Java Thread Synchronization
PPTX
Conditional and control statement
PPTX
C# classes objects
PPT
JDBC – Java Database Connectivity
PPTX
Multi-threaded Programming in JAVA
PPT
Object Oriented Programming Concepts using Java
PPTX
Python: Modules and Packages
PPT
Java And Multithreading
PDF
Object-oriented Programming-with C#
Arrays in Java
C# basics
Stack using Array
Function C programming
classes and objects in C++
Java awt
Pointers in c++
Regular expressions in Python
Methods in java
Thread model in java
Java Thread Synchronization
Conditional and control statement
C# classes objects
JDBC – Java Database Connectivity
Multi-threaded Programming in JAVA
Object Oriented Programming Concepts using Java
Python: Modules and Packages
Java And Multithreading
Object-oriented Programming-with C#
Ad
Ad

Similar to C# loops (20)

PDF
Notes2
 
PPTX
Python if_else_loop_Control_Flow_Statement
PDF
Python Flow Control
PPTX
Chapter08.pptx
PPTX
Loops in java script
PPTX
While loop
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPTX
PPTX
Lesson 6 Understanding Loops and Conditional Statements.pptx
 
PDF
Chapter 12 Computer Science ( ICS 12).pdf
PPTX
Presentation on nesting of loops
PPT
Control structures pyhton
DOCX
Coding in Disaster Relief - Worksheet (Advanced)
PPT
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PPS
Programming in Arduino (Part 2)
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
PPTX
Unit - 2 CAP.pptx
PPTX
JAVA LESSON-02.pptx
PPTX
Python Functions.pptx
 
Notes2
 
Python if_else_loop_Control_Flow_Statement
Python Flow Control
Chapter08.pptx
Loops in java script
While loop
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
Lesson 6 Understanding Loops and Conditional Statements.pptx
 
Chapter 12 Computer Science ( ICS 12).pdf
Presentation on nesting of loops
Control structures pyhton
Coding in Disaster Relief - Worksheet (Advanced)
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
Programming in Arduino (Part 2)
Unit 1- Part-2-Control and Loop Statements.pdf
Unit - 2 CAP.pptx
JAVA LESSON-02.pptx
Python Functions.pptx
 

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
ai tools demonstartion for schools and inter college
PPTX
Transform Your Business with a Software ERP System
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
ai tools demonstartion for schools and inter college
Transform Your Business with a Software ERP System
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Online Work Permit System for Fast Permit Processing
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Choose the Right IT Partner for Your Business in Malaysia
How Creative Agencies Leverage Project Management Software.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Nekopoi APK 2025 free lastest update
Operating system designcfffgfgggggggvggggggggg
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Softaken Excel to vCard Converter Software.pdf
top salesforce developer skills in 2025.pdf
CHAPTER 2 - PM Management and IT Context
Which alternative to Crystal Reports is best for small or large businesses.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Odoo Companies in India – Driving Business Transformation.pdf
ISO 45001 Occupational Health and Safety Management System
ManageIQ - Sprint 268 Review - Slide Deck

C# loops

  • 3. Loops enable us to execute a code block repeatedly for any number of times that we want In C# there are 4 different variants for loops The “while” loop The “do” loop The “for” loop The “foreach” loop
  • 4. The “while” loop The while loop simply executes a block of code as long as the condition we give it is true When the program runs we will get a listing of numbers, from 0 to 4. The number is first defined as 0, and each time the code in the loop is executed, it's incremented by one. When the number reaches 5 , the condition (number < 5) evaluates to false and the loop gets exited. The condition of the while loop is evaluated before the control enters the code block
  • 5. The “do” loop The do loop evaluates the condition after the loop has executed, which makes sure that the code block is always executed at least once The output is the same - once the number is more than 5, the loop is exited.
  • 6. The “for” loop The for loop is preferred when we know how many iterations we want, either because we know the exact amount of iterations, or because we have a variable containing the amount. This produces the exact same output, but the for loop is a bit more compact. It consists of 3 parts - we initialize a variable for counting, set up a conditional statement to test it, and increment the counter. The first part, where we define the “i” variable and set it to 0, is only executed once, before the loop starts. The last 2 parts are executed for each iteration of the loop. The number is incremented after a loop.
  • 7. The “foreach” loop The foreach loop operates on collections of items, for instance arrays or other built-in list types.
  • 8. The “foreach” loop – contind. We create an instance of an ArrayList, and then we add some string items to it. We use the foreach loop to run through each item, to output the data in the string item to the console window. It is always needed to tell the foreach loop which datatype we are expecting to pull out of the collection. In case we have a list of various types, we may use the object class instead of a specific class, to pull out each item as an object. foreach loop is simpler than any of the other loops when we are looping in collections.
  • 9. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 10. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com