01 Hello World
Programming fundamentals
       YP0616 - YP0601
  Elke Boonen & Tristan Vandevelde
                                     1
Before we start!
  Install Visual Studio on Windows
  (community or enterprise)
     https://visualstudio.microsoft.com/
     https://www.academicsoftware.eu/dashboard
     Install .NET desktop development:
     --> console applications using C#.
  Install Visual Studio on Mac
     https://visualstudio.microsoft.com/vs/mac/
     installation tutorial
                                                  2
Learning objectives (ECTS)
                      Basic principles (types, operators,
                      expressions) & structures (loop & if)
                      Arrays, lists, dictionaries
                      Methods and functions
                      Basic principles of OO
                      Files, in-and output IO
                      Exception handling
                                                              3
Learning materials
 Canvas LMS https://thomasmore.instructure.com/
    Presentations
    E-Book: Fundamentals of Computer Programming with C#
    Cheatsheet C#
    Assigments (CodeGrade)
 Online
    https://docs.microsoft.com/en-us/dotnet/csharp/
    https://github.com/ElkeBoonen/ProgrammingFundamentals (code from slides)
    https://github.com/ElkeBoonen/ProgrammingFundamentals-Students (code from class)
 Software
    Visual Studio (Community) https://visualstudio.Microsoft.com/                      4
Schedule
    Before autumn break            After autumn break
    01 Hello world                 07 Exception handling
    02 Variables & expression 08 Recap
    03 If-structures               09 Collections
    04 Loops                       10 Methods
    05 Files (IO)                  11 OO
    06 Arrays                      12 OO
                                   13 Exam prep
       Schedule is always subject to unexpected circumstances   5
Evaluation
 1st term
    Permanent Evaluation (30 %):
       CodeGrade exercises (each week, from week 02)
    Computer Exam (70 %) use of cheatsheet only!
 2nd term
    Computer Exam (100 %) use of cheatsheet only!
                                                       6
01 Hello world!
 Why programming?
 Why C#
 Hello World!
 Input/output
 CodeGrade
                    7
What is an algorithm
 A sequence of well-defined instructions
 to perform a special task
    Recipe
 Solve a problem or perform a
 computation
    I am hungry! So we need bread!
 Input to output
    From ingredients to bread
                                          8
What is a program?
 A series of instructions performed by a computer
 A program is always a combination of
    sequences
    selections
    loops
    methods
    ready-made objects
    objects you write yourself
                                                    9
What is a program?
 Coding = translating algorithm to code
 Programming = writing an algorithm in a
 programming language
 Programming is thinking!
 The written program is converted to machine
 language by
    Compiler: transfer full program (eg: C#, Java...)
    Interpreter: transfer line by line (eg: PHP, Python..)
    Assembler: transfer to zeros and ones
                                                             10
Execution of program
 Computer executes program step by step
 A computer is very strict
 Error in code = error in execution
                                          11
Why should you learn to program?
   “  Everybody in this country should learn
 to program a computer, because it teaches     https://www.youtube.com/embed
                                                /uL3047AJRgk?enablejsapi=1
              you how to think
               - Steve Jobs -
                                                                               12
From A# to Z++
                 Programming is only 50 years old
                 More than 700 programming languages!
                 Why is there not just one universal
                 language?
                                                       13
01 Hello world!
 Why programming
 Why C#
 Hello World!
 Input/output
 CodeGrade
                   14
Why C#
 It's not about the language,
 it't about the algorithm!
 But why C#?
    Object oriented
    Needs to be compiled
    Well documented
    Easier than C, C++ or Java
    Just a good starting point for learning to code
                                                      15
Visual Studio (community)
 It's just the best IDE (Integrated Development Environment) in town!
 Intellisense like code completion, quick info, member lists...
    always remember CTRL+Space, just saying...
 Download Visual Studio (Community) (also Mac-edition)
 https://visualstudio.microsoft.com/
 Install .NET desktop development: Build WPF, Windows Forms, and console
 applications using C#.
                                                                           16
01 Hello world!
 Why programming
 Why C#
 Hello World!
 Input/output
 CodeGrade
                   17
Hello to the world!
 First program!
    first program in any programming language
    just saying hi to the world!
 Easy peasy lemon squeezy
 Show text on screen
                                               18
First project
  Open Visual Studio
  Create a new project
     Choose Console.App (.Net Core) C#
     Mac-users : Apps - Console app
  Project name: Hello World
     Location: choose a location!
     Solution: Create new solution
     ✔ Place solution and project in the same directory
  Framework .NET 6.0
     ✔ Do not use top-level statements
     Mac-users: choose .NET 3.1
                                                          19
Take a look at the code
  1   using System;
  2
  3   namespace HelloWorld                             Hit F5 or
  4   {
  5       internal class Program
  6       {
  7           static void Main(string[] args)
  8           {
  9               Console.WriteLine("Hello World!");   Terminal opens
 10           }
 11       }                                            (press any key to close)
 12   }
 Hello World!
                                                                                  20
Putting it out there
                                          1   using System;
  Prints text to output screen            2
                                          3   namespace HelloWorld
  Text must be placed between " "         4   {
                                          5       class Program
  Every line of simple code must end      6       {
                                          7           static void Main(string[] args)
  with semicolon ;                        8           {
                                          9               Console.WriteLine("Hello World!");
  C# is case sensistive!                 10
                                         11       }
                                                      }
                                         12   }
        Try Console.Write(), what happens?
                                                                                          21
Everything starts in Main
                                        1   using System;
                                        2
                                        3   namespace HelloWorld
 Main-method                            4   {
                                        5       class Program
 Execution starts here!                 6
                                        7
                                                {
                                                    static void Main(string[] args)
                                        8           {
 Main-method = code block               9               Console.WriteLine("Hello World!");
                                       10           }
 Everything inside block code          11       }
                                       12   }
 is surrounded by braces { }
 Don't use ; after declaration of method or after { }
 Main is mandatory in every C#-program
 Everything inside Main will get executed
                   Add some extra writelines in Main, what happens?
                                                                                             22
The class is half full
   1   using System;
   2
   3   namespace HelloWorld
   4   {
   5       internal class Program
   6       {
   7           static void Main(string[] args)
   8           {
   9               Console.WriteLine("Hello World!");
  10           }
  11       }
  12   }
  Creates a class named Program!
       internat = limited to the assembly in which it is declared
  C# is object-oriented so creating a class is mandatory!
  Every class is a code block, so code in class is surrounded by braces { }
  Don't use ; after declaration of class or after { }                         23
To namespace and beyond
  1   using System;
  2
  3   namespace HelloWorld
  4   {
  5       class Program
  6       {
  7           static void Main(string[] args)
  8           {
  9               Console.WriteLine("Hello World!");
 10           }
 11       }
 12   }
 Creates a namespace HelloWorld (container for all associated classes)
 A namespace is a code block, so content is surrounded by braces { }
 Don't use ; after declaration of namespace or after { }
                                                                         24
To use or not to use
   1   using System;
   2
   3   namespace HelloWorld
   4   {
   5       class Program
   6       {
   7           static void Main(string[] args)
   8           {
   9               Console.WriteLine("Hello World!");
  10           }
  11       }
  12   }
  Without using System we would write System.Console.WriteLine
  The using-statement imports all functionality from the System namespace
  which we need to print text to a screen.
                                                                            25
Computer says no
  Your program won't run!
      Message: there where build errors
      Hit NO
  Red wavy lines indicate the error
  Start debugging by
  reading the description of the error
  Try to fix it! Test and re-test
“ Breathe in, breathe out, in any case never hit your computer!
                                                                  26
What's in a project
                  Find the project in file explorer
                      RMB project name in solution explorer,
                      select Open folder in File explorer )
                  HelloWorld.sln (solution le VS)
                  Program.cs (actual code)
                  bin (binary files, executable code)
                  obj (object files, temporary to build binary)
                                                                 27
Execute, make it happen!
Execute HelloWorld outside VS
   Open executable in file explorer
        navigate bin, debug, netcoreapp3.1, double-click HelloWorld.exe
        Look closely, because it closes quickly.. Why?
   Execute in terminal, open Powershell
   1   > cd <navigate to project folder>
   2   > cd bin
   3   > cd .\Debug\
   4   > cd .\netcoreapp3.1\
   5   > .\HelloWorld.exe
   6   Hello World!
                                                                          28
01 Hello world!
 Why programming
 Why C#
 Hello World!
 Input/output
 CodeGrade
                   29
What is a variable?
 Container for storing data values
 Why do we need variables?
    To store information given by the user as user input
    To calculate and store intermediate results
    To store information for later use
    ...
                                                           30
Creating a variable
 C# strongly typed variables: variable must be declared before using
 What is declaring a variable?
    give your variable a name
    give your variable a type: C# has different primitive types!
 What is initializing a variable?
    give your variable its first value
 Declaring and initializing can be done at the same time!
                                                                       31
String
 C# type for text = string
    text-value between " "
 Always declare variable before use
    type + name
 Value of variable can change!
               1   string text;
               2   text = "some text";
               3
               4   string name = "Elke";
               5   name = "Jan"
                                           32
Everything in the console is a string!
  Everything on the command line is a piece of text = a string!
      write to console = Console.WriteLine()
          different ways of concatenating strings
      read user input from console = Console.ReadLine()
          store value given by the user in a variable for later use!
 1   Console.WriteLine("What's your name?");                           What's your name?
 2
 3   string name = Console.ReadLine(); ;                               Elke
 4   Console.WriteLine($"Hello {name}!");                              Hello Elke!
 5   Console.WriteLine("Hello " + name + "!");
 6   Console.WriteLine("Hello {0}!", name);                            Hello Elke!
                                                                       Hello Elke!
                                                                                           33
"No comment" is a comment
  A comment is an explanation or annotation in the code
  They are added to clarify code and are ignored by the compiler
 1 //this is one line of comment
 2
 3 /*
 4 These are mulitple lines of comment!
 5 */
                                                                   34
01 Hello world!
 Why programming
 Why C#
 Hello World!
 Input/output
 CodeGrade
                   35
We ❤ CodeGrade, yes we do!
 Find the assignments in Canvas
 Spend some hours to do them (plagiarism results in a 0!)
 Submit (only) your .cs-file
 Wait for your automatically generated result
 Hit a home run? Do a little dance ;)
 Not so successful? Tweak your solution and re-submit!
 You can keep practicing until the deadline to become better, but also to score higher
 points on your permanent evaluation
                                                                                         36
01 Test CodeGrade
 Go to Modules or Assignments
 Find 01 Test CodeGrade
 READ the assignment!
 Create new project and solve assignment!
 Upload program.cs-file of project
                                            37
Practice makes perfect!
 Do your exercises, spend the hours!
 The better the exercises, the better the exam!
 Say what? How many hours?
 6 SP = 6 * 28 hours = 168 hours
 Lessons = 12 * 4 hours = 48 hours
 Exam = 2 hours
 Exercise = 168-48-2 = 118 hours
                                                  38