C# Programming: Syntax, I/O, Conditions, Loops and Debugging, Lecture notes of Computer Science

An introduction to the basic syntax of C# programming language, including Input/Output operations, Conditions, Loops, and Debugging techniques. It covers the use of variables, Console I/O, comparison operators, if-else statements, logical operators, loops, and debugging. It also includes examples and exercises to help readers understand the concepts.

Typology: Lecture notes

2020/2021

Uploaded on 07/19/2021

geese-2
geese-2 🇻🇳

5

(1)

2 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Basic Syntax
Basic Syntax , I/O, Conditions, Loops and Debugging
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download C# Programming: Syntax, I/O, Conditions, Loops and Debugging and more Lecture notes Computer Science in PDF only on Docsity!

Basic Syntax

Basic Syntax , I/O, Conditions, Loops and Debugging

  • Introduction and Basic Syntax
  • Input / Output
  • Comparison operators
  • The if-else / Switch-Case Statement
  • Logical Operators
  • Loops
  • Debugging and Troubleshooting Table of Contents 2
  • C# is modern, flexible, general-purpose

programming language

  • Object-oriented by nature, statically-typed, compiled
  • Runs on .NET Framework / .NET Core C# – Introduction 4 static void Main() { //Source code }

Program

starting

point

  • Visual Studio (VS) is powerful IDE for C#
  • Create a

console

application

Using Visual Studio 5

  • Defining and Initializing variables
  • Example: Declaring Variables 7 {data type / var} {variable name} = {value}; int number = 5; Data type Variable name Variable value

CONSOLE I/O

Reading from and Writing to the Console 8

  • Console.ReadLine() returns a string
  • Convert the string to number by parsing: Converting Input from the Console 10 string name = Console.ReadLine(); int age = int.Parse(Console.ReadLine()); double salary = double.Parse(Console.ReadLine()); bool isHungry = bool.Parse(Console.ReadLine());
  • We can print to the console, using the Console class
  • Use the System namespace to access System.Console

class

  • Writing output to the console:
    • Console.Write()
    • Console.WriteLine() Printing to the Console 11 Console.Write("Hi, "); Console.WriteLine("John!"); // Hi, John!
  • D – format number to certain digits with leading zeros
  • F – format floating point number with certain digits after the decimal point
  • Examples: Formatting Numbers in Placeholders double grade = 5.5334; int percentage = 55; Console.WriteLine("{0:F2}", grade); // 5. Console.WriteLine("{0:D3}", percentage); // 055

▪ Using string interpolation to print on the console ▪ Examples: Using String Interpolation string name = "George"; int age = 5; Console.WriteLine($"Name: {name}, Age: {age}"); //Name: George, Age 5 Put $ in front of the string to use string interpolation

COMPARISON OPERATORS

Comparison Operators 17

Operator Notation in C#

Equals ==

Not Equals !=

Greater Than >

Greater Than or Equals >=

Less Than <

Less Than or Equals <=

THE IF-ELSE STATEMENT

Implementing Control-Flow Logic

  • The most simple conditional statement
    • Test for a condition
  • Example: Take as an input a grade and check if the student has passed the exam (grade >= 3.00) The If Statement 20 double grade = double.Parse(Console.ReadLine()); if (grade >= 3.00) { Console.WriteLine("Passed!"); } In C# the opening bracket stays on a new line