The BFS loop runs while the queue is not empty:
while queue is not empty:
u = queue.pop_front()
for each neighbor v of u:
if not visited[v]:
visited[v] = true
dist[v] = dist[u] + 1
queue.push_back(v)
Each iteration removes the front node from the queue. Examine every neighbor of . If is already visited, skip it. If is unvisited, mark it visited, set dist[v] to dist[u] + 1, and add to the queue.
Repeat until the queue is empty. The distance array now holds the shortest distance to each node.