Lecture overview -- Keyboard shortcut: 'u'  Previous page: Control structures for Selection -- Keyboard shortcut: 'p'  Next page: Control structures for iteration -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Alphabetic index  Help page about these notes  Course home    Introduction to C# - slide 32 : 43

Control Structures for Iteration
Module WhileDemo

    Sub Main()
      Dim large As Integer = 106, small As Integer = 30
      Dim remainder As Integer

      While small > 0
        remainder = large Mod small
        large = small
        small = remainder
      End While

      Console.WriteLine("GCD is {0}", large)   ' Prints 2
    End Sub
End Module
using System;
class WhileDemo{

  public static void Main(){
    int large = 106, small = 30, remainder;

    while (small > 0){
      remainder = large % small;
      large = small;
      small = remainder;
    }

    Console.WriteLine("GCD is {0}", large);   // Prints 2
  }
}
Module ForDemo

    Sub Main()
      Dim sum As Integer = 0

      For i as Integer = 1 To 10
        sum = sum + i
      Next i

      Console.WriteLine("The sum is {0}", sum)   ' Prints 55
    End Sub
End Module
using System;
class ForDemo{

  public static void Main(){
    int sum = 0;

    for(int i = 1; i <= 10; i++)
      sum = sum + i;

    Console.WriteLine("The sum is {0}", sum);  // Prints 55
  }
}
Option Strict On
Option Explicit On
Module DoDemo

    Sub Main()
      Const PI As Double = 3.14159
      Dim radius As Double, area As Double

      Do 
        radius = Cdbl(InputBox("Type radius"))
        If radius < 0 Then
          Exit Do
        End If
        area = PI * radius * radius 
        Console.WriteLine(area)
      Loop

    End Sub
End Module
using System;
class ForDemo{

  public static void Main(){
    const double PI = 3.14159;
    double radius, area;

    for(;;){
      radius = double.Parse(Console.ReadLine());
      if (radius < 0) break;
      area = PI * radius * radius;
      Console.WriteLine(area);
    }
  }
}