C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.

Slides:



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

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Thanachat Thanomkulabut
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
C++ Programming: Basic Elements of C++.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
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.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
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.
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
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
Chapter Topics The Basics of a C++ Program Data Types
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Operators and Expressions
OPERATORS (2) CSC 111.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Fundamentals 2.
C# Programming.
Chapter # 2 Part 2 Programs And data
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

C# Basic Concept Thanachat Thanomkulabut

Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long  Must not be a reserved word  Case Sensitive C# Language Overview 2 Example nameName _data 9point  class  class_A class_”A”  point9

Outline 3  C# Beginning  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

C# Beginning 4  The starting point of the program is:  This is known as the method Main  A method is put inside a class  A class may be put inside a namespace static void Main () {... starting point... } static void Main () {... starting point... } C# Beginning

Main Method declaration 5  There are 4 ways to define Main function.  We use the simplest one.  static void Main() {...}  static void Main(string[] args) {...}  static int Main() {...}  static int Main(string[] args) {...} C# Beginning

6 Inside method Main  Variable declarations  Statements static void Main(string[] args) { const double pi = ; int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); } C# Beginning

Outline 7  C# Beginning  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

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

Data Types TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32, ,767 int4 byteStore integer-2.1 x x 10 9 long8 byteStore integer-9.2 x x double16 byte Store real number± 5.0x ± 1.7x stringN/AStore sequence of characters N/A Variable & Constant 9

C# Variable Declaration  Syntax: ;  Example:  We can also assign its initial value. Example: int radius; double area; bool isokay; int k = 200; bool done = false; Variable & Constant 10

C# Variable Declaration 11  Syntax:,,..., ;  Example:  We can also assign its initial value. Example: int width, length, height; double mean, sd, max, min; bool isokay, isright, check; int width=5, length=2, height=4;

Test I - Variable Declaration 12  Declare variable1  Name : num_Student  Type : interger  Initial value : nothing  Declare variable2  Name : gender  Type : character  Initial value : m  Declare variable3,4,5  Name3 : u  Name4 : t Name5 : a  Type : double  Initial value3 : 5.0  Initial value4 : nothing  Initial value5 : 9.8

C# Constant Declaration  Syntax: const = ;  Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; const char mckazine=‘m’; Variable & Constant 13

C# Constant Declaration  Syntax: const =, =,..., = ;  Example: const int radius = 15, height = 5; const double area=1.5, wide=3.2, lenght = 4.1; Variable & Constant 14

Test II - Constant Declaration 15  Declare Constant  Name : e  Type : double  Initial value :

Outline 16  C# Beginning  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

C# Expression 17 Expression Arithmetic Expression Boolean Expression Expression

Arithmetic Expression  Operators  + - * /  % (remainder after division)  Example    39 / 5   39.0/5   39 % 5   5.0 % 2.2  Expression

Piority of Arithmetic Operators Priorityoperator 1Parentheses () 2*, /, % 3+, - 4If equal precedence, left to right int a, b; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; a = -2 b = 1 Answer 19 Expression

Calculation Priority 20 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; } d = 2 e = 0 f = 2.5 g = 2.5 Answer Expression

Boolean Expression  Operators  Comparison == Equal == != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >=  Boolean && And && || Or || ! Not ! 0 and 0 = 0 0 and 1 = 0 1 and 0 = 0 1 and 1 = 1 0 or 0 = 0 0 or 1 = 1 1 or 0 = 1 1 or 1 = 1 not 0 = 1 not 1 = 0 Expression 21

Example: Boolean Expression  10 > 50   ’A’ < ’B’  false true Expression 22

Outline 23  C# Beginning  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

Statements  A statement is a unit of command to instruct your program  A method consists of one or more statements class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } Statement#1 Statement#2 Statement 24

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

Assignment Statement  Assigning value to variable equal sign (=)  Use the equal sign (=) when making assignments.  Syntax: = ; = ; int Width,High; Width=10;High=20+Width; Statement 26

Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string to other data type  int.Parse() Convert string to integer  double.Parse() Convert string to double Example string st; st = Console.ReadLine(); Statement 27

Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement 28

Output Statements  Use the method Write or WriteLine in the Console class (which is in System namespace)  Basic usage:  Advanced usage:  Even more advanced usage: Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary); Console.WriteLine("Hello");Console.WriteLine(area); Statement 29

Outline 30  C# Beginning  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

Increment & Decrement OperatorMeaningexample ++xpre increment int a = 5; int b = ++a; // a, b = 6 x++post increment int a = 5; int b = a++; // a = 6, b = 5 --xpre decrement int a = 5; int b = --a; // a, b = 4 x--post decrement int a = 5; int b = a- - ; // a = 4, b = 5  Pre in/de-crement:  Use the value which has already been in/de-crement.  Post in/de-crement:  Use the value before in/de-crement Modify-And-Assign 31

Increment & Decrement 32 Ex1: int a=5; int b=a++; Console.WriteLine(“a={0}, b={1}”,a,b); ab 556 a=6, b=5 Monitor Ex2: int a=5; int b=++a; Console.WriteLine(“a={0}, b={1}”,a,b); ab 566 a=6, b=6 Monitor

Modify-And-Assign Operations StatementDescription 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) int y=8; int a=2; Console.WriteLine(y -= 3+a); Try this ! Modify-And-Assign 33

Outline 34  C# Beginning  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

The Math Class Method/ Constant Value returnedExample CallResult PI Value of  Math.PI Max(x,y)Larger of the twoMath.Max(1,2)2 Abs(x)Absolute value of xMath.Abs(-1.3)1.3 Sqrt(x)Square-root of xMath.Sqrt(4.0)2.0 Round(x)Nearest integer to xMath.Round(0.8)1 Pow(x,y)xyxy Math.Pow(3,2)9.0 Log(x)Natural log of xMath.Log(10) Ceiling(x)Smallest integer greater than or equal to x Math.Ceiling(4.1)5 Cos(x)Cosine of x radiansMath.Cos(Math.PI) Math Class 35

Test III 36  Write the program which  Input : Your name  Output : Your name is.

Test IV 37  Write the program which  Input : 3 number  Output : average of 3 input number

Test VI 38  Write the program which  Input : lenght of radius of circle  Output : Area of circle

Any question?