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
Php and MySQL
PDF
4 pillars of OOPS CONCEPT
PPTX
Super keyword in java
DOCX
Abstraction in c++ and Real Life Example of Abstraction in C++
PPTX
Collections and its types in C# (with examples)
PPTX
Exception handling c++
PPTX
Procedure and Functions in pl/sql
C# Loops
Php and MySQL
4 pillars of OOPS CONCEPT
Super keyword in java
Abstraction in c++ and Real Life Example of Abstraction in C++
Collections and its types in C# (with examples)
Exception handling c++
Procedure and Functions in pl/sql

What's hot (20)

PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPT
Java Networking
PPSX
Complete C++ programming Language Course
PPT
Introduction to JavaScript
PPTX
Stack and Queue
PDF
JavaScript - Chapter 12 - Document Object Model
PPTX
Intro to Javascript
PPT
FUNCTIONS IN c++ PPT
PPT
Php with MYSQL Database
PPT
sets and maps
PPTX
Control flow statements in java
PPTX
Statements and Conditions in PHP
PPTX
Python OOPs
PPSX
Modules and packages in python
PDF
CLASS & OBJECT IN JAVA
PDF
Php introduction
PPT
Java And Multithreading
PPTX
Exception handling
PPTX
String Handling in c++
Basic Concepts of OOPs (Object Oriented Programming in Java)
Java Networking
Complete C++ programming Language Course
Introduction to JavaScript
Stack and Queue
JavaScript - Chapter 12 - Document Object Model
Intro to Javascript
FUNCTIONS IN c++ PPT
Php with MYSQL Database
sets and maps
Control flow statements in java
Statements and Conditions in PHP
Python OOPs
Modules and packages in python
CLASS & OBJECT IN JAVA
Php introduction
Java And Multithreading
Exception handling
String Handling in 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
Autodesk AutoCAD Crack Free Download 2025
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
history of c programming in notes for students .pptx
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Patient Appointment Booking in Odoo with online payment
PPTX
Operating system designcfffgfgggggggvggggggggg
Autodesk AutoCAD Crack Free Download 2025
Why Generative AI is the Future of Content, Code & Creativity?
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Computer Software and OS of computer science of grade 11.pptx
Wondershare Filmora 15 Crack With Activation Key [2025
history of c programming in notes for students .pptx
Designing Intelligence for the Shop Floor.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Digital Systems & Binary Numbers (comprehensive )
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
CHAPTER 2 - PM Management and IT Context
Odoo Companies in India – Driving Business Transformation.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
17 Powerful Integrations Your Next-Gen MLM Software Needs
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Patient Appointment Booking in Odoo with online payment
Operating system designcfffgfgggggggvggggggggg

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: [email protected]