1 nd Semester 2007 1 Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.

Slides:



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

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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
CMT Programming Software Applications
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
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.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
2440: 211 Interactive Web Programming Expressions & Operators.
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
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.
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.
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.
Introduction to Computer and Programing Thanachat Thanomkulabut.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Introduction to Computer and Programing Thanachat Thanomkulabut.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Introduction to Computer and Programing Thanachat Thanomkulabut.
1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
A Sample Program #include using namespace std; int main(void) { cout
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.
Bill Tucker Austin Community College COSC 1315
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 3: Understanding C# Language Fundamentals
Basic Elements of C++ Chapter 2.
Operators and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Advanced Programming Lecture 02: Introduction to C# Apps
Module 2: Understanding C# Language Fundamentals
C# Programming.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 3 – Introduction to C# Programming
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok THAILAND

1 nd Semester Outline  C# Overview  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

1 nd Semester C# Program  Consider all the programs you wrote in Lab#0  What C#'s programming rules can you derive? namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

1 nd Semester C# Program  C# syntax is case-sensitive  Every statement ends with a semicolon ;  White space means nothing  Code block is inside braces { }  Anything between /* */ or after // is considered a comment Comments will not be translated

1 nd Semester Program Structure  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... }

1 nd Semester Program Structure  In C# A program can contain several namespaces A namespace can contain several classes A class can contain several methods  In other words Think of a namespace as a container of classes Think of a class as a container of methods method1 method2 namespace Class Class

1 nd Semester Program Structure (example) namespace method1 method2 Class Class namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine(“Hello"); } namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine(“Hello"); } method3 method1 method2 Class namespace ::::

1 nd Semester Program Structure  For this course Program with only one class and at most one namespace  For now until sometime before midterm Program with one method (i.e., Main) namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

1 nd Semester Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long  Must not be a reserved word * Case Sensitive Example KU66 ≠ kU66 ≠ku66 KU66 ≠ kU66 ≠ ku66 C# Overview

1 nd Semester C# Reserved Words C# Overview

1 nd Semester Short break – 3 Minutes

1 nd Semester Outline  C# Overview  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

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

1 nd Semester C# Variable Declaration  Syntax: ;  Example:  We can also assign its initial value. Example: int radius; double area; int a,b,c; bool isokay; int k = 200; bool done = false; Variable & Constant

1 nd Semester 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 byteStore real number± 5.0x ± 1.7x stringN/AStore sequence of characters N/A

1 nd Semester C# Constant Declaration  Syntax: const = ;  Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; Variable & Constant

1 nd Semester Outline  C# Overview  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

1 nd Semester C# Expression  Arithmetic Expression  Boolean ExpressionExpression

1 nd Semester Arithmetic Expression  Operators + - * / % (remainder after division)  Example  / 2  5 11 % 2  % 2.2  0.6Expression

1 nd Semester Precedence rules for Arithmetic Operators 1.( ) parentheses 2.*, /, % 3.+ – 4.If equal precedence, left to right int Width,High; Width = 10*5+(16*12)/5; High= (16+5)+20%2;

1 nd Semester Calculation Priority public static void Main(){ int a,b,c,d; a=1; b=2; c=3; d = c/b*a; Console.WriteLine("d={0}",d); d = a/b; Console.WriteLine("d={0}",d); } Console.WriteLine(3/4*8); = Console.WriteLine((3/4)*8); = 0 ( = ) Calculate from left to right! 3 4 x8 ? ???

1 nd Semester Expression in C#  Arithmetic Expression  Boolean ExpressionExpression

1 nd Semester Boolean Expression  Operators Comparison =  Equal = !=  Not equal != <  Less < >  Greater > <=  Less than or equal to <= >=  Greater than or equal to >= Boolean &&  And && ||  Or || !  Not !Expression 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

1 nd Semester Example: Boolean Expression  50 > 10  true  ’A’ < ’B’  true  ’ANT’ < ’B’  true  ’5’ > ’1’  true  ’5’ > ’10’  true  ’5’ > ’100’  true

1 nd Semester Quiz  ((1 and 0) or (1 and 1)) = ?  What is I/O device ?  What is RAM and ROM ?  1 byte = xxx bits  1 Mbyte = ?  C# is xxx language.  = ?

1 nd Semester Short break – 5 Minutes

1 nd Semester Outline  C# Overview  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

1 nd Semester 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 () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } class Hello { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } Statement#1 Statement#2

1 nd Semester C# Statement Types  Assignment Statement  Input Statement  Output StatementStatement

1 nd Semester Assignment Statement  Assigning value to variable equal sign (=)  Use the equal sign (=) when making assignments.  Syntax: = ; = ; int Width,High; Width=10;High=5; int Width = 10; int High = 5; Statement

1 nd Semester C# Statement Types  Assignment Statement  Input Statement  Output StatementStatement

1 nd Semester 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 = System.Console.ReadLine(); Statement

1 nd Semester string st; int i; double d; 1.st = Console.ReadLine(); i = int.Parse(st); 2.st = Console.ReadLine(); d = double.Parse(st); 3.i = int.Parse(Console.ReadLine()); 4.d = double.Parse(Console.ReadLine()); Example: Input Statement

1 nd Semester Example: Input Statement Ex1: string myname; myname = System.Console.ReadLine(); Ex2: int Width,High; string temp1; temp1 = System.Console.ReadLine(); Width = int.Parse(temp1); temp1 = System.Console.ReadLine(); High = int.Parse(temp1); Statement

1 nd Semester Example: Input StatementStatement

1 nd Semester C# Statement Types  Assignment Statement  Input Statement  Output StatementStatement

1 nd Semester 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); More information about formatting Console.WriteLine("Hello");Console.WriteLine(area);

1 nd Semester Outline  C# Overview  Variable and Constant  Expression  Statement  Modify-And-Assign  Math Class

1 nd Semester Increment & Decrement OperatorMeaningexample + + x+ + xpre increment int a = 5; int b = ++a; // a, b = 6 x + +x + +post increment int a = 5; int b = a++; // a = 6, b = xpre decrement int a = 5; int b = --a; // a, b = 4 x - -x - -post increment int a = 5; int b = a- - ; // a = 4, b = 5  Pre in/decrement: Use the value which has already been in- decrement.  Post in-decrement: Use the value before in-decrement

1 nd Semester 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 !

1 nd Semester Operator Priority ( ) ++(x), --(x), +(x), -(x) * / % + - =, +=, -=, *=, /=, %= (x)++, (x)-- Highest Priority (Do First) Lowest Priority (Do Last)

1 nd Semester 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 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) xyxy Math.Pow(3,2)9.0 Log(x) Natural log of x Math.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)

1 nd Semester Summary  C# Overview  Variable and Constant  Expression  Statement  Modify-And-Assign  Math ClassSummary