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 and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 32 : 43
Object-oriented Programming in C#
Introduction to C#
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

A Visual Basic Program with a while loop.

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
  }
}

The similar C# program with a while loop.

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

A Visual Basic Program with a for loop.

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
  }
}

The similar C# program with a for loop.

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

A Visual Basic Program with a Do Loop.

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);
    }
  }
}

The similar C# program with a similar for and a break.