[go: up one dir, main page]

0% found this document useful (0 votes)
18 views55 pages

Chapter 2 Updated

C# variables can store values of different data types. Variables are named locations in memory and follow specific naming conventions. There are value and reference data types in C#. Value types directly contain data while reference types contain a memory address. Operators perform actions on operands and have a precedence order for evaluating expressions. The document provides examples of variables, data types, and operators in C# programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views55 pages

Chapter 2 Updated

C# variables can store values of different data types. Variables are named locations in memory and follow specific naming conventions. There are value and reference data types in C#. Value types directly contain data while reference types contain a memory address. Operators perform actions on operands and have a precedence order for evaluating expressions. The document provides examples of variables, data types, and operators in C# programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Computer Programming 2

C# Programming Language

Prepared by: Joshua Relatorres


C# VARIABLES DATA TYPES,
OPERATORS AND EXPRESSIONS
(THE BASICS)
Objectives:

• Understand the concept of variables and its Naming Conventions


• Understand the Concept of Data Types
• Learn Constant and Literals
• Learn about C#’s Operators and its Precedence
C# VARIABLES
C# VARIABLES

• The elements that facilitate the storage of characters and numbers in the
random-access memory.

• A name given to a memory location, that is used to hold a value.


• A symbol you can use to run the same code with different values.
VARIABLES – NAMING CONVENTION

• A combination of alphabetic characters, numeric digits, and the underscore could be used.
• The first character in the name may not be a numeric.
• No spaces can be placed between characters.
• Reserved words like keywords cannot be use.
• The at sign (@) could be use as the first character.
• Case sensitive
• Use meaningful names that represent the item being described.
VARIABLES – NAMING CONVENTION

• Use PascalCasing for class names and method names.


• Use camelCasing for method arguments and local variables.
• Do not use Hungarian notation or any other type identification in identifiers.
• Do not use Screaming Caps for constants or readonly variables.
• Avoid using Abbreviations.
• Do not use Underscores in identifiers.

More on: https://github.com/ktaranov/naming-convention/blob/master/C%23%20Coding%20Standards%20and%20Naming%20Conventions.md


VARIABLES - DECLARATION
Variable could be declared using <datatype> <variableName> = <value>;

A red underline shows that there’s an error in the specific code. This shows that the fullname is
not been declared.
VARIABLES - DECLARATION

A green underline shows that the specific variable is not used in the entire
Program.
VARIABLES - DECLARATION

• Compile-time initialization - when a variable is declared together with


assigning a value to them using <datatype> <variableName> =
<value>;.

• Run-time initialization - the user has to enter the value and that value is
copied to the required variable.
C# DATATYPES
DATATYPES

C# mainly categorized data types in two


types:
• Value Data types - include simple types
(such as int, float, bool, and char), enum
types, struct types, and Nullable value
types.
• Reference Data types - include class
types, interface types, delegate types, and
array types.
DATATYPES – VALUE DATATYPES

Value Data type- Holds a data value within its own memory space. It means
variables of these data types directly contain their values.
DATATYPES – VALUE DATATYPES

• There are 2 types of value data type in C# language.


1. Predefined Data Types - such as Integer, Boolean, Float, etc.
2. User defined Data Types - such as Structure, Enumerations, etc.
• The memory size of data types may change according to 32-bit or 64-bit
operating system.
DATATYPES – VALUE DATATYPES

SIGNED UNSIGNED
Indicates that a variable can hold negative and Indicates a variable that can hold only positive
positive values. numbers.
DATATYPES – VALUE DATATYPES

CHAR AND BOOLEAN

Char – indicates a variable that can hold a single


Unicode character.

FLOATING Bool – indicates a variable that can hold true or false.

Indicates a variable that can hold numbers with one or


more decimal points. It can be negative or positive
numbers.
DATATYPES – VALUE DATATYPES
Alias and its Type Name
DATATYPES – REFERENCE DATATYPES

Reference Data type- Do not contain the actual data stored in a variable, but it
contains a pointer to another memory location that holds the data.
DATATYPES – REFERENCE DATATYPES

• There are 2 types of reference data type in C# language.


1. Predefined Data Types - such as Objects and String.
2. User defined Data Types - such as Classes, Interface
DATATYPES

var type can be used to store a simple .NET data type.


DATATYPES

A variable could be re used but its value will be overwritten.


DATATYPES

Implicitly declared variables could store different kind of datatype depending on the first value
it received.
DATATYPE CONVERSION
IMPLICIT CONVERSION
EXPLICIT CONVERSION

• Int.parse()
• Convert.ToInt32()
• (int)variable
OUTPUT FORMATTING
STRING OUTPUT FORMATTING

• Plus sign (+) is used to build


strings from variables and
constant strings.
• Dollar sign ($) special character
identifies a string literal as an
interpolated string. An
interpolated string is a string
literal that might contain
interpolation expressions.
NUMERIC OUTPUT FORMATTING

• Currency ("C") Format


Specifier - converts a
number to a string that
represents a currency
amount. The precision
specifier indicates the
desired number of
decimal places in the
result string.
NUMERIC OUTPUT FORMATTING

• Decimal ("D") Format


Specifier - converts a
number to a string of
decimal digits (0-9),
prefixed by a minus sign
if the number is negative.
This format is supported
only for integral types.
NUMERIC OUTPUT FORMATTING

• Exponential ("E") Format


Specifier - converts a number
to a string of the form "-
d.ddd…E+ddd" or "-d.ddd…
e+ddd", where each "d“
indicates a digit (0-9). The
string starts with a minus sign
if the number is negative.
Exactly one digit always
precedes the decimal point.
NUMERIC OUTPUT FORMATTING

• Fixed-Point ("F") Format


Specifier - converts a
number to a string of the
form "-ddd.ddd…" where
each "d" indicates a digit
(0-9). The string starts
with a minus sign if the
number is negative.
NUMERIC OUTPUT FORMATTING

• Custom numeric format strings


• You can create a custom numeric
format string, which consists of
one or more custom numeric
specifiers, to define how to
format numeric data.
• A custom numeric format string
is any format string that is not a
standard numeric format string.
CONSTANT AND LITERALS
CONSTANTS

• Refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals.
• Itcan be of any of the basic data types like an integer constant, a floating
constant, a character constant, or a string literal.
• Itis treated just like regular variables except that their values cannot be
modified after their definition.
CONSTANTS

Constants could be declared using;


<const keyword> <datatype> <variablename> = <value>;.
LITERALS

• A literal is a fixed value that is explicitly written in your code.


• Literals are used directly in expressions or assignments in your code.
• Common examples of literals include numeric literals (e.g., 5, 3.14), character
literals (e.g., 'A', '\n'), string literals (e.g., "Hello, World!"), and boolean
literals (e.g., true, false).
• Literals are used when you want to specify a constant value directly in your
code without assigning it to a variable or using it in an expression.
LITERALS

int age = 30; // 30 is a numeric literal


char grade = 'A'; // 'A' is a character literal
string message = "Hello, World!"; // "Hello, World!" is a string literal
bool isStudent = true; // true is a boolean literal
CONSTANTS & LITERALS – KEY
DIFFERENCES

• Constants are declared using the const keyword, while literals are
values used directly in code without any special keyword.
• Constants must be assigned a value at the time of declaration and
cannot be changed afterward. Literals are used directly as values.
• Constants are typically used for values that should remain fixed
throughout the program, while literals are used for temporary, directly
specified values.
OPERATORS AND ITS
PRECEDENCE
OPERATORS

• Operators in C# are some special symbols that perform some action on


operands.
• In mathematics, the plus symbol (+) do the sum of the left and right numbers.
In the same way, C# includes various operators for different types of
operations.
OPERATORS - ASSIGNMENT OPERATORS

The assignment operator (=) assigns its right-hand value to its left-hand
variable, property, or indexer. It can also be used with other arithmetic, Boolean
logical, and bitwise operators.
OPERATORS - ARITHMETIC OPERATORS

The arithmetic operators perform arithmetic operations on all the numeric type
operands such as sbyte, byte, short, ushort, int, uint, long, ulong, float, double,
and decimal.
OPERATORS - ARITHMETIC OPERATORS
OPERATORS – COMPOUND ASSIGNMENT OPERATORS

A compound assignment operator has a shorter syntax to assign the result. The
operation is performed on the two operands before the result is assigned to the
first operand.
OPERATORS – COMPOUND ASSIGNMENT OPERATORS
OPERATORS – COMPARISON/RELATIONAL OPERATORS

Comparison operators compare two numeric operands and returns true or false.
It is used in conditional statements, especially in loops, where the result of the
comparison decides whether execution should proceed.
OPERATORS – COMPARISON/RELATIONAL OPERATORS
OPERATORS – COMPARISON/RELATIONAL OPERATORS
OPERATORS – EQUALITY OPERATORS

The equality operator checks whether the two operands are equal or not.
OPERATORS – EQUALITY OPERATORS

Example:
OPERATORS - BOOLEAN LOGICAL
OPERATORS

• The Boolean logical operators perform a logical operation on bool operands.


• The operands in logical operators must always contain only Boolean values.
Otherwise, Logical Operators will throw an error.
OPERATORS - BOOLEAN LOGICAL
OPERATORS
OPERATORS - BOOLEAN LOGICAL
OPERATORS
Examples:
OPERATORS – OPERATOR PRECEDENCE

Operator precedence is a set of rules which defines how an expression is


evaluated. In C#, each C# operator has an assigned priority and based on these
priorities, the expression is evaluated.

Example:
bool x = 2+3 > 1*4 && 5/5 == 1;
OPERATORS – OPERATOR PRECEDENCE

You might also like