General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Control structures
  3. Iteration (Loops)

While loops

While Loop in Computer Programming

The while loop is a fundamental control flow statement in computer programming that repeatedly executes a block of code as long as a given condition is true. It's used for iterative tasks when the number of iterations isn't known before the loop starts. The loop includes a condition that is evaluated before each iteration, and if the condition evaluates to false, the loop terminates and control passes to the next statement following the loop.

Example in Python

In Python, a while loop syntax is straightforward. Here's a basic example that prints numbers from 1 to 5.

counter = 1
while counter <= 5:
    print(counter)
    counter += 1

Example in C#

Similarly, in C#, a while loop can perform the same task. Below is a simple example that demonstrates iterating from 1 to 5.

int counter = 1;
while (counter <= 5)
{
    Console.WriteLine(counter);
    counter++;
}
PreviousFor loopsNextDo-While loops

Last updated 1 year ago

Was this helpful?