# The Mathematica Programming Language

## While Loop Example Program

**Click below to go directly to a specific section:**

Description | Source Code
| Sample Run | Program Notes

## Description

This program begins by creating a random vector. This random vector is given the size of 4 and a range for its contents of 0 to 1. This random vector is set equal to a. The while loop counts the number of runs of 0 or 1 and appends its result to a list. The output will be a list of the number of runs in the vector.

## Source Code

Clear[uRandomVector,a]

uRandomVector[size_,{min_,max_}]:=Table[Random[Integer,{min,max}],{size}]

a=uRandomVector[4,{0,1}];

Clear[i,x,y,cnt,ls]

cnt=0;

ls={};

i=1;

While[(i<=Length[a]),x=a[[i]];

While[((i<=Length[a])&&(a[[i]]==x)),cnt+=1;i+=1];

ls=Append[ls,cnt];

cnt=0]

a

ls

## Sample Run

{1,0,0,1}

{1,2,1}

## Program Notes

This program was tested and executed using Mathematica 3.0

*Last modified: 01:51 PM on 12/10/1999*