While Loops

While loops are control flow structures in programming that repeatedly execute a block of code as long as a specified condition is true. They are particularly useful when the number of iterations is not known in advance and depends on runtime conditions. While loops are commonly used for tasks such as user input validation, interactive programs, and implementing iterative algorithms. For example, in a game program, a while loop could be used to continuously prompt the user for input until a valid option is selected. Similarly, while loops are used in numerical computations, where iterations continue until a convergence criterion is met.

Select Languages

Examples

C

char* carList[] =
  {"Mazda", "Toyota", "Honda"};

int numItems =
  sizeof(carList) / sizeof(carList[0]);
  
int i = 0;
while(i < numItems) {
  printf(carList[i]);
  printf("\n");
  i++;
}

C#

using System.Collections.Generic;

List<string> carList = new List<string>();
carList.Add("Mazda");
carList.Add("Toyota");
carList.Add("Honda");
        
while(carList.Count > 0) {
  Console.WriteLine(carList[0] + "\n");
  carList.RemoveAt(0);
}

Console.WriteLine(carList.Count);

C++

#include <iostream>
#include <queue>
using namespace std;

queue<string> carList;
carList.push("Mazda");
carList.push("Toyota");
carList.push("Honda");
  
while(carList.size() > 0) {
  cout << carList.front() << endl;
  carList.pop();
}

cout << carList.size() << endl;

Go

import "fmt"

carList := []string{"Mazda", "Toyota", "Honda"}

for len(carList) > 0 {
  fmt.Println(carList[0])
  carList = carList[:0]
}

fmt.Println(len(carList))

Java

import java.util.ArrayList;

ArrayList<String> carList = new ArrayList<>();
carList.add("Mazda");
carList.add("Toyota");
carList.add("Honda");

while(carList.size() > 0) {
  System.out.println(carList.get(0) );
  carList.remove(0);
}

System.out.println(carList.size());

JavaScript

const carList = ['Mazda', 'Toyota', 'Honda'];

while (carList.length) {
  console.log(carList[0]);
  carList.shift();
}

console.log(carList.length);

Kotlin

val cars =
  mutableListOf<String>("Mazda", "Toyota", "Honda");

while(cars.count() > 0) {
  println("${cars[0]}\n");
  cars.removeAt(0);
}
println(cars.count())

MatLab

carList = ["Mazda", "Toyota", "Honda"];

while size(carList) > 0 
  fprintf(carList(1))
  carList(1)=[];
end

disp(isempty(carList));

PHP

$carList = array('Mazda', 'Toyota', 'Honda');

while (count($carList) > 0) {
  echo $carList[0]. "\n";
  array_shift($carList);
}

echo count($carList). "\n";

Python

carList = ['Mazda', 'Toyota', 'Honda']

while(len(carList) > 0):
  print(carList[0])
  carList.pop(0)
 
print(len(carList))

R

carList <- list("Mazda", "Toyota", "Honda")

while(length(carList) > 0) {
  print(carList[1])
  carList <- carList[-1]
}


print(length(carList))

Ruby

carList = Array["Mazda", "Toyota", "Honda"]

while carList.length > 0
  puts carList[0]
  carList.shift()
end

puts carList.length

Rust

use std::collections::VecDeque;

let mut carList = VecDeque::new();
carList.push_back("Mazda".to_string());
carList.push_back("Toyota".to_string());
carList.push_back("Honda".to_string());

while carList.len() > 0 {
  println!("{}", carList[0]);
  carList.remove(0);
}

println!("{}", carList.len());

Scala

import scala.collection.mutable.Queue;

var carList = Queue("Mazda", "Toyota", "Honda");

while(carList.length > 0) {
  println(carList.front);
  carList.dequeue();
}

println(carList.length)

Swift

var carList = ["Mazda", "Toyota", "Honda"];

while(carList.count > 0) {
  print(carList[0]);
  carList.removeFirst();
}

print(carList.count)

TypeScript

const carList:string[] =
  ['Mazda', 'Toyota', 'Honda'];

while (carList.length) {
  console.log(carList[0]);
  carList.shift();
}

console.log(carList.length);

Copyright 2025. All Rights Reserved. IronCodeMan.