PCP − Lecture 02

Fall 2020

Clément Aubert

August 17, 2020

Last Time - Presentation

Intro: Principles of Computer Programming:

Principles

  1. Commons principles at the core of every programming language.
  2. Always been and always will be (some studies since ancient history!).
  3. Ties with Mathematics.

Computer

  1. Particular, contingent.
  2. Evolving fast (parallel computing, quantum computing, etc.).
  3. Ties with Physics (circuits).

Programming

Basic building blocks that are used to construct any program (= software = app = application).

Generalities

On Computers

On Software

Specificity of C#

Workflow

 

Some of the specificity of C# is that:

  1. Everything will be done in Visual Studio (VS).
  2. Compiling is called “Build” and running is called “Debugging” or “Start without Debugging”

An object-oriented language

There are multiple paradigms (cf. https://en.wikipedia.org/wiki/Programming_paradigm), i.e. “families”, of languages. Object-oriented language is one of them, and Java, C#, Python, … are in that same family. The key concepts (that we will study at length) are:

History

A Simple Program

// Hello!

using System;
class Welcome
{
   static void Main()
   {
      Console.WriteLine("Welcome to PCP!");
   }
}
/*
 This is a multi-line
 comment
*/

Only one line actually does something, (Console.WriteLine("Welcome to PCP!");), the rest is “overhead”: for now, let’s take that to be a magical incantation, we’ll expound on that soon.

“Line by line” reading of the code:

Rules and Conventions

Rules

General rules for the syntax of C#:

  1. C# is case-sensitive.
  2. Every statement ends with a ;.
  3. Braces and parenthesis must be matched.

Oddly enough, C# does not care (almost at all) about spaces, but the programmer does: this is the difference between rules (use exact case, for instance) and conventions (like indentation, class name starting with an upper-case letter, …). Rules are required by the compilers, conventions are recommended by the programmers (and your instructors!).

There are also rules when we will be writing classes:

Conventions

Some conventions include:

Those are actually rules in Java!

Reserved Words and Identifiers

Rules For Identifiers

“Rules”: for the compiler.

An identifier…

  1. Must not be a reserved word.
  2. Must contain only letters, a → Z, digits, 0 → 9, underscore, _.
  3. Does not begin with digits.
  4. Does not contain any spaces.

Conventions For Identifiers

“Conventions”: for the programmers, common to many programming languages (some of Java’s rules are C# conventions).

  1. Identifiers should be descriptive.
  2. Class names starts with an upper-case letter.
  3. Pick names that are easy to memorize and type (ClASs is probably not a good choice).

Multiple Statements and Escape Sequences

Starting here, your instructor will draw at the board “Output window”, and will stop writing all the code, to focus on the Main method or notion at stake.

In our body, we can have multiple statements:

Console.WriteLine("Hey");
Console.WriteLine("Mom");

In addition to Console.WriteLine, we can also use Console.Write: the difference is that the former adds a new line after displaying the message, while the latter only displays the message, without adding a new line.

We can also use escape sequences for special characters:

All of these use the escape character (\).