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
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
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
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.