C# Loops Summary

Contents

keyboard_tab
Intro & Overview
1
Introduction
PREVIEW1m 14s
2
Overview
PREVIEW39s
Loops
5
Lists
1m 23s
Course Conclusion

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
23m
Students
50
Ratings
5/5
starstarstarstarstar
Description

Structures to repeat tasks or access data collections are at the heart of compact and reusable code. C#, like other programming languages, provides three basic mechanisms allowing you to execute statements multiple times dynamically. In this course, we'll take an in-depth look at the for loop, while loop, and foreach loop.

These looping mechanisms vary in structure and intended application, and we'll examine how to use them in different scenarios. In the course of this detailed investigation, you will learn some of the potential pitfalls that accompany programming with each type of loop, how to avoid them, and create efficient loops. There are code examples demonstrating the use of each loop type in a practical way, along with other helpful C# and .NET code snippets.

Learning Objectives

  • An in-depth understanding of for loops
  • Learn about while loop syntax and see how they are used
  • Gain a foundational understanding of object lists
  • Learn how to use, and not to use, a foreach loop to iterate through a list

Intended Audience

This course is intended for those who already have a basic understanding of C# and want to learn about loops.

Prerequisites

To get the most out of this course, you should have a basic knowledge of C# as well as an understanding of object orientation and C# classes. Please consider taking our Introduction to Object Orientation and C# Classes course before taking this one.

 

Source Code

Source code related to the lectures can be found here

Transcript

A for loop uses a numeric index variable initialized to any value, usually zero.  While that variable does not meet some boundary condition, typically does not exceed the size of an array, statements inside the loop are repeated. Each iteration of the loop adjusts the index variable by some amount according to the iterator statement.  Even though for loops are incredibly flexible, allowing the adjustment of the boundary comparison value and the index variable within the loop's body, their primary function is to access elements in an array incrementing sequentially by one.

A while loop says continue performing the statements in the loop's body while some Boolean condition holds true. While loops are generally not used for repeating tasks a predefined number of times, but for repeating actions until some event occurs. While loops are often employed when monitoring or polling events like waiting for user input. 

The foreach loop is simple, convenient, and efficient but lacks the flexibility of a for-loop. It accesses all elements in a list in a forward direction, not allowing any list structure modification within the loop. While this is slightly more efficient, the main convenience is having a variable of the list's data type available for use within the loop.

About the Author
Avatar
Hallam Webber
Software Architect
Students
13332
Courses
26
Learning Paths
3

Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a  Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard. 

Covered Topics