Presentation is loading. Please wait.

Presentation is loading. Please wait.

3. Declaring Value-Type Variables

Similar presentations


Presentation on theme: "3. Declaring Value-Type Variables"— Presentation transcript:

1 3. Declaring Value-Type Variables
Computer Programming 1

2 C# integer variable types

3 C# Floating point variable types
Method variable in C# MinValue Display minimum value of variable in C# MaxValue Display maximum value of variable in C#

4 using System; namespace MaxMinVariable { public class Program static void Main(string[] args) Console.WriteLine(“min sbyte =” + SByte.MinValue); Console.WriteLine(“max sbyte =” + SByte.MaxValue); Console.WriteLine(“min byte =” + Byte.MinValue); Console.WriteLine(“max byte =” + Byte.MaxValue); Console.WriteLine(“min shot =” + Int16.MinValue); Console.WriteLine(“max shot =” + Int16.MaxValue); Console.WriteLine(“min ushot =” + UInt16.MinValue); Console.WriteLine(“max ushot =” + UInt16.MaxValue); Console.WriteLine(“min int =” + Int32.MinValue); Console.WriteLine(“max int =” + Int32.MaxValue); Console.WriteLine(“min uint =” + UInt32.MinValue); Console.WriteLine(“max uint =” + UInt32.MaxValue); Console.WriteLine(“min long=” + Int64.MinValue); Console.WriteLine(“max long =” + Int64.MaxValue); Console.WriteLine(“min ulong =” + UInt64.MinValue); Console.WriteLine(“max ulong =” + UInt64.MaxValue); Console.Read(); }

5 Variable name of raw English name and meaning
First name is character and then is character or member, or insert underscore above it. ex. Ant, A1200, fName, Last_name, etc. Not space in variables Not Reserved Word Nor Special character # * % / ? < > $ ^ & ( ) + ‘ “

6 Example of integer & floating point variable
using System; namespace ExVariable { public class Program static void Main(string[] args) Console.WriteLine(20); Console.WriteLine(020); Console.WriteLine(0x20); Console.WriteLine( ”); Console.WriteLine( L”); Console.WriteLine( f); Console.WriteLine( d); Console.Read(); } Result

7 Boolean Variable True False Example of Boolean variable using System;
namespace ExVariable { public class Program static void Main(string[] args) Console.WriteLine(true);          Console.WriteLine(false);          Console.WriteLine(1 < 2);          Console.WriteLine('a' == 'b'); Console.Read(); } Result true false true false

8 Character Variable Char char sex = ‘M’ Example of character variable
using System; namespace ExCharVariable { public class Program static void Main(string[] args)          Console.WriteLine("new line [{0}]", '\n');          Console.WriteLine("tab [{0}]", '\t');          Console.WriteLine("single quote [{0}]", '\''); Console.Read(); } Result new line [ ] tab [        ] single quote [']

9 Special char types Some characters within a given font are not printable in the sense that you don’t see anything when you look at them on the computer screen or printer. The most obvious example of this is the space, which is represented by the character ‘ ‘ (single quote, space, single quote).

10 escaped sequence of Unicode character
escaped sequence of Unicode character if you write\u follow hexadecimal number 4 digit. Example of escaped sequence variable using System; namespace ExCharVariable { public class Program static void Main(string[] args)          System.Console.WriteLine('\u0061');         System.Console.WriteLine("\u0048\u0065\u006c\u006c\u006f");          System.Console.WriteLine('\u0e01'); Console.Read(); } Result a Hello ก

11 String Variable Another common variable type is the string.
The following examples show how you declare and initialize string variables: // declare now, initialize later string someString1; someString1 = “this is a string”; // or initialize when declared string someString2 = “this is a string”; string someString = “This is a line\nand so is this”; string s = “this is a phrase”+ “ and so is this”; string s1 = “a”; string s2 = “b”; string s3 = s1 + s2; // result is “ab”

12 Declaring Numeric Constants

13 Changing Types Humans don’t treat different types of counting numbers differently. For example, a normal person (as distinguished from a C# programmer) doesn’t think about the number 1 as being signed, unsigned, short, or long. Although C# considers these types to be different, even C# realizes that a relationship exists between them. For example, the following code converts an int into a long: // this is OK // this is illegal int nValue = 10; long lValue = 10; long lValue; int nValue; lValue = nValue; nValue = lValue; But what if you know that the conversion is okay? // this is now OK long lValue = 10; double dValue = 10.0; int nValue; long lValue = (long) dValue; nValue = (int) lValue;

14 Implicit Changing Types
sbyte shot, int, long, float,double, decimal byte shot, ushot, int, uint, long, ulong, float,double, decimal shot int, long, float,double, decimal ushot int, uint, long, ulong, float,double, decimal int long, float,double, decimal uint long, ulong, float,double, decimal long loat,double, decimal char ushot, int, uint, long, ulong, float,double, decimal float double example int n1 = 10 ; double d2 = 5.0 ; int nResult = n1 * (int) d2 ;

15 Changing Types between Number and String
C# don’t direct a numeric to input method . In this last chapter C# have String input method , so C# to design method Parse for to change variable in .NET Framework . Syntax : <Numeric data type(.NET)> . Parse(<string expressing>) .NET Numeric Decimal Double Single Int16 Int32 Int64 UInt16 UInt32 UInt64 Byte SByte C# Name decimal double float short int long ushort uint ulong byte sbyte

16 Example of Changing Types between Number and String. x = Int32
Example of Changing Types between Number and String x = Int32.Parse(str); y = Double.Parse(Console.ReadLine()); Example using System; namespace ExChgNumStr { public class Program static void Main(string[] args) double num1, num2, sum; Console.Write(“Enter Num 1 : ”); num1 = Double.Parse(Console.ReadLine()); Console.Write(“Enter Num 2 : ”); num2 = Double.Parse(Console.ReadLine()); sum = num1 + num2; Console.WriteLine(“{0} + {1} = {2}”,num1,num2,sum); Console.Read(); } Result Enter Num 1 : Enter Num 2 : = 5.9

17 Changing Types between String and Number
In display output to C# on Console or Windows application will display output to String variable. So that if the data to display output is numeric and wanted to show in display, you will be to convert from numeric to String. This method is ToString in class Convert Syntax: Convert. ToString(<string>); Example: num = 2 + 3; str1 = Convert. ToString(num);

18 Ex. Changing Types between String and Number
Example using System; namespace ExChgStrNum { public class Program static void Main(string[] args) int num1, num2, sum; string str1; num1 = 35; num2 = 25; sum = num1 + num2; str1 = Convert. ToString(sum); Console.WriteLine(“num1 + num2 = ”); Console.WriteLine(str1); Console.Read(); } Result num1 + num2 = 60


Download ppt "3. Declaring Value-Type Variables"

Similar presentations


Ads by Google