SlideShare a Scribd company logo
1 of 31
Download to read offline
C # Programming
Variables, Data types, Keywords, Identifiers, Comments
Program
As a programmer, after problem solving phase, next step is to code
the program-that is, to express our solution in a programming
language. We will translate the logic from the algorithm, flowchart
or pseudocode-or some other tool to a programming language.
Program Structure
Virtually all structured programs share a similar overall pattern:
➔ Statements to establish the start of the program
➔ Variable declaration
➔ Program statements (blocks of code)
2
C# - Comments
★ Comments can be used to explain C# code, and to make it more readable.
★ It can also be used to prevent execution when testing alternative code.
★ Two Types of Comments in C#:
○ Single-line Comments
■ Single-line comments start with two forward slashes (//).
■ Any text between // and the end of the line is ignored by C# (will not be
executed).
■ This example uses a single-line comment before a line of code:
3
C# - Comments
○ Multi-line Comments
■ Multi-line comments start with /* and ends with */.
■ Any text between /* and */ will be ignored by C#.
■ This example uses a multi-line comment (a comment block) to explain the
code:
4
C# - Keywords
➔ Keywords are predefined sets of reserved words that have special meaning in a
program.
➔ The meaning of keywords can not be changed, neither can they be directly used as
identifiers in a program.
➔ For example,
◆ long mobileNum;
● Here, long is a keyword and mobileNum is a variable (identifier). long
has a special meaning in C# i.e. it is used to declare variables of type
long and this function cannot be changed.
➔ Also, keywords like long, int, char, etc. can not be used as identifiers. So, we
cannot have something like:
◆ long long;
➔ C# has a total of 79 keywords.
5
6
7
C# - Keywords
➔ Although keywords are reserved words, they can be used as
identifiers if @ is added as prefix. For example,
◆ int @void;
8
C# - Identifiers
➔ Identifiers are the name given to entities such as variables, methods,
classes, etc.
➔ They are tokens in a program which uniquely identify an element. For
example,
◆ int value;
● Here, value is the name of variable. Hence it is an
identifier.
➔ Reserved keywords can not be used as identifiers unless @ is added as
prefix. For example,
◆ int break;
● This statement will generate an error in compile time.
9
Rules for Naming an Identifier
★ An identifier can not be a C# keyword.
★ An identifier must begin with a letter, an underscore or @ symbol.
○ The remaining part of identifier can contain letters, digits
and underscore symbol.
★ Whitespaces are not allowed. Neither it can have symbols other
than letter, digits and underscore.
★ Identifiers are case-sensitive. So, getName, GetName and
getname represents 3 different identifiers.
10
C# - Identifiers
➔ Here are some of the valid and invalid identifiers:
11
Example: Find list of keywords and identifiers in a program
12
Example: Find list of keywords and identifiers in a program
13
C# - Variables
➔ In computer programming, a variable is a storage location and an
associated symbolic name which contains some known or
unknown quantity or information, a value. It is helpful to think of
variables as containers that hold information.
➔ Every variable has a name, called the variable name, and a data
type. A variable's data type indicates what type of value the
variable represents, such as whether it is an integer, a floating-
point number, or a character.
14
Rules for Naming Variables in C#
There are certain rules we need to follow while naming a variable. The rules for
naming a variable in C# are:
➔ The variable name can contain letters (uppercase and lowercase),
underscore( _ ) and digits only.
➔ The variable name must start with either letter, underscore or @
symbol.
➔ C# is case sensitive. It means age and Age refers to 2 different
variables.
➔ A variable name must not be a C# keyword. For example, if, for,
using can not be a variable name.
15
16
Best Practices for Naming a Variable
1. Choose a variable name that make sense. For example, name, age,
subject makes more sense than n, a and s.
2. Use camelCase notation (starts with lowercase letter) for naming
local variables. For example, numberOfStudents, age, etc.
3. Use PascalCase or CamelCase (starts with uppercase letter) for
naming public member variables. For example, FirstName, Price,
etc.
4. Use a leading underscore (_) followed by camelCase notation for
naming private member variables. For example, _bankBalance,
_emailAddress, etc.
17
Practice Session
Using System
nameSpace Practice ;
{
class Practice Problem ;
Static void main ( String args )
{
Console . writeline ( “ Practice
Problem ” )
}
} ;
18
1. Identify the errors and correct them.
2. What are the keywords and identifiers?
Practice Session Solution
using System ;
namespace Practice
{
class PracticeProblem
{
static void Main ( string [] args )
{
Console . WriteLine ( “ Practice Problem ” ) ;
}
}
}
19
Keywords Identifiers
using System
namespace Practice
class PracticeProblem
static Main
void args
string Console
WriteLine
How to declare variables in C#?
➔ Here's an example to declare a variable in C#.
◆ int age;
● In this example, a variable age of type int (integer) is
declared and it can only store integer values.
➔ We can assign a value to the variable later in our program like
such:
◆ int age;
◆ ... ... …
◆ age = 24;
20
How to declare variables in C#?
➔ However, the variable can also be initialized to some value during
declaration. For example,
◆ int age = 24;
● Here, a variable age of type int is declared and
initialized to 24 at the same time.
➔ Since, it’s a variable, we can change the value of variables as well.
For example,
◆ int age = 24;
◆ age = 35;
● Here, the value of age is changed to 35 from 24.
21
Data Types
➔ A data type, in programming, is a classification that specifies
which type of value a variable has and what type of mathematical,
relational or logical operations can be applied to it without causing
an error.
➔ A string, for example, is a data type that is used to classify text
and an integer is a data type used to classify whole numbers.
22
Data Types
There are two types of data type
➔ Built-in data types
◆ Fundamental data types (int, char, float, double, boolean,
short,void)
◆ Derived data types (array, string, structure)
➔ Programmer-defined data types (Union, Enumeration)
23
24
Data
Type
Description Storage
size
Value
range
Example
void Used to denote the type with
no values
int Used to denote an integer
type.
2 bytes -32768 to
32767
Example: 10, -10
{
int age;
age = 20;
}
char Used to denote a character
type. A single character such
as a letter of the alphabet or
punctuation. Character
variables are letters of the
alphabet, ASCII characters
or numbers 0-9.
1 byte -128 to
127 or 0
to 255
Example: ‘A’, ‘b’
{
char Letter;
Letter = ‘H’
}
25
Data Type Description Storage
size
Value range Example
float Used to denote a floating
point type. It stores real or
fractional numbers (also
called float to indicate a
floating point number).
4 bytes 1.2E-38 to
3.4E+38
Example: 2.54, -35.5
{
float weight;
weight = 65.50;
}
Array A finite sequence of variables of the same data type.
String An array of character variables. We can say , a collection of characters.
Structure A collection of related variables of the same and/or different data types. The
structure is called a record and the variables in the record are called members or
fields.
26
Declaring a
string variable
Declaring an
int variable
C# Literals
Let's look at the following statement:
int number = 41;
Here,
★ int is a data type
★ number is a variable and
★ 41 is a literal
Literals are fixed values that appear in the program. They do not
require any computation. For example, 5, false, 'w' are literals that
appear in a program directly without any computation.
27
C# Expressions
● An expression in C# is a combination of operands (variables,
literals, method calls) and operators that can be evaluated to a
single value.
● To be precise, an expression must have at least one operand
but may not have any operator.
● The difference between expression and statement is that-
Expressions are evaluated to produce a value, whereas statements
are executed to perform an action or task.
28
Expressions are evaluated to produce a value, whereas statements are executed to perform an action or task.
1
Expressions are evaluated to produce a value, whereas statements are executed to perform an action or task.
C# Expressions
● Example:
29
C# Statements
● A statement is a basic unit of execution of a program.
● A program consists of multiple statements.
30
C# Blocks
● A block is a combination of zero or more statements
that is enclosed inside curly brackets { }.
31

More Related Content

Similar to L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf

INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxMamataAnilgod
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variablesyarkhosh
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageZubayer Farazi
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic conceptsAbhinav Vatsa
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types Hemantha Kulathilake
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C languageSachin Verma
 
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxNithya K
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticsfloraaluoch3
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introductionnikshaikh786
 

Similar to L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf (20)

PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
history of c.ppt
history of c.ppthistory of c.ppt
history of c.ppt
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming Language
 
C language ppt
C language pptC language ppt
C language ppt
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
COM1407: Variables and Data Types
COM1407: Variables and Data Types COM1407: Variables and Data Types
COM1407: Variables and Data Types
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
C tokens
C tokensC tokens
C tokens
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
 
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptx
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
C
CC
C
 

More from MMRF2

XRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.ppt
XRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.pptXRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.ppt
XRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.pptMMRF2
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfMMRF2
 
Presentation dlide Bus 201.pptx
Presentation dlide Bus 201.pptxPresentation dlide Bus 201.pptx
Presentation dlide Bus 201.pptxMMRF2
 
PDF document 7.pdf
PDF document 7.pdfPDF document 7.pdf
PDF document 7.pdfMMRF2
 
PDF document 6.pdf
PDF document 6.pdfPDF document 6.pdf
PDF document 6.pdfMMRF2
 
PDF document 5.pdf
PDF document 5.pdfPDF document 5.pdf
PDF document 5.pdfMMRF2
 
PDF document 4.pdf
PDF document 4.pdfPDF document 4.pdf
PDF document 4.pdfMMRF2
 
PDF document 3.pdf
PDF document 3.pdfPDF document 3.pdf
PDF document 3.pdfMMRF2
 
PDF document 2.pdf
PDF document 2.pdfPDF document 2.pdf
PDF document 2.pdfMMRF2
 
PDF document.pdf
PDF document.pdfPDF document.pdf
PDF document.pdfMMRF2
 

More from MMRF2 (10)

XRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.ppt
XRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.pptXRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.ppt
XRobbins_EOB10_Basic_PPT_CH04 entrepreneurship.ppt
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdf
 
Presentation dlide Bus 201.pptx
Presentation dlide Bus 201.pptxPresentation dlide Bus 201.pptx
Presentation dlide Bus 201.pptx
 
PDF document 7.pdf
PDF document 7.pdfPDF document 7.pdf
PDF document 7.pdf
 
PDF document 6.pdf
PDF document 6.pdfPDF document 6.pdf
PDF document 6.pdf
 
PDF document 5.pdf
PDF document 5.pdfPDF document 5.pdf
PDF document 5.pdf
 
PDF document 4.pdf
PDF document 4.pdfPDF document 4.pdf
PDF document 4.pdf
 
PDF document 3.pdf
PDF document 3.pdfPDF document 3.pdf
PDF document 3.pdf
 
PDF document 2.pdf
PDF document 2.pdfPDF document 2.pdf
PDF document 2.pdf
 
PDF document.pdf
PDF document.pdfPDF document.pdf
PDF document.pdf
 

Recently uploaded

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf

  • 1. C # Programming Variables, Data types, Keywords, Identifiers, Comments
  • 2. Program As a programmer, after problem solving phase, next step is to code the program-that is, to express our solution in a programming language. We will translate the logic from the algorithm, flowchart or pseudocode-or some other tool to a programming language. Program Structure Virtually all structured programs share a similar overall pattern: ➔ Statements to establish the start of the program ➔ Variable declaration ➔ Program statements (blocks of code) 2
  • 3. C# - Comments ★ Comments can be used to explain C# code, and to make it more readable. ★ It can also be used to prevent execution when testing alternative code. ★ Two Types of Comments in C#: ○ Single-line Comments ■ Single-line comments start with two forward slashes (//). ■ Any text between // and the end of the line is ignored by C# (will not be executed). ■ This example uses a single-line comment before a line of code: 3
  • 4. C# - Comments ○ Multi-line Comments ■ Multi-line comments start with /* and ends with */. ■ Any text between /* and */ will be ignored by C#. ■ This example uses a multi-line comment (a comment block) to explain the code: 4
  • 5. C# - Keywords ➔ Keywords are predefined sets of reserved words that have special meaning in a program. ➔ The meaning of keywords can not be changed, neither can they be directly used as identifiers in a program. ➔ For example, ◆ long mobileNum; ● Here, long is a keyword and mobileNum is a variable (identifier). long has a special meaning in C# i.e. it is used to declare variables of type long and this function cannot be changed. ➔ Also, keywords like long, int, char, etc. can not be used as identifiers. So, we cannot have something like: ◆ long long; ➔ C# has a total of 79 keywords. 5
  • 6. 6
  • 7. 7
  • 8. C# - Keywords ➔ Although keywords are reserved words, they can be used as identifiers if @ is added as prefix. For example, ◆ int @void; 8
  • 9. C# - Identifiers ➔ Identifiers are the name given to entities such as variables, methods, classes, etc. ➔ They are tokens in a program which uniquely identify an element. For example, ◆ int value; ● Here, value is the name of variable. Hence it is an identifier. ➔ Reserved keywords can not be used as identifiers unless @ is added as prefix. For example, ◆ int break; ● This statement will generate an error in compile time. 9
  • 10. Rules for Naming an Identifier ★ An identifier can not be a C# keyword. ★ An identifier must begin with a letter, an underscore or @ symbol. ○ The remaining part of identifier can contain letters, digits and underscore symbol. ★ Whitespaces are not allowed. Neither it can have symbols other than letter, digits and underscore. ★ Identifiers are case-sensitive. So, getName, GetName and getname represents 3 different identifiers. 10
  • 11. C# - Identifiers ➔ Here are some of the valid and invalid identifiers: 11
  • 12. Example: Find list of keywords and identifiers in a program 12
  • 13. Example: Find list of keywords and identifiers in a program 13
  • 14. C# - Variables ➔ In computer programming, a variable is a storage location and an associated symbolic name which contains some known or unknown quantity or information, a value. It is helpful to think of variables as containers that hold information. ➔ Every variable has a name, called the variable name, and a data type. A variable's data type indicates what type of value the variable represents, such as whether it is an integer, a floating- point number, or a character. 14
  • 15. Rules for Naming Variables in C# There are certain rules we need to follow while naming a variable. The rules for naming a variable in C# are: ➔ The variable name can contain letters (uppercase and lowercase), underscore( _ ) and digits only. ➔ The variable name must start with either letter, underscore or @ symbol. ➔ C# is case sensitive. It means age and Age refers to 2 different variables. ➔ A variable name must not be a C# keyword. For example, if, for, using can not be a variable name. 15
  • 16. 16
  • 17. Best Practices for Naming a Variable 1. Choose a variable name that make sense. For example, name, age, subject makes more sense than n, a and s. 2. Use camelCase notation (starts with lowercase letter) for naming local variables. For example, numberOfStudents, age, etc. 3. Use PascalCase or CamelCase (starts with uppercase letter) for naming public member variables. For example, FirstName, Price, etc. 4. Use a leading underscore (_) followed by camelCase notation for naming private member variables. For example, _bankBalance, _emailAddress, etc. 17
  • 18. Practice Session Using System nameSpace Practice ; { class Practice Problem ; Static void main ( String args ) { Console . writeline ( “ Practice Problem ” ) } } ; 18 1. Identify the errors and correct them. 2. What are the keywords and identifiers?
  • 19. Practice Session Solution using System ; namespace Practice { class PracticeProblem { static void Main ( string [] args ) { Console . WriteLine ( “ Practice Problem ” ) ; } } } 19 Keywords Identifiers using System namespace Practice class PracticeProblem static Main void args string Console WriteLine
  • 20. How to declare variables in C#? ➔ Here's an example to declare a variable in C#. ◆ int age; ● In this example, a variable age of type int (integer) is declared and it can only store integer values. ➔ We can assign a value to the variable later in our program like such: ◆ int age; ◆ ... ... … ◆ age = 24; 20
  • 21. How to declare variables in C#? ➔ However, the variable can also be initialized to some value during declaration. For example, ◆ int age = 24; ● Here, a variable age of type int is declared and initialized to 24 at the same time. ➔ Since, it’s a variable, we can change the value of variables as well. For example, ◆ int age = 24; ◆ age = 35; ● Here, the value of age is changed to 35 from 24. 21
  • 22. Data Types ➔ A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. ➔ A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers. 22
  • 23. Data Types There are two types of data type ➔ Built-in data types ◆ Fundamental data types (int, char, float, double, boolean, short,void) ◆ Derived data types (array, string, structure) ➔ Programmer-defined data types (Union, Enumeration) 23
  • 24. 24 Data Type Description Storage size Value range Example void Used to denote the type with no values int Used to denote an integer type. 2 bytes -32768 to 32767 Example: 10, -10 { int age; age = 20; } char Used to denote a character type. A single character such as a letter of the alphabet or punctuation. Character variables are letters of the alphabet, ASCII characters or numbers 0-9. 1 byte -128 to 127 or 0 to 255 Example: ‘A’, ‘b’ { char Letter; Letter = ‘H’ }
  • 25. 25 Data Type Description Storage size Value range Example float Used to denote a floating point type. It stores real or fractional numbers (also called float to indicate a floating point number). 4 bytes 1.2E-38 to 3.4E+38 Example: 2.54, -35.5 { float weight; weight = 65.50; } Array A finite sequence of variables of the same data type. String An array of character variables. We can say , a collection of characters. Structure A collection of related variables of the same and/or different data types. The structure is called a record and the variables in the record are called members or fields.
  • 27. C# Literals Let's look at the following statement: int number = 41; Here, ★ int is a data type ★ number is a variable and ★ 41 is a literal Literals are fixed values that appear in the program. They do not require any computation. For example, 5, false, 'w' are literals that appear in a program directly without any computation. 27
  • 28. C# Expressions ● An expression in C# is a combination of operands (variables, literals, method calls) and operators that can be evaluated to a single value. ● To be precise, an expression must have at least one operand but may not have any operator. ● The difference between expression and statement is that- Expressions are evaluated to produce a value, whereas statements are executed to perform an action or task. 28 Expressions are evaluated to produce a value, whereas statements are executed to perform an action or task. 1 Expressions are evaluated to produce a value, whereas statements are executed to perform an action or task.
  • 30. C# Statements ● A statement is a basic unit of execution of a program. ● A program consists of multiple statements. 30
  • 31. C# Blocks ● A block is a combination of zero or more statements that is enclosed inside curly brackets { }. 31