Advanced Computer Programming

Similar documents
Visual C# Instructor s Manual Table of Contents

Chapter 2: Using Data

Introduction to C# Applications

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Values & Variables 1

BASIC ELEMENTS OF A COMPUTER PROGRAM

Understand Computer Storage and Data Types

10/9/2012. Computers are machines that process data. assignment in C# Primitive Data Types. Creating and Running Your First C# Program

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Fundamental of Programming (C)

Program Fundamentals

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 4: Making Decisions

Basics of Java Programming

Full file at

Chapter 4: Making Decisions

Variables, Constants, and Data Types

Fundamentals of Programming

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

.Net Technologies. Components of.net Framework

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSI33 Data Structures

Basic data types. Building blocks of computation

CSC Web Programming. Introduction to JavaScript

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Lecture 2 Tao Wang 1

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

2 nd Week Lecture Notes

Java Notes. 10th ICSE. Saravanan Ganesh

Full file at

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

COMP Primitive and Class Types. Yi Hong May 14, 2015

Basic Types, Variables, Literals, Constants

Declaration and Memory

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Reserved Words and Identifiers

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Language Reference Manual simplicity

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2: Data and Expressions

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

CSc Introduction to Computing

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

REVIEW. The C++ Programming Language. CS 151 Review #2

LECTURE 04 MAKING DECISIONS

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

VLC : Language Reference Manual

Chapter 2: Overview of C++

Computer Programming : C++

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

FRAC: Language Reference Manual

CMPT 125: Lecture 3 Data and Expressions

ENGR 101 Engineering Design Workshop

Chapter 2: Using Data

Decisions. Arizona State University 1

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

6.096 Introduction to C++ January (IAP) 2009

Advanced Computer Programming

BITG 1233: Introduction to C++

Chapter 2: Data and Expressions

IT 374 C# and Applications/ IT695 C# Data Structures

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Program Elements -- Introduction

Chapter 6 Primitive types

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Introduction to the C++ Programming Language

Unit 3. Constants and Expressions

Introduction to Bioinformatics

egrapher Language Reference Manual

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

The C++ Language. Arizona State University 1

Java Basic Datatypees

Tokens, Expressions and Control Structures

Number Systems, Scalar Types, and Input and Output

Getting started with C++ (Part 2)

Fundamentals of Programming CS-110. Lecture 2

LECTURE 02 INTRODUCTION TO C++

C# INTRODUCTION. Object. Car. data. red. moves. action C# PROGRAMMERS GUIDE LESSON 1. CsGuideL1.doc. Date Started: May 8,2001

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

printf( Please enter another number: ); scanf( %d, &num2);

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

3. Java - Language Constructs I

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

1 Lexical Considerations

Chapter 2: Basic Elements of C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Transcription:

Hazırlayan Yard. Doç. Dr. Mehmet Fidan

VARIABLE TYPES Integral Types: In C#, an integral is a category of types. For anyone confused because the word Integral sounds like a mathematical term, from the perspective of C# programming, these are actually defined as Integral types in the C# programming language specification. They are whole numbers, either signed or unsigned, and the char type. The char type is a Unicode character, as defined by the Unicode Standard. For more information, visit The Unicode Home Page. Type Size (in bits) Range sbyte 8-128 to 127 byte 8 0 to 255 short 16-32768 to 32767 ushort 16 0 to 65535 int 32-2147483648 to 2147483647 uint 32 0 to 4294967295 long 64-9223372036854775808 to 922337203685477580 ulong 64 0 to 18446744073709551615 char 16 0 to 65535

VARIABLE TYPES Floating Point and Decimal Types: A C# floating point type is either a float or double. They are used any time you need to represent a real number, as defined by IEEE 754. For more information on IEEE 754, visit the IEEE Web Site. Decimal types should be used when representing financial or money values. Type Size (in bits) precision Range float 32 7 digits 1.5 x 10-45 to 3.4 x 10 38 double 64 15-16 digits 5.0 x 10-324 to 1.7 x 10 308 decimal 128 28-29 decimal places 1.0 x 10-28 to 7.9 x 10 28 float 4 bytes float fval = -1,2; double 8 bytes double dval = -3.565; decimal 8 bytes decimal dval = -3456.343;

VARIABLE TYPES The String Type: A string is a sequence of text characters. You typically create a string with a string literal, enclosed in quotes: "This is an example of a string. string mystring = Hello! ; Hello! Verbatim strings string mystring = @ 2.5 disk ; string e = "Joe said \"Hello\" to me"; string f = @"Joe said ""Hello"" to me"; string g = "\\\\server\\share\\file.txt"; string h = @"\\server\share\file.txt"; 2.5 disk Joe said "Hello" to me Joe said "Hello" to me \\server\share\file.txt \\server\share\file.txt Some characters aren't printable, but you still need to use them in strings. Therefore, C# has a special syntax where characters can be escaped to represent non-printable characters. For example, it is common to use newlines in text, which is represented by the '\n' char. The backslash, '\', represents the escape. When preceded by the escape character, the 'n' is no longer interpreted as an alphabetical character, but now represents a newline. You may be now wondering how you could represent a backslash character in your code. We have to escape that too by typing two backslashes, as in '\\'.

TYPE CONVERSION Value must be compatible with the variable. If not, we have to make a type conversion. Some data types can be converted to an other and some can not. We can lose data during unsafe conversion e.g. assigning a floating point number to an integer. In that case we lose fraction. Implicit Conversion Explicit Conversion

IMPLICIT CONVERSION Numeric Numeric Properties: Occurs between compatible types. Transfers data from a smaller type to a bigger type. No data loss. No extra operation. Automatic type conversion.

IMPLICIT CONVERSION Type float 32 double 64 decimal 128 Size (in bits) Type Size (in bits) Range sbyte 8 byte 8 short 16 ushort 16 int 32 uint 32 long 64 ulong 64 char 16

IMPLICIT CONVERSION Implicit Conversion No data loss

EXPLICIT CONVERSION Numeric Character Character Numeric Occurs between incompatible types. May lose data. Extra coding needed. Use with caution.

EXPLICIT CONVERSION Type Size (in bits) Range sbyte 8-128 to 127 byte 8 0 to 255 short 16-32768 to 32767 ushort 16 0 to 65535 int 32-2147483648 to 2147483647 uint 32 0 to 4294967295 long 64-9223372036854775808 to 922337203685477580 ulong 64 0 to 18446744073709551615 char 16 0 to 65535

.ToString() method. NUMERIC TO CHARACTER Can be used with numeric types. Converts numeric value to string type. There is also a formatting option.

NUMERIC TO CHARACTER Output : 123

FORMATTING EXAMPLE NumberFormatInfo class. Used especially with floating point numbers. Resides under System.Globalization namespace. Formatting properties Number decimal digits Number decimal separator Number group separator

FORMATTING EXAMPLE Output: 12.345,67 Detailed Information: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

CHARACTER TO NUMERIC Convert class Explicit Conversion Used in conversion of multiple types. There should be compatible types between conversions. Output: 132

CHARACTER TO NUMERIC

CHARACTER TO NUMERIC Console.ReadLine() class Used for getting input from screen. String type value is returned. Returns when user press Enter key. Later, return value can be converted to other types.

LOGICAL OPERATORS and CONTROL STATEMENTS Boolean Boolean takes true or false values. Used in deciding states. Commonly used in conditional statements and loops. A logical operation is a formula that has a true or false result. Boolean variables are defined with bool keyword. bool bval = true;

BOOLEN EXAMPLE

BOOLEN OPERATORS Used for constructing Boolean expressions. and && or not! equal == Not equal!= Comparison <, >, <=, >=

AND (&&) OR ( ) NOT (!) AND: Both values must be true. OR: At least one value is to be true. true && true = true false && true = false true && false = false false && false = false true true = true false true = true true false = true false false = false NOT: Reverses the value.!true = false!false = true

EQUAL (==) NOT EQUAL (!=) COMPARISON EQUAL: Returns true if two values are equal. NOT EQUAL: Returns true if two values are not equal. 1 == 2 1 == 0 42 == 42 variable1 == othervariable 1!= 2 1!= 0 42!= 42 a!= variable COMPARISON: (>,<,>=,<=): 1 < 2 0 > 1 42 <= 42 age>= 18

OPERATOR PRIORITY Highest Parantheses Not (!) Comparison(<, >, <=, >=) Equals(==) Not Equals(!=) And(&&) Or( ) Lowest

EXAMPLE

EXAMPLE Parantheses Not (!) Comparison(<, >, <=, >=) Equals(==) Not Equals(!=) And(&&) Or( )

CONTROL STATEMENTS: IF - ELSE Decides which code block will run depending on the result of logical expression. Logical expressions retuns boolean values. If return value is true then code blocks within th if statement will be executed. Otherwise else statement will be executed. A simple if case decides whether corresponding code block will be executed or not. A structure that is composed of if and else will execute two different code blocks depending on the logical expression. if/else statement can be used in nested forms to express more complex stuations.

If control statements has two forms: IF CASE } if ( expression ) { codes; or if ( expression ) code;

IF CASE Common if use case : if ( expression ) statement; We can also write if else as: if (expression ) statement1; else statement2;

IF CASE if ( boolean expression is true ) statement ; else if (boolean expression is true) statement ; else statement ;

IF CASE When there are multiple conditions with one line of statements, we can use if-else structure seen below: if (boolean expression is true ) statement; else if (boolean expression is true ) statement; else if (boolean expression is true ) statement; else statement;

IF CASE if (boolean expression is true ) { statement blocks; } else if (boolean expression is true ) { statement blocks; } else { statement blocks; }

IF CASE

EXERCISE Meteorogy department asks for a program that converts humudity values to a human readable format. Program will write to output the following results depending input ranges between 0 and 100. %20 and lower too dry" %21 - %40: dry" %41 - %60: little dry" %61 - %80: little moist" %81 and higher: moist Write a program that takes a humudiy value from user and writes it s corresponding human readable output.

SWITCH CASE Used if the value of a variable is used for controlling the program flow. May execute different code blocks for each different value of the variable. C# language offers this functionality with switch-case structure.

switch (variable ) { case constant_value1 : statements; break; case constant_value2 : statements; break; case constant_value3 : statements; break; default : statements; break; } SWITCH CASE switch is the starting point of the structre. A variable should be provided after switch. This variable could be numeric or character. We should use only constant value in case sections, no expressions. All cases should end with break keywords. No need to use { } paranthesis aftes cases.

SWITCH CASE Write a program that takes the course grade (just one letter) from user. Your program will write the following result to the output depending on the grade. A: best" B: good" C: all right" D: not bad" F: bad"

SWITCH CASE

EXERCISE Meteorogy department asks for a program that converts humudity values to a human readable format. Program will write to output the following results depending input ranges between 0 and 100. %20 and lower too dry" %21 - %40: dry" %41 - %60: little dry" %61 - %80: little moist" %81 and higher: moist Write a program that takes a humudiy value from user and writes it s corresponding human readable output by using switch /case control structure.

switch (variable ) { case constant_value1 : statements; break; case constant_value2 : statements; break; case constant_value3 : statements; break; default : statements; break; } SWITCH CASE NOTES double and decimal types are not used within switch paranthesis. Any number of statements can be used within the case element. Using default: element help us to detect defects. Forgetting to use breaks is the most common coding bad habbit. Check for breaks after constructing the switch structure.

EXAMPLES You enter a character from the keyboard, and the program uses a nested if statement to determine whether the input character is an alphabetic character. If the input character is an alphabetic character, the program checks whether the input character is lowercase or uppercase. A message appears for each case. Console.Write("Enter a character: "); char c = Convert.ToChar(Console.ReadLine()); if (Char.IsLetter(c)) { if (Char.IsLower(c)) { Console.WriteLine("The character is lowercase."); } else { Console.WriteLine("The character is uppercase."); } } else { Console.WriteLine("The character isn't an alphabetic character."); }