# for and while loops — Bash / Shell Scripting

Source: https://www.geekswithgeeks.com/en/bash/bash-loops

> Explain for and while loops, run a small example, and check the result.

## The essential idea

**for and while loops** is easiest when you first identify its job. Name the input, the change, and the result before memorising syntax. Small explicit steps make programs easier to repair later.

## Place this on the learning path

This idea is one part of the path from a source file to useful output.

![Bash / Shell Scripting source code moving through its runtime or interpreter to output.](assets/figures/bash/bash-learning-map.svg) — Figure 3.1 — Keep the complete program path in mind.

## A common trap

Do not trust code just because it looks familiar. For **for and while loops**, change one value, run the smallest example, and compare the result with your prediction.

Quick check

**Quiz:** What is the best first practice for for and while loops?

- [x] Run one small, focused example.
- [ ] Memorize syntax without testing it.
- [ ] Ignore error messages.

*Answer:* Run one small, focused example.. A small experiment gives direct feedback about cause and effect.
