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."); }