Values & Variables 1

Similar documents
Chapter 2: Using Data

Visual C# Instructor s Manual Table of Contents

Advanced Computer Programming

Unit 3. Operators. School of Science and Technology INTRODUCTION

Information Science 1

Programming in C++ 5. Integral data types

Chapter 2: Data and Expressions

2 nd Week Lecture Notes

Introduction to C# Applications

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Operators in C. Staff Incharge: S.Sasirekha

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 3. More About C

Chapter 2: Data and Expressions

UNIT- 3 Introduction to C++

DEPARTMENT OF MATHS, MJ COLLEGE

Operators & Expressions

Chapter 2: Data and Expressions

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

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

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Declaration and Memory

Work relative to other classes

Information Science 1

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Notes. 10th ICSE. Saravanan Ganesh

AP Computer Science A

CS102: Variables and Expressions

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 6 Primitive types

Basics of Java Programming

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Accelerating Information Technology Innovation

ECE 122 Engineering Problem Solving with Java

More Programming Constructs -- Introduction

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

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

A flow chart is a graphical or symbolic representation of a process.

Lecture 3: Variables and assignment

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

CMPT 125: Lecture 3 Data and Expressions

Client-Side Web Technologies. JavaScript Part I

Fundamentals of Programming CS-110. Lecture 3

3. Java - Language Constructs I

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

JAVA Programming Fundamentals

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Computer System and programming in C

QUIZ: What value is stored in a after this

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

A First Program - Greeting.cpp

Full file at

Informatics Ingeniería en Electrónica y Automática Industrial

Full file at

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Prefix/Infix/Postfix Notation

JAVA OPERATORS GENERAL

High Performance Computing in C and C++

Values and Variables 1 / 30

Operators. Java operators are classified into three categories:

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Engineering Computing I

Chapter 2. C++ Basics

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

C: How to Program. Week /Mar/05

Outline. Data and Operations. Data Types. Integral Types

Elementary Programming

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

Course Outline. Introduction to java

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

FUNDAMENTAL DATA TYPES

Chapter 2 Working with Data Types and Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

.Net Technologies. Components of.net Framework

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

1.8 Intro to Variables, Algebraic Expressions, and Equations

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

Chapter 2 Elementary Programming

4 Programming Fundamentals. Introduction to Programming 1 1

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Creating a C++ Program

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

CSC Web Programming. Introduction to JavaScript

Chapter 2 - Introduction to C Programming

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

Course PJL. Arithmetic Operations

CSC 1107: Structured Programming

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

CIS 110: Introduction to Computer Programming

Transcription:

Values & Variables 1

Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords can not be used as variable names 2

Keywords 3

How to define a variable Type Value int x = 3; Variable Semicolon: End of statement 4

Ways to define a variable int ninteger; string sstring; int ninteger = 42; string sstring = "This is a string!"; int ninteger; string sstring;... ninteger = 42; Double quotes represents a string sstring = "This is a string!"; 5

Necessity to set a value to a variable string svalueless; MessageBox.Show(sValueless); Error! 6

Variable Types Simple types Integers Floating point numbers Characters Strings 7

Integers short 2 bytes ( 32,768 <-> 32,767) short sval = -12; ushort 2 bytes (0 <-> 65,535) ushort sval= 12; int 4 bytes ( 2,147,483,647 <-> 2,147,483,647) int nval = -12500; uint 4 bytes (0 <-> 4,294,967,295) uint nval = 12500; long 8 bytes long lval = -548444101; ulong 8 bytes Ulong lval = 548444101 8

Floating Point Numbers float 4 bytes float fval = -1,2; double 8 bytes double dval = -3.565; decimal 8 bytes decimal dval = -3456.343; 9

Expressions Expressions are used for performing operations over variables. Return a value of known type. Two types of expressions Operators Functions 10

Arithmetic operations They need more than one variable. Performs mathematical operations + (addition operation) - (subtraction operation) * (multiplication operation) / (division operation) % (modulus operation). 11

Arithmetic operations Abbreviations int m = 5; int n = 4; m = m + n; equals m += n; In other words in the end of both expressions m will have value of 9 and the value of n will not be changed. 12

Increment and decrement operations They operate on one variable ++ is increment operator i++; i = i + 1; -- is decrement operator i --; i = i 1; Prefix and postfix operators will yield to different results. i.e. i++ and ++i are not same. 13

Increment and decrement operations ++k The result of the operation is the value of the operand after it has been incremented. k++ The result of the operation is the value of the operand before it has been incremented. --k The result of the operation is the value of the operand after it has been decremented. k-- The result of the operation is the value of the operand before it has been decremented. 14

Example int k=0, m; Values of m and k will be 1 m = ++k; int k=0, m; m = k++; m will be 0 and k will be 1 int k=5, m, n=2; m = --k + n; m will be 6 and k will be 4 int k=0, m, n=7; m = k++ + --n; m will be 6 and k will be 1 and n will be 6 15

Exercise What will be the values of the variables after code piece below is executed? int i, j, k; i = 2; j = 3 + i++; k = 3 + ++i; i *= ++k + j--; i /= k-- + ++j; 16

Exercise Assuming that line of codes are independent, what will be the value of variable m after each line is executed? int i = 0, j = 6, k = 4, m = 5; m = k-- + ++i; m *= j % 4; m += k++ + (j-- * ++i); 17

Order of Operations Rules that defines which procedures should be performed first. In C# language some operators have execution privilege over others. To predict the result of an expression first we should know the order of operations. 18

Example PEMDAS phrase may help to remember the order. 1 + 2 * 3-4 / 5 =? 1 + (2 * 3) (4 / 5) P E M D A S Parenthesis Exponent Multiplication Division Addition Subtraction 6.2 19

Example(result) If we use all numbers in integer type then the result will be integer(in other words fraction will be removed) 1 + (2 * 3) (4 / 5) 7 4/5 = 0 (integer division) 20

Exercise Different data types may yield different results in same operations. Write and execute the codes in the next slides. Explain the difference between results. 21

Exercise (continues) 22

Exercise (continues) 23

Characters char 1 byte 0-256 'a' 'z' 'A' 'Z' '?' '@' '0' '9' Special characters are represented by using \ prefix. '\n' : new line '\t' : tab '\'' : single quote '\\' : backslash 24

Strings (Character Arrays) Sequence of characters. Example: Hello! first line\n second line \n third line Empty string 25

Strings string Class Unicode 16 bit Example: string mystring = Hello! ; Verbatim strings string mystring = @ 2.5 disk ; 26

string operations Appending two strings Result: Hello world! 27

string operations Searching within a string int IndexOf () Result: 1 Exercise: Find the usage of LastIndexOf() function and write an example by using this function. 28

string operations Retrieve a substring from a string string Substring() Result : llo 29

Exercise Put your name and surname into two string variables. Concatenate two strings. Write the result to the console. 30

DateTime C# language has built-in DateTime structure to represent date and time values. We can store year, month, day, hour, minute, second values in a DateTime structure. 31

Creating a DateTime Object Type Creating a new object DateTime dt = new DateTime(year, month, day); Variable name Initial values 32

Example A new DateTime object is created 33

Constants Their values can not be changed. They have types. We can use them in expressions bur can not alter them. Defined by using const keyword before variable type. Their values can be set only during definition line. const int nvar = 34; 34

Example 35

Advanced Computer Programming HOMEWORK HW1: Install VS.Net 2015 HW2: Write a program to display user s complete mailing address. Accept user s name, city, street, pin and house no. HW3: Write a program to display student information. Accept Student s name, Roll no, Age, class, and university name and display it on console. 36