Lesson 4 Basic Programming Constructs.pptx

JLMagdaraog 248 views 30 slides Jun 06, 2024
Slide 1
Slide 1 of 30
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30

About This Presentation

Grade 10 Lesson 1 Quarter 4


Slide Content

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,615 64-bit unsigned integer type decimal Use this data type for currency value (-7.9 x 10 28 to 7.9 x 10 28 ) /10 to 28 128-bit precise decimal values with 28-29 significant digits

Type Description Range Represent float Floating point number -3.4 x 10 38 to +3.4 x 10 38 32-bit single -precision floating point type Double Double floating point (+/-) 5.0 x 10 -324 to (+/-)1.7 x 10 308 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

//Declare and initialize some variables byte centuries = 20; 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
Tags