0% found this document useful (0 votes)
14 views

2.Basic Programming Conceptssss

Chapter 02 covers basic programming concepts in C#, including object-oriented programming methodology, syntax rules for identifiers, and types of variables such as local, instance, static, and read-only variables. It also explains constants, operators (arithmetic, relational, logical, bitwise), and input/output operations using the Console class. Additionally, the chapter details data types, their characteristics, and the importance of specifying data types in programming.

Uploaded by

mahdiware
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)
14 views

2.Basic Programming Conceptssss

Chapter 02 covers basic programming concepts in C#, including object-oriented programming methodology, syntax rules for identifiers, and types of variables such as local, instance, static, and read-only variables. It also explains constants, operators (arithmetic, relational, logical, bitwise), and input/output operations using the Console class. Additionally, the chapter details data types, their characteristics, and the importance of specifying data types in programming.

Uploaded by

mahdiware
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/ 28

Chapter 02:

Basic Programming Concepts


Basic Syntax

 In OOP Methodology a program consists of various


objects that interact with each other by means of
actions.
 Member Variables
 Variables are attributes or data members of a class. They are used for
storing data

 Member Functions
 Functions are set of statements that perform a specific task.

 Instantiating a Class
 In the preceding program, the class ExecuteRectangle is used as a
class, which contains the Main() method and instantiates the Rectangle
class.
Basic Syntax

 Identifiers
 An identifier is a name used to identify a class, variable,
function, or any other user defined item. The basic rules for
naming classes in C# are as follows:
 A name must begin with a letter that could be followed by a
sequence of letters, digits (0 - 9), or underscore. The first
character in an identifier cannot be a digit.

 It must not contain any embedded space or symbol like ? -


+! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \. However, an
underscore ( _ ) can be used.
 It should not be a C# keyword.
Variables in C#

 A variable is a named storage location that holds a


value, which can be changed during program execution.
 dataType variableName = value;
 int age = 25; // Integer variable
 double price = 99.99; // Floating-point variable
 string name = "John"; // String variable
 bool isActive = true; // Boolean variable
Types of Variables

 a) Local Variables
void ExampleMethod()
{
int num = 10; // Local variable
Console.WriteLine(num);
}
 b) Instance Variables (Fields)
class Person
{
public string name; // Instance variable
}
Types of Variables

 c) Static Variables
class Counter
{
public static int count = 0; // Static variable
}
d) Read-Only Variables
class Sample
{
public readonly int myValue;
public Sample(int value)
{
myValue = value;
}
}
Constants in C#

 A constant is a variable whose value cannot be changed after it is assigned.

Syntax of Constant
const dataType constantName = value;

Example
const int MAX_USERS = 100; // Constant integer
Key Points about Constants

•Must be initialized at the time of declaration.


•Cannot be modified later.
•Are implicitly static, so they belong to the class and not an instance .
Operators in C#

Operators in C# are special symbols that perform operations on


variables and values.
C# provides different types of operators, categorized based on their
functionality.
1. Arithmetic Operators
These operators perform mathematical calculations.
Example

int a = 10, b = 5;
Console.WriteLine(a + b); // Output: 15
Console.WriteLine(a - b); // Output: 5
Relational (Comparison)
Operators

Used to compare values and return a bool (true/false).


Example

int a = 10, b = 5;
Console.WriteLine(a > b); // Output: True
Console.WriteLine(a == b); // Output: False
Logical Operators

Used for boolean (true/false) expressions.


Example

bool x = true, y = false;


Console.WriteLine(x && y); // Output: False
Console.WriteLine(x || y); // Output: True
Console.WriteLine(!x); // Output: False
Bitwise Operators

Perform operations on binary numbers.

Example

int a = 5, b = 3;
Console.WriteLine(a & b); // Output: 1
Console.WriteLine(a | b); // Output: 7
Input and Output (I/O) Operations

C# provides several ways to handle user input and


display output, primarily using the Console class for
console applications and other methods for file
handling, GUI applications, and web-based
input/output.
1. Console Input (Console.ReadLine, Console.Read,
Console.ReadKey)
Reading User Input from the Console
C# allows reading user input via the Console.ReadLine(),
Console.Read(), and Console.ReadKey() methods.
Input and Output (I/O) Operations

a) Console.ReadLine() – Reads a full line


•Returns user input as a string .

•Commonly used for reading text and numbers.

Console.Write("Enter your name: ");


string name = Console.ReadLine();
Console.WriteLine("Hello, " + name + "!");
Input and Output (I/O) Operations

b) Console.Read() – Reads a single character as an integer.


•Returns an ASCII integer value of a single character.
•Pressing Enter is required to submit input.

Example

Console.Write("Press any key: ");


int key = Console.Read();
Console.WriteLine("\nYou pressed: " +
(char)key);
Input and Output (I/O) Operations

C) Console.ReadKey() – Reads a single character without


pressing Enter
•Immediately captures a key press.
•Returns a ConsoleKeyInfo object, which contains the pressed
key.

Example
Console.Write("Press any key to continue...");
ConsoleKeyInfo key = Console.ReadKey();
Console.WriteLine("\nYou pressed: " +
key.KeyChar);
Input and Output (I/O) Operations

2. Console Output (Console.Write and Console.WriteLine)


C# provides Console.Write() and Console.WriteLine() methods to display output.

a) Console.WriteLine() – Prints text followed by a new line


•Moves the cursor to the next line after printing.

b) Console.Write() – Prints text without a new line


•Keeps the cursor on the same line.
C# Keywords
C# Keywords
Data types

 Value types
 Value type variables can be assigned a value directly.

 Reference types
 The reference types do not contain the actual data stored in a
variable, but they contain a reference to the variables.

 Pointer types
 Pointer type variables store the memory address of another
type. Pointers in C# have the same capabilities as the pointers
in C or C++.
Data types

 As its name indicates, a data type represents a


type of the data which you can process using your
computer program. It can be numeric,
alphanumeric, decimal, etc.

For Example :
Name : Zara Ali
Class : 6th
Section :J
Age : 13
Gender :F
Data types

 Student name "Zara Ali" is a sequence of


characters which is also called a string.
 Student class "6th" has been represented by a
mix of whole number and a string of two
characters. Such a mix is called alphanumeric.
 Student section has been represented by single
character which is 'J'.
 Student age has been represented by whole
number which is 13.
 Student gender has been represented by a single
character which is 'F'
Data types

 Similar way when we write our computer program


to process different types of data, we need to
specify its type clearly otherwise computer does
not understand how different operations can be
performed on that given data. Different
programming languages use different keywords to
specify different data types
Data types

Type Keywor Value range


d
Character Char -128 to 127 or 0 to 255
Number Int -32,768 to 32,767 or
-2,147,483,648 to 2,147,483,64
Small Number Short -32,768 to 32,767
Long Number Long -2,147,483,648 to 2,147,483,647
Decimal Float 1.2E-38 to 3.4E+38 till 6 decimal
Number places
End

You might also like