Basic Programming Constructs
Variable
Programmer-defined word that holds the value of the
user.
Portion of the memory to store a determined value.
Rules in Declaring variables
 It must begin with a letter or underscore (_), then followed by a letter, digit, or underscore.
 It must not include blanks.
 It must not be the keywords, which are reserved words by the language.
 It must not be given a name with the same name as other functions.
Constant
 Expression with a fixed value that cannot be changed at runtime.
 To declare a constant in C#, the const keyword is used.
 To define a constant, this syntax should be followed:
 Const <data_type> <constant_name> = value;
Literals
 Values within the source code of a program.
 Examples a= 5; 5 in this piece of code is a literal constant.
 Literal Constant can be integer numerals, floating-pointing numerals, characters, strings , or
Boolean values.
Integer literals
 Constant and are also called decimal numerals.
 For example
235
612
18
29
Floating-point literal
 Numbers with decimals, fractions, and /or exponents.
 For Example:
3.14159 // 3.14159
6.02e23 // 6.02 x 10
Character and String
 Non-numeric constant like
‘z’ – character
‘j’ – character
‘a’ – character
“jasmin” – string
“Hello World” - string
Data type
 Variable allocated in the computer’s memory.
 Variable should be identified based on the type of data it can hold.
 Data types categorized as value types and reference type.
Value type
 Holds data within its own memory location
Reference type
 Contains a pointer to another memory location that holds the data.
int a = 42;
char ch = ‘A’;
bool result = true; Value types
object obj = 42;
string str = “Hello”;
byte[] bytes = {1,2,3}; - Reference type
Characteristics of a Data type
 Name – value type
 Size – how much memory they use
 Default value – the initial value
Type Description Range Represent
bool Represent True or false True or False Boolean Value
sbyte Signed byte -128 to 127 8-bit signed integer type
byte Unsigned byte 0 to 255 8-bit unsigned integer
short Short integer -32,768 to 32,767 16-bit signed integer type
ushort Unsigned short integer 0 to 65,535
16-bit unsigned integer
type
int Integer
-2,147,483,648 to
2,147,483,647
32-bit signed integer type
uint Unsigned integer 0 to 4,294,967,295
32-bit unsigned integer
type
long 64-bit signed integer type
-
9,223,372,036,854,775,808
to
9,223,372,036,854,775,808
64-bit signed integer type
ulong Unsigned long integer
0 to
18,446,744,073,709,551,61
5
64-bit unsigned integer
type
decimal
Use this data type for
currency value
(-7.9 x 1028to 7.9 x 1028
) /10 0 to 28
128-bit precise decimal
values with 28-29
significant digits
Type Description Range Represent
float Floating point number
-3.4 x 1038 to +3.4 x 1038 32-bit single-precision
floating point type
Double Double floating point
(+/-) 5.0 x 10-324 to (+/-
)1.7 x 10308 64-bit double-precision
floating point type
Char Unicode character Any valid character
16-bit Unicode
character
 C# supports unsigned data types for short, long and int which uses letter ‘u’ + data type for
identification
 Unsigned used to exclude the negative sign while signed data types are value can have a negative
number
 Example int -247 and Uint 247
Reference types
Reference type Description Size
Object Base type of all other types 8+ bytes
String Character array 20+bytes
Reference types
 A variable is declared like this:
<data type><variable>;
 Example:
string name;
int a,b,c;
 Variable can have an initial value by adding a value to it during its declaration. It is declared as:
 <Data type > <variable name> = value;
 Example
int a=5;
double B= 2.9;
double pi= 3.1416
ushort years= 2000;
decimal decimalPI= 3.141592653589793238m;
bool isEmpty= true;
char ch= ‘a’;
string firstName =“John”;
ch = (char)5;
char secondChar;
// Here we use an already initialized variable and reassign
it
secondchar = ch;
What is a Console
Console
A window, which users can interact with in a system program.
Interaction can be by text input from standard input (keyboard),
text display standard output (monitor), or simply input-output
operations.
System.Console class has different properties and methods
which are used to read and display text on the console as well
as its formatting.
What are C# Input/Output
Statement
I/O statement
Used for user and computer interaction.
It uses the standard input stream Console.
Print information on the standard output stream Console.Out,
and can signal for problem situations in the standard error
stream Console.Error.
Example: Console.Out.WriteLine(“Hello World”);
Output: Hello World
Console.Write/WriteLine
Method that can print all basic types (string, numeric, and
primitive type).
Console.Write writes the text and places the cursor after the last
character while the Console.
WriteLine writes the text and places the cursor to the next line.
Example
// Print String
Console.WriteLine(“Hello World);
//Print int
Console.WriteLine(5);
// Print Double
Console.WriteLine (3.141512412412)
Output
Hello World
5
3.141512412412
String Concatenation
 Combining two strings and returns as a result of a new string.
 C# uses ‘+’ operator to concatenate two strings.
 For example:
string age = “twenty-six”;
String text = “ He is” + age + “years old”;
Console.WriteLine(text);
Output: He is twenty-six years old.
Mixed String Concatenation
 Concatenation can be done alternately with a variable.
 Composed of an output text followed by a variable then another output
text.
 Example
int age = 26;
String text = “He is “ + age + “years old. “ ;
Console.WriteLine(text);
Console.Read/ReadLine()
More Example
Lesson 4 Basic Programming Constructs.pptx

Lesson 4 Basic Programming Constructs.pptx

  • 1.
  • 2.
    Variable Programmer-defined word thatholds the value of the user. Portion of the memory to store a determined value.
  • 3.
    Rules in Declaringvariables  It must begin with a letter or underscore (_), then followed by a letter, digit, or underscore.  It must not include blanks.  It must not be the keywords, which are reserved words by the language.  It must not be given a name with the same name as other functions.
  • 4.
    Constant  Expression witha fixed value that cannot be changed at runtime.  To declare a constant in C#, the const keyword is used.  To define a constant, this syntax should be followed:  Const <data_type> <constant_name> = value;
  • 5.
    Literals  Values withinthe source code of a program.  Examples a= 5; 5 in this piece of code is a literal constant.  Literal Constant can be integer numerals, floating-pointing numerals, characters, strings , or Boolean values.
  • 6.
    Integer literals  Constantand are also called decimal numerals.  For example 235 612 18 29
  • 7.
    Floating-point literal  Numberswith decimals, fractions, and /or exponents.  For Example: 3.14159 // 3.14159 6.02e23 // 6.02 x 10
  • 8.
    Character and String Non-numeric constant like ‘z’ – character ‘j’ – character ‘a’ – character “jasmin” – string “Hello World” - string
  • 9.
    Data type  Variableallocated in the computer’s memory.  Variable should be identified based on the type of data it can hold.  Data types categorized as value types and reference type.
  • 10.
    Value type  Holdsdata within its own memory location
  • 11.
    Reference type  Containsa pointer to another memory location that holds the data. int a = 42; char ch = ‘A’; bool result = true; Value types object obj = 42; string str = “Hello”; byte[] bytes = {1,2,3}; - Reference type
  • 12.
    Characteristics of aData type  Name – value type  Size – how much memory they use  Default value – the initial value
  • 13.
    Type Description RangeRepresent bool Represent True or false True or False Boolean Value sbyte Signed byte -128 to 127 8-bit signed integer type byte Unsigned byte 0 to 255 8-bit unsigned integer short Short integer -32,768 to 32,767 16-bit signed integer type ushort Unsigned short integer 0 to 65,535 16-bit unsigned integer type int Integer -2,147,483,648 to 2,147,483,647 32-bit signed integer type uint Unsigned integer 0 to 4,294,967,295 32-bit unsigned integer type long 64-bit signed integer type - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 64-bit signed integer type ulong Unsigned long integer 0 to 18,446,744,073,709,551,61 5 64-bit unsigned integer type decimal Use this data type for currency value (-7.9 x 1028to 7.9 x 1028 ) /10 0 to 28 128-bit precise decimal values with 28-29 significant digits
  • 14.
    Type Description RangeRepresent float Floating point number -3.4 x 1038 to +3.4 x 1038 32-bit single-precision floating point type Double Double floating point (+/-) 5.0 x 10-324 to (+/- )1.7 x 10308 64-bit double-precision floating point type Char Unicode character Any valid character 16-bit Unicode character
  • 15.
     C# supportsunsigned data types for short, long and int which uses letter ‘u’ + data type for identification  Unsigned used to exclude the negative sign while signed data types are value can have a negative number  Example int -247 and Uint 247
  • 16.
    Reference types Reference typeDescription Size Object Base type of all other types 8+ bytes String Character array 20+bytes
  • 17.
    Reference types  Avariable is declared like this: <data type><variable>;  Example: string name; int a,b,c;  Variable can have an initial value by adding a value to it during its declaration. It is declared as:  <Data type > <variable name> = value;  Example int a=5; double B= 2.9; double pi= 3.1416
  • 18.
    ushort years= 2000; decimaldecimalPI= 3.141592653589793238m; bool isEmpty= true; char ch= ‘a’; string firstName =“John”; ch = (char)5; char secondChar; // Here we use an already initialized variable and reassign it secondchar = ch;
  • 19.
    What is aConsole
  • 20.
    Console A window, whichusers can interact with in a system program. Interaction can be by text input from standard input (keyboard), text display standard output (monitor), or simply input-output operations. System.Console class has different properties and methods which are used to read and display text on the console as well as its formatting.
  • 21.
    What are C#Input/Output Statement
  • 22.
    I/O statement Used foruser and computer interaction. It uses the standard input stream Console. Print information on the standard output stream Console.Out, and can signal for problem situations in the standard error stream Console.Error. Example: Console.Out.WriteLine(“Hello World”); Output: Hello World
  • 23.
    Console.Write/WriteLine Method that canprint all basic types (string, numeric, and primitive type). Console.Write writes the text and places the cursor after the last character while the Console. WriteLine writes the text and places the cursor to the next line.
  • 24.
    Example // Print String Console.WriteLine(“HelloWorld); //Print int Console.WriteLine(5); // Print Double Console.WriteLine (3.141512412412)
  • 25.
  • 26.
    String Concatenation  Combiningtwo strings and returns as a result of a new string.  C# uses ‘+’ operator to concatenate two strings.  For example: string age = “twenty-six”; String text = “ He is” + age + “years old”; Console.WriteLine(text); Output: He is twenty-six years old.
  • 27.
    Mixed String Concatenation Concatenation can be done alternately with a variable.  Composed of an output text followed by a variable then another output text.  Example int age = 26; String text = “He is “ + age + “years old. “ ; Console.WriteLine(text);
  • 28.
  • 29.