---
title: CSCI 1301 -- Lab 23
author: Clément Aubert
lang: en
fontsize: 10pt
documentclass: scrartcl
numbersections: true
links-as-notes: true
papersize: letter
geometry: margin=1in
keywords:
- Computer Science
- Problem solving methods
- Syllabus
- Augusta University
- Bachelor of Science
---

# From `while` to `for`

Rewrite the following `while` (or `do...while`) loops as `for` loops.

~~~~~~~{.cs .numberLines .lineAnchors}
int a = 0;
while (a != 10)
{
    Console.WriteLine(a);
    a++;
}
~~~~~~~

~~~~~~~{.cs .numberLines .lineAnchors}
int b = 3;
while (b >= -2)
{
    Console.WriteLine(b);
    b -= 2;
}
~~~~~~~

~~~~~~~{.cs .numberLines .lineAnchors}
int c = 10;
while(c <= 100) {
    Console.WriteLine(c);
    c += 10;
}
~~~~~~~

~~~~~~~{.cs .numberLines .lineAnchors}
int d = 1;
do
{
    Console.WriteLine(d);
    d *= 2;
} while (d <= 100);
~~~~~~~

# From `for` to `while`

Rewrite the following `for` loops as `while` loops:

~~~~~~~{.cs}
for (int e = 10; e <= 100; e += 10) Console.Write(e + " ");
~~~~~~~

~~~~~~~{.cs}
for (double f = 150; f > 2; f/=2 ) Console.Write(f + " ");
~~~~~~~

~~~~~~~{.cs}
for (char g = 'A' ; g !='a'; g = (char)((int)g +1) ) Console.Write(g + " ");
~~~~~~~

~~~~~~~{.cs}
for (int h = 0; h > -30; h -= 1) Console.Write(h + " ");
~~~~~~~

# Pushing Further (Optional)

This lab's pushing further is about two modifications of `for` loops that are sometimes considered as bad design: used poorly, they can make the code harder to read, to debug, and sometimes makes it hard to follow the flow of control of your program.
They are introduced because you may see them in your future, but, except for rare cases, should be avoided completely.

## Multiple Initializations and Updates

The exact structure of `for` loops is actually more complex than what we discussed in class.
It is

~~~~~~~{.cs}
for(<initializations>; <condition>; <updates>)
{
    <statement block>
}
~~~~~~~

That is, there can be more than one initialization (but only if the variables all have the same datatype) and more than one update.
That is, are legal statements like:

~~~~~~~{.cs}
for(int z = 0,  y = 10; z < y ; z++){ Console.WriteLine($"{z} + {y} = {z+y}"); }
~~~~~~~

or

~~~~~~~{.cs}
for (int x = 0, y = 12 ; x != y; x++, y--)
    Console.WriteLine($"The difference between {x} and {y} is {x - y}");
~~~~~~~

Also, the initialization, as well as the update condition, are actually optional: we could have

~~~~~~~{.cs}
int w = 0;
for (; w < 5; w++) { Console.WriteLine(w); }
~~~~~~~

and

~~~~~~~{.cs}
for(int r = 10; r > 0;) { Console.WriteLine(r--); }
~~~~~~~

Try to rewrite the four `for` loops just given as four `for` loops with exactly one initialization and one update in the header of the `for` loop.

## `continue` and `break`

Programmers can use two keywords in loops, `continue` and `break`, that modify the control flow.
Looking at the following code, try to understand what those statements do.

~~~~~~~{.cs}
for (int i = 1; i <= 5; i++)
{
    if (i == 3) continue;
    Console.Write(i + " ");
}
~~~~~~~


~~~~~~~{.cs}
for (int i = 1; i <= 5; i++)
{
    if (i == 3) break;
    Console.Write(i + " ");
}
~~~~~~~

You can also use `break` and `continue` in `while` loops.
Try to rewrite the previous two `for` as `while` loops: there is a trick to make the `while` loop using `break` works properly, can you spot it?



## Default values

Execute the following:

~~~~~~~{.cs}
int[] ar = new int[5];
ar[0] = 5;
for (int i = 0; i < ar.Length; i++)
    Console.WriteLine(ar[i]);
~~~~~~~

What can you conclude about the value of the cells that were not assigned?
