0% found this document useful (0 votes)
3 views75 pages

Unit 1.2

This document is a course outline for a Bachelor in Computer Science and Information Technology program, focusing on C# programming language constructs. It covers topics such as basic language constructs, data types, operators, control statements, and console input/output in C#. The instructor, Tekendra Nath Yogi, provides insights into C# features and its applications across various domains.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views75 pages

Unit 1.2

This document is a course outline for a Bachelor in Computer Science and Information Technology program, focusing on C# programming language constructs. It covers topics such as basic language constructs, data types, operators, control statements, and console input/output in C#. The instructor, Tekendra Nath Yogi, provides insights into C# features and its applications across various domains.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Tribhuvan University

INSTUTE OF SCIENCE AND TECHNOLOGY(IOST)


Bachelor in Computer Science and Information Technology (B.SC CSIT)
VI Semester
NET Centric Computing(CSC-367 )
Unit-1.2: Basic Language Constructs

Instructor
Tekendra Nath Yogi
Tekendranath@gmail.com
Contents: Basic Languages constructs
• An Overview of C#: Introduction, First Simple Program in C#, Basic
structure of C# Program.

• Tokens and data types in C#: Keywords, Data Types, Identifiers: Variables,
Literals

• Operators in C#: Types of Operators, Expressions, Operator precedence and


associativity.

• Control Statements: Conditional Statements: If, else, switch. Loop


Statements: while, for, do while loop and foreach loop, Jump Statements:
Break, continue, return, goto and throw statements.

• Console I/O in C# Program.

11/23/2024 By: Tekendra Nath Yogi 2


Introduction
• C# (pronounced as "C-Sharp“) is a simple, modern, general-purpose,
object-oriented programming language developed by Microsoft within its
.NET initiative led by Anders Hejlsberg. C# was first released in 2002.

11/23/2024 By: Tekendra Nath Yogi 3


Contd…,
• C# is mainly influenced by C, C++ and Java.

Figure: C# Family Tree.


11/23/2024 By: Tekendra Nath Yogi 4
C# Features
• Simple, Modern and General Purpose.

• Objected-Oriented and Component Oriented: Encapsulation, Inheritance,


abstraction and Polymorphism.

• Portable, Platform Independence, architecture neutral and Cross-language


interoperability.

• Type safe, strongly typed, Case Sensitive, secure, Robust and Scalable.

• Automatic Memory Management: Automatic garbage collection.

• Language Integrated Query (LINQ) support

• Asynchronous Programming by using async/await.

• Rich Standard Library.

• Exception Handling.

11/23/2024 By: Tekendra Nath Yogi 5


Introduction
• It is used to develop variety of the applications:
– Window applications

– Web applications

– Web Service applications

– Database applications.

– Mobile applications

– Distributed applications

– AI, ML and IOT applications

– And much, much more!

11/23/2024 By: Tekendra Nath Yogi 6


C# First Program

11/23/2024 By: Tekendra Nath Yogi 7


General Structure of C# Program

11/23/2024 By: Tekendra Nath Yogi 8


General Structure of C# Program

11/23/2024 By: Tekendra Nath Yogi 9


Example Program 1

11/23/2024 By: Tekendra Nath Yogi 10


Example Program 2

11/23/2024 By: Tekendra Nath Yogi 11


Example Program 3

11/23/2024 By: Tekendra Nath Yogi 12


Example Program 4

11/23/2024 By: Tekendra Nath Yogi 13


Comments C#
• Comments:
– Single line Comments: //comment text goes here.

– Multi-line comment: /* comment text goes here and may span in multiple lines */

11/23/2024 By: Tekendra Nath Yogi 14


C# Keywords
• The C# Reserved Keywords

11/23/2024 By: Tekendra Nath Yogi 15


C# Keywords
• The C# Contextual Keywords

11/23/2024 By: Tekendra Nath Yogi 16


C# Data Types – Value Types

11/23/2024 By: Tekendra Nath Yogi 17


C# Data Types – Value Types
• For a value type, a variable holds an actual value, such as 3.1416 or 212. In C#
value types as shown in table below:

11/23/2024 By: Tekendra Nath Yogi 18


C# Data Types – Value Types
• Integers:

• Floating Point Types: float: size-32 bit and range- 1.5E-45 to 3.4E+38.
double: size-64 bit and range - 5E-324 to 1.7E+308

• Decimal Types: size-128 bit and range - 1E-28 to 7.9E+28

• Characters: size-16 bit and range - 0 to 65535.

• The bool Types: size- 1 bit, values: true and false.

11/23/2024 By: Tekendra Nath Yogi 19


C# Data Types – Reference Types
• For a reference type, a variable holds a reference to the value.

• E.g., object, class, string, etc.

• Declaration and assignment of reference type variable:

Class_name ref_var_name = new class_name( );

• E.g., ABC x = new ABC( );

11/23/2024 By: Tekendra Nath Yogi 20


C# Data Types – Dynamic Types
• Pointer: Pointer is a variable that hold the address of the another variable. It
uses & operator and * operator.

• Declaration of Pointer Variables: int *var;

• Initialization of Pointer variable: Let int x , then int *y =&x;

• Accessing value using pointer variable: *y

11/23/2024 By: Tekendra Nath Yogi 21


Identifiers in C# -- Literals
• Fixed value. For example, 110, ‘a’, “xyz”,etc.

11/23/2024 By: Tekendra Nath Yogi 22


Identifiers in C# -- Literals
Literal Example
Integer 100
Character ‘a’
Floating point 10.21f
Long int 12L
Unsigned int 100u
Unsigned long int 110ul
Double 10d
Decimal 10.5m
Hexadecimal 0x123a
String “xyz”
Verbatim string @”x
Y
z”

11/23/2024 By: Tekendra Nath Yogi 23


Identifiers in C# -- Literals
• Escape Sequences:

11/23/2024 By: Tekendra Nath Yogi 24


Identifiers in C# -- Variables
• Declaration: type var_name; . E.g., int x;

• Initialization: var_name = value; . E.g. x=10;

• Combining declaration and initialization: type var_name = value;. E.g., int


x=10;

• Declaring multiple variables of same type: int x, y, z;

• Declaring and initializing multiple variables of same type: int x=10, y=20,
z=30;

• Declaring and initializing multiple variables of same type with same value:
int x=y= z=100;

• Static initialization: z=100;

• Dynamic initialization: z= x + y;

11/23/2024 By: Tekendra Nath Yogi 25


Identifiers in C# -- Variables
• Implicitly typed variable: declared using keyword var and it must be
initialized by some value.

– Syntax: var var_name = value;

– E.g. var x=10;

• Note:
– Once that type has been determined, the variable has a type, and this type is fixed
throughout the lifetime of the variable.

– Only one implicitly typed variable can be declared at any one time.

– var s1 = 4.0, s2 = 5.0; // Error!

11/23/2024 By: Tekendra Nath Yogi 26


Type Conversion
• In a expression having mixed type of operands then need to convert the type
before applying the operation on the operands.

• Two types of Conversion:


– Implicit conversion: automatic type conversion, requires type compatibility, widening
conversion so no loss of information. E.g., float f; int i; f=i;

– Explicit Conversion: forced type conversion. Also known as type casting. Narrowing
conversion so, loss of information may occurs.

– Syntax: (target type)expression

– Example: double x, y; (int)(x/y);

11/23/2024 By: Tekendra Nath Yogi 27


Type Conversion

11/23/2024 By: Tekendra Nath Yogi 28


C# Operators
• Operator is a symbol that enables construction and evaluation of expressions in the
program.
• Operators in C#:
– Arithmetic Operator: +, -, *, /, %, ++, --
– Assignment operator: = and shorthand form of assignment operator.
– Relational Operator: <, >, <=, >=, ==, !=
– Logical Operator: &, |, ^, ||, &&, !
– Bitwise Operator: &, |, ^, ~, <<, >>
– Ternary Operator: Exp1? Exp2: Exp3;
– Array indexing Operator: [index]
– Member access Operator: ., ->
– Null Coalescing Operators: ??
– Type Testing and Casting Operators: is, as
– lambda operator: =>
– Await Operator: await
– Miscellaneous Operators: new, &, checked, unchecked, (cast), sizeof.

11/23/2024 By: Tekendra Nath Yogi 29


C# Operators: Arithmetic Operator

Operator Meaning Use (let A=10 , B= Result


3)
+ Addition A+B 13
- Subtraction A-B 7
* Multiplication A*B 30
/ Division A/B 3
% Modulus A%B 1
++ Increment A = ++B A= 4 and B=4
A=B++ A= 3 and B=4
-- Decrement A = --B A= 2 and B=2
A=B-- A= 3 and B=2

11/23/2024 By: Tekendra Nath Yogi 30


C# Operators: Relational Operator
Operator Meaning Use (let A=10 , B= Result
3)
< Less than A<B false
> Greater than A>B true
<= Less than or equal to A<=B false
>= Greater than or equal to A>=B true
== Equal to A==B false
!= Not equal to A != B true

11/23/2024 By: Tekendra Nath Yogi 31


Bitwise Operator: Logical Operator

Operator Meaning Use (let A=true , Result


B=false)
& AND A&B false
| OR A|B true
^ XOR A^B true
|| SHORT CIRCUIT OR A||B true
&& SHORT CIRCUIT AND A&&B false
! NOT !B true

11/23/2024 By: Tekendra Nath Yogi 32


Bitwise Operator: Bitwise Operator
• &, |, ^, ~, <<, >>

Operator Meaning Use (let A=1 1 0 1 0 Result


0 1 1 , B= 1 0 1 0 1
0 1 0)
& Bitwise-AND A&B 10000010
| Bitwise-OR A|B 11111011
^ Bitwise-XOR A^B 011111001
~ Bitwise-NOT ~B 01010101
<< Shift left A<<2 0 1 0 0 1 100
>> Shift right B>>2 001 0 1 0 1 0

11/23/2024 By: Tekendra Nath Yogi 33


Assignment operator:
• Var_name = expression;

• C# supports chain of assignment: x=y=z=10;

• Also includes compound assignment or shorthand assignment:

– var op=Expression.

– E.g. A+=B;

11/23/2024 By: Tekendra Nath Yogi 34


Ternary Operator (? :)
• Also known as conditional operator.

• Syntax: condition? Expression1: Expression 2.

• For example: if (a<b)? return true: return false.

11/23/2024 By: Tekendra Nath Yogi 35


Operator Precedence in C#

11/23/2024 By: Tekendra Nath Yogi 36


Operator Associativity in C#
• Operators having Right to left associativity:

– Ternary operator( ? :),

– unary operators (++, --, -, +, ~, !, sizeof, (type), * and &),

– equality relational operators(==, !=),

– assignment and shorthand assignment operator ( =, +=, …)

• Other, operators have left to right associativity.

11/23/2024 By: Tekendra Nath Yogi 37


Control Statements in C#
Statement that is used to change the normal sequential execution flow of a program.

if
Selection
Statements switch
while
do-while

Control Statements Iteration for


in C# Statements
foreach

break
continue
Jump
goto
Statements
return
throw
11/23/2024 By: Tekendra Nath Yogi 38
Control Statements in C# -- selection Statement
• The if statement:
if(condition)

statement sequence

else

statement sequence

11/23/2024 By: Tekendra Nath Yogi 39


Contd…,

11/23/2024 By: Tekendra Nath Yogi 40


Control Statements in C# -- selection Statement
• The Nested if statement:
if(condition)
{
if(condition)
{
statement sequence
}
else
{
statement sequence
}
}
else
{
statement sequence
}

11/23/2024 By: Tekendra Nath Yogi 41


Contd…,

11/23/2024 By: Tekendra Nath Yogi 42


Control Statements in C# -- selection Statement
• The if-else-if Ladder:
if(condition){
statement;
}
else if(condition){
statement;
}
else if(condition){
statement;
}
...
else{
statement;
}

11/23/2024 By: Tekendra Nath Yogi 43


Contd…,

11/23/2024 By: Tekendra Nath Yogi 44


Control Statements in C# -- selection Statement
• The Switch Statement:
switch(expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}
11/23/2024 By: Tekendra Nath Yogi 45
Contd…,

11/23/2024 By: Tekendra Nath Yogi 46


Control Statements in C# -- Iteration Statement
• The For Loop:
for(initialization; condition; iteration)

statement sequence

11/23/2024 By: Tekendra Nath Yogi 47


Contd…,

11/23/2024 By: Tekendra Nath Yogi 48


Control Statements in C# -- Iteration Statement
• The while Loop:
While(condition)

Statements

11/23/2024 By: Tekendra Nath Yogi 49


Contd…,

11/23/2024 By: Tekendra Nath Yogi 50


Control Statements in C# -- Iteration Statement
• The do-while Loop:
do

statements;

} while(condition);

11/23/2024 By: Tekendra Nath Yogi 51


Contd…,

11/23/2024 By: Tekendra Nath Yogi 52


Control Statements in C# -- Iteration Statement
• The foreach Loop:

– The foreach loop cycles through the elements of a collection such as array.

– The general form of foreach is:

foreach(type loopvar in collection) statement;

For example, foreach ( int x in Array_name) {statements;}

11/23/2024 By: Tekendra Nath Yogi 53


Contd…,

11/23/2024 By: Tekendra Nath Yogi 54


Control Statements in C# -- Jump Statement
• The break Statement: to exit a loop
Loop statement

{
If(condition)

break;

Statement after loop statement

11/23/2024 By: Tekendra Nath Yogi 55


Contd…,

11/23/2024 By: Tekendra Nath Yogi 56


Control Statements in C# -- Jump Statement
• The continue Statement: forces the next iteration of the loop to take place,
skipping any code in between.
Loop statement; can be for, while, do-while or foreach loop.

{
If(condition)

continue;

Statement after loop statement

11/23/2024 By: Tekendra Nath Yogi 57


Contd…,

11/23/2024 By: Tekendra Nath Yogi 58


Control Statements in C# -- Jump Statement
• The return Statement: causes a method to return. It can also be used to return
a value.
return_type method_name (args )

statements;

return x;

11/23/2024 By: Tekendra Nath Yogi 59


Contd…,

11/23/2024 By: Tekendra Nath Yogi 60


Control Statements in C# -- Jump Statement
• The goto statement:
Statement;

Label_name:

Statements;

if(condtion)

goto label_name;

11/23/2024 By: Tekendra Nath Yogi 61


Contd…,

11/23/2024 By: Tekendra Nath Yogi 62


Console I/O in C# Program
• Console.WriteLine(“ “);

• Var x = Console.ReadLine ( );

• y = int.parse(x);

• Now, y can be processed in a program as int data. Where as while reading it is


a string data.

11/23/2024 By: Tekendra Nath Yogi 63


Contd…,

11/23/2024 By: Tekendra Nath Yogi 64


Homework
• Write a program to add two numbers and display the sum.

• Write a program for calculating the simple interest using the formula SI=

(PTR)/100, where, P denotes the principal, T time and R rate of interest.

• Write a program to decide whether a number is positive or negative.

• Write a program to test a number for even or odd.

• Write a program to calculate area (=PI*r*r) and circumference (2*PI*r) of a

circle having radius r. The radius r should be taken from user.

11/23/2024 By: Tekendra Nath Yogi 65


Contd…,
• Ram’s basic salary is input through the keyboard. His dearness allowance is
40% of basic salary, and house rent allowance is 20% of basic salary. Write
a program to calculate his gross salary.

• The distance between two cities (in km.) is input through the keyboard.
Write a program to convert and print this distance in meters, feet, inches
and centimeters. [Note: 1km=1000 meter, 1km= 3280.84 feet, 1 km=
39370.08 inches, and 1 km= 100000 centimeter].

• Write a program which reads radius of sphere from keyboard and calculates
its volume and area [ hint: volume=4PIr3/3 and Area= 4PIr2].

11/23/2024 By: Tekendra Nath Yogi 66


Contd…,
• Write a program to read three sides of a triangle and calculate the area (
Note: area= sqrt(s*(s-a)*(s-b)*(s-c)) where, s=(a+b+c)/2).

• If the marks obtained by a student in five different subjects are input


through the keyboard, find out the aggregate marks and percentage marks
obtained by the student. Assume that the maximum marks that can be
obtained by a student in each subject is 100.

• A cloth shop during festival seasons offers a discount of 10% on all


purchases made in that shop. The total amount after discount for a
customer is given as Rs.1000.5. Write a program to calculate and display
the discount, and amount before discount.
11/23/2024 By: Tekendra Nath Yogi 67
Contd…,
• Write a program to find the size of each primitive data types in your
system.

• Write a program that requests two float type numbers from the user and
then divides the first number by the second and display the result along
with the numbers.

• Write a program to read the price of an item in decimal form (like 15.95
Rs) and print the output in Paisa (like 1595 Paisa).

• If the total selling price of 15 items and the total profit earned on them is
input through the keyboard, write a program to find the cost price of one
item.

• Write a program to decide whether a input number is less than 100 or not.

• Write a program that prints the even numbers from 1 to 100.


11/23/2024 By: Tekendra Nath Yogi 68
Contd…,
• Write a program to read number of days from the user and convert them
into months and days.
• Write a program to read two integer number ( number of male and female )
and finds the ratio between them to express as floating point value.
• Write a program to read two integer value m and n and decide and print
whether m is a multiple of n.
• Write a program to find the sum of the series following series and display
the result. 1+1/2+1/3 + …….1/n. Take the value of n from the user.
• Write a program that evaluate the expression: a=5<=8&&6!=5 and display
the result on the screen.
• Write a program that evaluate the expression: x=a-b/3+c*2-1 and display
the result on the screen. Note take the value of variables from the user.

11/23/2024 By: Tekendra Nath Yogi 69


Contd…,
• Write a program to determine whether a given number is ‘odd’ or ‘even’ and print
the message number is even or number is odd.

• Write a program that reads four values a, b, c and d from the terminal and evaluates
the ratio of (a+b) and (c-d) and prints the result, if (c-d) is not equal to zero,
otherwise print the appropriate message on the screen.

• If the ages of Ram, Shyam and Ajay are input through the keyboard, write a
program to determine the youngest of the three.

• While purchasing certain items, a discount of 10% is offered if the quantity


purchased is more than 1000. If quantity and price per item are input through the
keyboard, write a program to calculate the total expenses.

• Write a Program to read two numbers and an arithmetic operator from the user,
program performs the operation on input data based on input arithmetic operator
and display the result (note: use else..if ladder).
11/23/2024 By: Tekendra Nath Yogi 70
Contd…,
• If cost price and selling price of an item is input through the keyboard, write a
program to determine whether the seller has made profit or incurred loss. Also
determine how much profit he made or loss he incurred.

• The current year and the year in which the employee joined the organization are
entered through the keyboard. If the number of years for which the employee has
served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the
employee. If the years of service are not greater than 3, then the program should do
nothing.

• Write a program to finding the grading of students based on the marks entered by
the user. ( Note: use switch statement).

• Write a Program to read two numbers and an arithmetic operator which performs
the specified operation and display the result (note: use Switch statement).

11/23/2024 By: Tekendra Nath Yogi 71


Contd…,
• Write a c program that reads a value in the range 1 to 12 and print the name of that
month. Print error for any other input value.
• Write a program to checks whether an year (integer) entered by the user is a leap
year or not (Note: A leap year is exactly divisible by 4 except for century years
(years ending with 00). The century year is a leap year only if it is perfectly
divisible by 400.).
• The marks obtained by a student in 5 different subjects are input through the
keyboard. The student gets a division as per the following rules:
– percentage above or equal to 80 – Distinction division
– Percentage between 60 and 79 - First division
– Percentage between 50 and 59 - Second division
– Percentage between 40 and 49 - Third division
– Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.
• Write a program to find the sum of first n natural numbers.

• Write a program to find the sum and average of first n natural numbers.

11/23/2024 By: Tekendra Nath Yogi 72


Contd…,
• Write a program to find the sum of square of first n natural numbers.
• Write a program to display the Fibonacci sequence of first n numbers
(entered by the user) using loop.
• Write a program to generate a multiplication table of a number entered by
the user.
• Write a program that reads weight and height of boys in a class and then
count the number of boys whose weight is less than 50 kg and height is
greater than 170 cm.
• Write a program to find the number of and sum of all integers greater than
100 and less than 200 that are divisible by 7.
• Write a program that read the code 1 for a boy and 2 for a girl and then
count the number of boys and girls in a class based on the entered code.

11/23/2024 By: Tekendra Nath Yogi 73


Contd…,
• Write a program to take a number from the user and then count the number
of digits in that number.

• Write a program to find the sum of digits of a number entered by the user.

• Write a program to reverse the number entered by the user.

• Write a program to Check Whether a Number entered by the user is


Palindrome or Not

• Write a program to check whether a number entered by the user is prime or


not.

11/23/2024 By: Tekendra Nath Yogi 74


Thank You !

11/23/2024 By: Tekendra Nath Yogi 75

You might also like