Thanachat Thanomkulabut

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Introduction to Computer and Programing Thanachat Thanomkulabut.
C++ Programming: Basic Elements of C++.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Introduction to Computer and Programing Thanachat Thanomkulabut.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Input & Output Statement
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Introduction to Computer and Programing Thanachat Thanomkulabut.
Operators.
Chapter Two Fundamental Programming Structures in Java Adapted from MIT-AITI slides Variables and Primitive Data Types 1.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Introduction to Computer and Programing Thanachat Thanomkulabut.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Chapter # 2 Part 2 Programs And data
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter Topics The Basics of a C++ Program Data Types
Computing and Statistical Data Analysis Lecture 2
Basic Elements of C++.
Multiple variables can be created in one declaration
Computing with C# and the .NET Framework
Basic Elements of C++ Chapter 2.
Operators and Expressions
OPERATORS (2) CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Fundamentals 2.
C# Programming.
Chapter # 2 Part 2 Programs And data
Expressions and Assignment
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

Thanachat Thanomkulabut C# Basics & Math Thanachat Thanomkulabut

Outline Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Lecture 1: Program Structure C# Beginning Lecture 1: Program Structure The starting point of the program is: This is known as the Main method A method is put inside a class A class can be put inside a namespace namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

Lecture 1: Variable Naming Rules C# Beginning Lecture 1: Variable Naming Rules Letters, digits and underscores(_) First character must be a letter or _ Up to 63 characters long Must not be a reserved word Example name Name point9 9point _data class class_A class_"A"

Outline Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

What is in a program? Variable declarations Statements C# Beginning What is in a program? Variable declarations Statements static void Main(string[] args) { const double pi = 3.1416; int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }

A variable is used to store some “data.” Variable & Constant What is Variable? A variable is used to store some “data.” “It must be declared before it can be used”

Data Types Type Size Description Range bool 1 byte Store truth value Variable & Constant Data Types Type Size Description Range bool 1 byte Store truth value true / false char Store one character character code 0 – 255 byte Store positive integer 0 – 255 short 2 byte Store integer -32,768 -- 32,767 int 4 byte -2.1 x 109 -- 2.1 x 109 long 8 byte -9.2 x 1018 -- 9.2 x 1018 double 16 byte Store real number ± 5.0x10-324 -- ± 1.7x10308 string N/A Store sequence of characters

Outline Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Variable Declaration Syntax: Example: Variable & Constant Variable Declaration Syntax: <data type> <name>; Example: We can also assign its initial value. Example: int radius; double area; bool isokay; int k = 200; bool done = false;

Multiple-Variable Declaration Variable & Constant Multiple-Variable Declaration Syntax: <data type> <name_1>, <name_2>,..., <name_n>; Example: We can also assign their initial value. Example: int width, length, height; double mean, sd, max, min; bool isokay, isright, check; int width=5, length=2, height=4;

Test : Variable Declaration Variable & Constant Test : Variable Declaration Declare variable1 Name : num_Student Type : integer Initial value : nothing Declare variable2 Name : gender Type : character Initial value : m int num_Student; char gender = ‘m’;

Test : Multiple-Variable Declaration Variable & Constant Test : Multiple-Variable Declaration Declare variables 1,2,3 Name1 : u Name2 : t Name3 : a Type : double Initial value1 : 5.0 Initial value2 : nothing Initial value3 : 9.8 double u=5.0, t, a=9.8;

Constant Declaration Syntax: Example: Variable & Constant Constant Declaration Syntax: const <data type> <name> = <value>; Example: const int radius = 15; const double area=1.5; const bool is_done=true; const string movie=”StarWarIII”; const char my_initial=‘m’;

Multiple-Constant Declaration Variable & Constant Multiple-Constant Declaration Syntax: const <data type> <name_1> = <value_1>, <name_2> = <value_2>, ... , <name_n> = <value_n>; Example: const int radius = 15, height = 5; const double area=1.5, width=3.2, length = 4.1;

Test : Constant Declaration Declare Constant Name : e Type : double Initial value : 2.71828 const double e=2.71828;

Outline Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Arithmetic Expression Boolean Expression

Arithmetic Expression Operators + - * / % (modulo: remainder after division) Integer operates Integer  Integer Integer/Real operates Real/Integer  Real Example 11 + 5  39 / 5  39.0/5  39 % 5  5.0 % 2.2  16 7 7.8 4 0.6

Example static void Main(){ int a,b,c,d; double e,f,g; a=2; b=7; c=5; Arithmetic Example static void Main(){ int a,b,c,d; double e,f,g; a=2; b=7; c=5; d=c/a; e=5/b; f=5.0/2; g=5/2.0; } Answer d = 2 e = 0 f = 2.5 g = 2.5

Pre/Post Increment & Decrement In/Decrement Pre/Post Increment & Decrement Pre in/de-crement: Increment/decremente the value first, then use the value Post in/de-crement: Use the value first, then increment/decrement the value Operator Meaning example ++x pre increment int a = 5; int b = ++a; // a, b = 6 x++ post increment int b = a++; // a = 6, b = 5 --x pre decrement int b = --a; // a, b = 4 x-- post decrement int b = a- - ; // a = 4, b = 5

Pre & Post Increment a b Post: int a=5; int b=a++; In/Decrement Pre & Post Increment a b Post: int a=5; int b=a++; Console.WriteLine(“a={0}, b={1}”,a,b); Monitor 5 6 5 a=6, b=5 a b Pre: int a=5; int b=++a; Console.WriteLine(“a={0}, b={1}”,a,b); Monitor 6 5 6 a=6, b=6

Priority of Arithmetic Operators 1 Parentheses () 2 x++, x-- 3 ++x, --x 4 *, / , % 5 +, - 6 If equal priority, work from left to right

Example int a, b, c; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; Arithmetic Example int a, b, c; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; c = 5+9%2*4-14/4%3 Answer a = -2 b = 1 c = 9

Comparison x/y*++b x/y*b++ Answer: 4.5 Answer: 3.0 Given x = 3.0, y = 2.0, b =2, what is the output? x/y*++b x/y*b++ 3 2 Answer: 4.5 Answer: 3.0

Modify-and-Assign Operations Statement Description var += expression Increment var by the value of expression var -= expression Decrement var by the value of expression var *= expression Multiply var by the value of expression, then store the result in var var /= expression Divide var by the value of expression, then store the result in var sum += x; // is equivalent to sum = sum + x prod *= 2.5; // is equivalent to prod = prod * 2.5 y -= 3+a; // is equivalent to y = y – (3+a)

Example 3 7.5 -4 double x = 1, sum = 2, prod = 3; int y = 4, a = 5; Modify-And-Assign Example double x = 1, sum = 2, prod = 3; int y = 4, a = 5; sum += x; // is equivalent to sum = sum + x prod *= 2.5; // is equivalent to prod = prod * 2.5 y -= 3+a; // is equivalent to y = y – (3+a) Console.WriteLine(sum); Console.WriteLine(prod); Console.WriteLine(y); Monitor 3 7.5 -4

Math Class Math Class Method/ Constant Value returned Example Call Result PI Value of π Math.PI 3.1415927 Max(x,y) Larger of the two Math.Max(1,2) 2 Abs(x) Absolute value of x Math.Abs(-1.3) 1.3 Sqrt(x) Square-root of x Math.Sqrt(4.0) 2.0 Round(x) Nearest integer to x Math.Round(0.8) 1 Pow(x,y) xy Math.Pow(3,2) 9.0 Log(x) Natural log of x = ln x Math.Log(10) 2.302585 Floor(x) Greatest integer smaller than or equal to x Math.Floor(4.9) 4 Ceiling(x) Smallest integer greater than or equal to x Math.Ceiling(4.1) 5 Cos(x) Cosine of x radians Math.Cos(Math.PI) -1

Example What is the output? 3 4 Monitor Console.WriteLine(Math.Round(3.499)); Console.WriteLine(Math.Floor(3.499)); Console.WriteLine(Math.Ceiling(3.499)); 3 4

Example cos(π/3) | ln(4) | Write arithmetic expressions using Math class cos(π/3) | ln(4) | Math.Cos(Math.PI/3) Math.Pow(3,0.2) Math.Abs(Math.Log(4))

Outline Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Statement A statement is a command to computer Statement#1 class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } Statement#2

C# Statement Types C# Statement Types Assignment Statement Input Statement Output Statement

Assignment Statement To assign value to variable Use equal sign (=) Syntax: <variable> = <expression>; int Width,Height; Width=10; Height=20+Width;

Output Statement Value Math expression More usages of output statement will be shown later in this course Console.WriteLine("Hello"); Console.WriteLine(2012); Console.WriteLine(a); Console.WriteLine(a+b); Console.WriteLine(Math.Pow(a,b));

Any question?