Skip to Main Content

Workshops

Workshop

C# Workshop Terminology and Activity

Programs Needed: Visual Studio

Visual Studio is an integrated development environment from Microsoft. It is used to develop computer programs, as well as websites, web apps, web services and mobile apps. You will need it to complete the example in the workshop.


C# Workshop Terminology

Console.Readkey(); : tells the program to wait for input before continuing 

Variables: stores values, which can be named

  • Int - stores integers (whole numbers)
  • String - a series of text found within double quotes “”
  • var++ - adds 1 to the variable
  • var+= - adds a number to the variable

If statements: tells the program to look for a specific case

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the previous condition is false
  • Use && to specify two conditions in the same If statement that both have to be true
    • Greater than    (example: a > b)
    • Less than        (example: a < b)
    • Greater than or equal to   (example: a >= b)
    • Less than or equal to      (example: a <= b)
    • Equal to   (example: a == b)

For loops: use to run the same code a set multiple of times


FizzBuzz Activity

Print all the numbers from 1 to 100. If the number is divisible by 3, print "Fizz" instead.  If the number is divisible by 5, print "Buzz" instead. If the number is divisible by both 3 AND 5, print "FizzBuzz" instead.

 

Setting up our loop and counting out our numbers

Using a for loop, set the int variable to 1 then direct the program to continue until the number is less than 101. Finally, set int to increase by 1 each time. 

Your end result will look like this:

for (int i = 1; i < 101; i++)
Setting up our “Fizz” and “Buzz” checks

Following the code comment, our next step is to check to see if the number is divisible by 3 and to add Console.WriteLine(“Fizz”) in your curly brackets. To do this, compare the variable to 3 by using %, which is the dividing modular operator. To check that the number divides equally with a remainder of 0, add == 0 to the if statement. Additionally, add an else statement to prevent the program from printing the number that is divisible by 3.

Copy the first if statement and paste it directly below it. Change the if statement to an else if statement, replace 3 with 5 and replace “Fizz” with “Buzz”

Your end result will look like this: 

for (int i = 1; i < 101; i++)
{
   if (i % 3 == 0)
       {
                Console.WriteLine(“Fizz”);
       }
else if (i % 5 == 0)
       {
                 Console.WriteLine(“Buzz”);
       }

      else
       {
                  Console.WriteLine(i);
        }
}

Setting up our “FizzBuzz” check

Add another else if statement to your program. Since the program will only run the first statement that applies to your variable, add the new statement above the others. This one will check to see if the number is divisible by both 3 and 5. Add Console.WriteLine(“FizzBuzz”); to your else if statement as the result. 

Your end result will look like this:

for (int i = 1; i < 101; i++)
{
else if (i % 3 == 0 && i % 5 ==0)
          {
                     Console.WriteLine(“FizzBuzz”);
          }
   if (i % 3 == 0)
          {
                     Console.WriteLine(“Fizz”);
          }
else if (i % 5 == 0)
         {
                      Console.WriteLine(“Buzz”);
          }

          else
          {
                      Console.WriteLine(i);
          }
}

Congratulations! You have successfully written your first program!