Mindoro State University Introduction to Programming Languages PDF
Document Details
Uploaded by EndearingMercury541
Mindoro State University
Jozel Mina M. Dacanay
Tags
Summary
This Mindoro State University document introduces programming concepts, with a focus on the C# programming language. It gives historical context to programming languages. The document provides detailed explanations and code examples.
Full Transcript
Mindoro State University College of Computer Studies Introduction to Programming Languages Prepared by: JOZEL MINA M. DACANAY Instructor I, College of Computer Studies E-mail Address: [email protected] IT...
Mindoro State University College of Computer Studies Introduction to Programming Languages Prepared by: JOZEL MINA M. DACANAY Instructor I, College of Computer Studies E-mail Address: [email protected] ITP 222 : QUANTITATIVE METHODS Bachelor of Science in Information Technology College College of of Computer Computer Studies Studies At the end of the lesson, the learners should be able to: 1. gained knowledge on the origin of programming language 2. familiarized oneself on the proper use of data types, keywords, variables, constants, expressions and operators 3. memorized the parts of the Environment of the program to be used and its purpose 4. applied shortcut arithmetic operators 5. practiced to create a program with input and output statements using the program structure ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Ada Lovelace (1843) invents the first-ever machine algorithm for Charles Babbage’s Difference Machine that lays the foundation for all programming languages. Konrad Zuse (1944-45: Plankalkül) developed the first ‘real’ programming language called Plankalkül (Plan Calculus). ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies History of programming languages: A timeline ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies C# (pronounced see-sharp) is a modern, object-oriented programming language developed by Microsoft in the early 2000s as part of its.NET initiative. It combines the power and flexibility of C++ with the simplicity of Visual Basic. C# is known for its robustness, rich library, and support for diverse programming paradigms. This comprehensive guide explores the essential features of C#, from its basic structure to advanced concepts like LINQ and asynchronous programming, including detailed explanations and code examples. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Martin Richards developed BCPL in 1967 designed mainly for writing operating systems and compilers. Ken Thompson developed B, modeled on BCPL. Dennis Ritchie developed the C language from B in 1972 ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: Using Directives Namespace declaration Class or Class Definition Class methods Class attributes Main method Statements and Expressions Comments ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 1. Using Directives These statements allow you to use types defined in other namespaces without needing to specify the full namespace path each time. using System; using System.Collections.Generic; ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 1. Using Directives Using directives in C# are used to include namespaces, which contain classes and other resources. This prevents the need for fully qualified names. Here's a list of commonly used directives: using System; : Fundamental classes and base classes, like Console, Math, Func, Action. using System.Collections.Generic;: Generic collections like List, Dictionary. using System.Linq; : Language Integrated Query (LINQ) extensions. using System.Text ;: Classes for working with text, like StringBuilder. using System.Threading.Tasks; : Task-based asynchronous programming. using System.IO; : Input-output classes, like FileStream, StreamReader. using System.Net.Http; : Classes for sending HTTP requests and receiving responses. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 2. Namespace Declaration Namespaces are used to organize code into a hierarchical structure, making it easier to manage and avoid naming conflicts. A namespace is a collection of classes. namespace MyApplication { // Classes, methods, etc. } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 3. A Class A class is a blueprint for creating objects. It encapsulates data and methods that operate on that data. public class Car { public string Make { get; set; } public string Model { get; set; } public int Year { get; set; } } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 4. Class Methods Methods within a class define the behaviors of the objects created from the class. They can perform actions and return values. public void Drive() { Console.WriteLine("The car is driving."); } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 5. Class Attributes Attributes, also known as fields or properties, store the data or state of an object. public string Make { get; set; } public string Model { get; set; } public int Year { get; set; } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 6. A Main Method The Main method is the entry point of a C# application. It’s where the program starts execution. public static void Main(string[] args) { Car myCar = new Car(); myCar.Make = "Toyota"; myCar.Model = "Corolla"; myCar.Year = 2020; myCar.Drive(); } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 7. Statements and Expressions Statements perform actions, while expressions evaluate to produce values. int x = 5; // Statement int y = x + 2; // Expression ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program consists of the following parts: 8. Comments Comments are non-executable lines in the code meant for documentation or explanation. They are ignored by the compiler. // This is a single-line comment ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# program Syntax Rules: C# syntax is influenced by C and C++ and includes: Case Sensitivity: C# is case-sensitive. Semicolons: Each statement ends with a semicolon. Braces: Used to define the scope of classes and methods. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies A C# Reserved Words C# has reserved words, such as: if, else, switch, case: Control structures. int, double, bool, string: Data types. public, private, protected: Access modifiers. namespace, class, interface: Type declarations. new, static, void: Method and member modifiers. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Data Types C# is a strongly-typed language. It means we must declare the type of a variable that indicates the kind of values it is going to store, such as integer, float, decimal, text, etc. The following declares and initialized variables of different data types: string int float char bool ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Data Types 1. string: A sequence of characters used to represent text. string greeting = "Hello, World!"; 2. int: A data type used to store integer values. int age = 25; 3. float: A data type used to store floating-point numbers, which are numbers with a fractional part. float temperature = 36.6f; 4. char: A data type used to store a single character. char grade = 'A'; 5. bool: A data type used to store boolean values, which can be either true or false. bool isStudent = true; ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Data Types C# mainly categorized data types in two types: Value types and Reference types. Value types include simple types (such as int, float, bool, and char), enum types, struct types, and Nullable value types. Reference types include class types, interface types, delegate types, and array types. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Example: Variables of Different Data Types 1. string stringVar = "Hello World!!"; 2. int intVar = 100; 3. float floatVar = 10.2f; 4. char charVar = 'A'; 5. bool boolVar = true; ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Predefined Data Types in C# C# includes some predefined value types and reference types. The following table lists predefined data types: Type Description Range Suffix byte 8-bit unsigned integer 0 to 255 sbyte 8-bit signed integer -128 to 127 short 16-bit signed integer -32,768 to 32,767 ushort 16-bit unsigned integer 0 to 65,535 ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Predefined Data Types in C# Type Description Range Suffix int 32-bit signed integer -2,147,483,648 to 2,147,483,647 uint 32-bit unsigned integer 0 to 4,294,967,295 u long 64-bit signed integer -9,223,372,036,854,775,808 to l 9,223,372,036,854,775,807 ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615 ul float 32-bit Single-precision floating -3.402823e38 to 3.402823e38 f point type ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Predefined Data Types in C# Type Description Range Suffix double 64-bit double-precision floating -1.79769313486232e308 to d point type 1.79769313486232e308 decimal 128-bit decimal type for financial (+ or -)1.0 x 10e-28 m and monetary calculations to 7.9 x 10e28 char 16-bit single Unicode character Any valid character, e.g. a,*, \x0058 (hex), or\u0058 (Unicode) bool 8-bit logical true/false value True or False ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Predefined Data Types in C# Type Description Range Suffix object Base type of all other types. string A sequence of Unicode characters DateTime Represents date and time 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999 ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Arithmetic Operators The arithmetic operators perform arithmetic operations on all the numeric type operands such as sbyte, byte, short, ushort, int, uint, long, ulong,Name Operator float, double, and decimal. Description Example + Addition Computes the sum of left and right operands. int x = 5 + 5; - Subtraction Subtract the right operand from the left operand int x = 5 - 1; * Multiplication Multiply left and right operand int x = 5 * 1; / Division Divides the left operand by the right operand int x = 10 / 2; % Remainder Computes the remainder after dividing its left operand by its right int x = 5 % 2; operand ++ Unary increment Unary increment ++ operator increases its operand by 1 x++ -- Unary decrement Unary decrement -- operator decreases its operand by 1 x-- + Unary plus Returns the value of operand +5 - Unary minus Computes the numeric negation of its operand. -5 ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Assignment Operators The assignment operator = assigns its right had value to its left-hand variable, property, or indexer. It can also be used with other arithmetic, Boolean logical, and bitwise operators. Operator Name Description Example = Assignment Assigns its right had value to its left- x = 10; hand variable, property or indexer. x op= y Compound Short form of x =x op y where op = x += 5; assignment any arithmetic, Boolean logical, and bitwise operator. ??= Null- C# 8 onwards, ??= assigns value of x ??= 5; coalescing the right operand only if the left assignment operand is null ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Comparison Operators Comparison operators compare two numeric operands and returns true or false. Operator Description Example < Returns true if the left operand is less than the right operand x < y; > Returns true if the left operand is greater than the right x > y; operand = y; right operand ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Equality Operators The equality operator checks whether the two operands are equal or not. Operator Description Example == Returns true if operands are equal otherwise x == y; false. != Returns true if operands are not equal x != y; otherwise false. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Boolean Logical Operators The Boolean logical operators perform a logical operation on bool operands. Operator Description Example ! Reverses the bool result of bool expression. Returns false if result is true !false and returns true if result is false. && Computes the logical AND of its bool operands. Returns true both operands x && y; are true, otherwise returns false. || Computes the logical OR of its bool operands. Returns true when any one x || y; operand is true. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Basic Input/Output (I/O) operations in C# programming 1. Console.WriteLine(); This method is used for sending data (output) to the standard output stream, which is typically the console or terminal. It's part of output functionality because it displays information to the user. 2. Console.ReadLine(); This method is used for reading input from the standard input stream, which is typically user input from the keyboard. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Example: Variables of Different Data Types using System; public class Program { public static void Main() { string stringVar = "Hello World!!"; int intVar = 100; float floatVar = 10.2f; char charVar = 'A'; bool boolVar = true; Console.WriteLine(stringVar); Console.WriteLine(intVar); Console.WriteLine(floatVar); Console.WriteLine(charVar); Console.WriteLine(boolVar); } } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Example: Variables of Different Data Types The value of unsigned integers, long, float, double, and decimal type must be suffix by u,L,f,d, and m, respectively. Example: Value Suffix uint ui = 100u; float fl = 10.2f; long l = 45755452222222L; ulong ul = 45755452222222ul; double d = 11452222.555d; decimal mon = 1000.15m; ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Operators and Precedence C# Operators Operators in C# are some special symbols that perform some action on operands. In mathematics, the plus symbol (+) do the sum of the left and right numbers. In the same way, C# includes various operators for different types of operations. ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Operators and Precedence C# Operators The following example demonstrates the + operator in C#. Example: + Operator int x = 5 + 5; int y = 10 + x; int z = x + y; ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Operators and Precedence using System; public class Program { public static void Main() { int x = 5 + 5; int y = 10 + x; int z = x + y; Console.WriteLine(z); } } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Operators and Precedence concatenate C# Operators The following example demonstrates the + operator in C#. Example: + Operator with Strings string greet1 = "Hello " + "World! "; string name1 = “ Mina”; string name2 = “Jozel” string finalGreeting = greet1 + “” + name1; ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies Operator Precedence Example: int a = 5 + 3 * 3; int b = 5 + 3 * 3 / 2; int c = (5 + 3) * 3 / 2; PEMDModAS int d = (3 * 3) * (3 / 3 + 5); ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages College College of of Computer Computer Studies Studies using System; public class Program { public static void Main() { int a = 5 + 3 * 3; int b = 5 + 3 * 3 / 2; int c = (5 + 3) * 3 / 2; int d = (3 * 3) * (3 / 3 + 5); Console.WriteLine($"a = {a}"); Console.WriteLine("a = " + a); Console.WriteLine($"b = {b}"); Console.WriteLine($"c = {c}"); Console.WriteLine($"d = {d}"); } } ITC 112 – COMPUTER PROGRAMMING I TOPIC: Introduction To Programming Languages