BİL528 – Bilgisayar Programlama II

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 3: Using Variables and Constants
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.
CS 355 – Programming Languages
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
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.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
.NET Data types. Introduction ٭ A "data type" is a class that is primarily used just to hold data. ٭ This is different from most other classes since they're.
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
BİL527 – Bilgisayar Programlama I Strings 1. Contents More on Variables – Type Conversions – Enumerations – Structs – Arrays – String Operations 2.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Data type, Type Casting,Constants. DATA TYPES IN JAVA.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
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.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:
Enumeration.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Programming with Microsoft Visual Basic th Edition
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
1.2 Primitive Data Types and Variables
Murach, Chapter 4. Two Data Types Value Types – Store their own data Reference Types Do not store their own data Stores a reference to an area of memory.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
LESSON 06.
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Chapter 6 – Data Types CSCE 343.
Primitive/Reference Types and Value Semantics
Understand Computer Storage and Data Types
2. Understanding VB Variables
Chapter 2.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Data Types and Expressions
Lecture 8.
Data Types Imran Rashid CTO at ManiWeber Technologies.
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Variables and Constants
Review of Java Fundamentals
Presentation transcript:

BİL528 – Bilgisayar Programlama II Variables

Contents Variables

Variables In your programs, you must determine the type of your data (int, string, etc.) In object oriented programming languages, you must also determine the level of visibility of your data (public, private, etc). This visibility is known as scope.

Visual C# Data Types In Visual C#, there are two categories of data types: Value types Reference types

Integral Value Types Type Approx. Range Size byte 0 to 255 1 byte sbyte –128 to 127 char U+0000 to U+FFFF 2 bytes short –32768 to 32767 ushort 0 to 65535 int –2,147,483,648 to 2,147,483,647 4 bytes uint 0 to 4,294,967,295 long –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 8 bytes ulong 0 to 18,446,744,073,709,551,615

Floating Value Types Type Approx. Range Precision Size float ±1.5 × 10−45 to ±3.4 × 1038 7 digits 32 bits double ±5.0 × 10−324 to ±1.7 × 10308 15-16 digits 64 bits decimal ±1.0 × 10−28 to ±7.9 × 1028 28-29 digits 128 bits * Use decimal for currencies.

Other Value Types Type Explanation bool Stores Boolean values, true and false enum Enumeration. Example: enum Days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; struct Lightweight version of class.

Reference Types Type Explanation string Unicode character arrays object Everything in C# is inherited from object class directly or indirectly class Blueprint of objects delegate Method signature (like function pointers in C) interface An interface contains only the signatures of methods, properties, events or indexers.

Type Casting There are two types of type casting: Implicit and Explicit Implicit conversions are done automatically by the compiler Widening cast No data loss Example: conversion from float to double Explicit conversions require programmers approval Narrowing cast Data may be lost Example: conversion from double to float

Safe Conversions Type Can Be Safely Converted To byte short, int, long, float, double, decimal short int, long, float, double, decimal int long, float, double, decimal long float, double, decimal float double decimal

Explicitly Converting Data Simplest way is using C syntax: double d = 2.3; int i = (int) d; You can also use Convert class: int i = Convert.ToInt32(d);

Some Common Methods of Convert Class Converts to ToBoolean bool ToByte byte ToSByte sbyte ToChar char ToDateTime DateTime ToInt16 short ToInt32 int ToInt64 long

Some Common Methods of Convert Class Converts to ToDecimal decimal ToDouble double ToSingle float ToString string ToUInt16 ushort ToUInt32 uint ToUInt64 ulong

Declaring Variables datatype variable_name = initial_value; int a; string str = "BIM211"; double m = 10, n = 20; long k, l = 100; Visual C# is a strongly typed language; therefore, you must always declare the data type of a variable. In addition, Visual C# requires that all variables be initialized before they’re used.

Where to put variables? Put variables in the class definition above the methods

Arrays string[] strMyArray; strMyArray = new string[10]; Accessing elements: strMyArray[0] = "BIM211"; strMyArray[1] = "Visual Programming";

Multidimensional Arrays int[,] intMeasurements; intMeasurements = new int[3, 2]; Accessing elements: intMeasurements[0, 0] = 1; intMeasurements[2, 1] = 6;

Constants You can define constant variables whose value can not be changed during the program execution by keyword const: const double PI = 3.14159;

Exercise Create a boolean variable to store whether a confirmation box appears or not Change its value when “Confirm on exit” menu item is clicked Handle the FormClosing event and write the following code: (see next slide)

FormClosing Event if (m_blnPromptOnExit) { if (MessageBox.Show(“Close the Picture Viewer program?”, “Confirm Exit”, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) e.Cancel = true; }

Arithmetic Operations Arithmetic operations are same as C but you may prefer to read chapter 12 of the textbook.

String Operations Length SubString() IndexOf() Trim(), TrimStart(), TrimEnd(), Remove() Replace()

Date/Time Operations DateTime dateMyBirthday = new DateTime(2008,7,22); AddDays(), AddHours(), etc. ToLongDateString() ToShortDateString() ToLongTimeString() ToShortTimeString()

Date/Time Operations DateTime.Today DateTime.Now