• Loops

    A loop in programming is like running around a track. When you start running, you keep going until you have gone around the track once. In programming, a loop is when you tell the computer to do the same thing over and over again until it has done what you told it to do. For example, if you want your computer to count from 1 to 10, it will create a loop that says "count 1, count 2, count 3" all the way up to 10.

      • For Loops

      • import std.stdio : writeln;
        
        void main()
        {
        	for (int key = 1; key < 4; key++) {
        		writeln(key); // 1, 2, 3
        	}
        }
        
      • For ...in Loops

      • import std.stdio : writeln;
        import std.string;
        import std.array;
        
        void main()
        {
        	string[string] fruits = ["a": "apple", "b": "banana", "c": "cherry"];
        
        	foreach (key, value; fruits) {
        		writeln(key, ": ", value); // c: cherry etc. (does not preserve order)
        	}
        }
        
      • While Loops

      • import std.stdio : writefln;
        
        void main()
        {
        	int times = 1;
        
        	while (times <= 5) {
        		writefln("Looped %d", times); // Looped 1 etc.
        		times += 1;
        	}
        }
        
      • Do-While Loops