0% found this document useful (0 votes)
4 views37 pages

02-Introduction To C Sharp Programming

The document is an introduction to C# programming, outlining the qualifications of the instructor and the structure of the course. It covers basic concepts such as console applications, simple programs, memory concepts, arithmetic operations, and decision-making using equality and relational operators. The content includes examples of code and explanations of programming fundamentals.

Uploaded by

kbahadorzai
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)
4 views37 pages

02-Introduction To C Sharp Programming

The document is an introduction to C# programming, outlining the qualifications of the instructor and the structure of the course. It covers basic concepts such as console applications, simple programs, memory concepts, arithmetic operations, and decision-making using equality and relational operators. The content includes examples of code and explanations of programming fundamentals.

Uploaded by

kbahadorzai
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/ 37

Subject: C Sharp Programming

Instructor : Ajmal Rahmati

Qualifications

 BCS
 MCS
 Sql Server Certify
 MCITP
 CCNA

 University Lecturer
 Software Developer

[email protected] Ajmal Rahmati 0779649697


2

Chapter 2 – Introduction to C#
Programming
Outline
1 Introduction
2 Simple Program: Printing a Line of Text
3 Another Simple Program: Adding Integers
4 Memory Concepts
5 Arithmetic
6 Decision Making: Equality and Relational
Operators

.
3

Introduction
Console applications
No visual components
Only text output
Two types
MS-DOS prompt
Used in Windows 95/98/ME
Command prompt
Used in windows 2000/NT/XP
Windows applications
Forms with several output types
Contain Graphical User Interfaces (GUIs)

.
4

Simple Program: Printing a line of text


Comments
Comments can be created using //…

Multi-lines comments use /* … */

Comments are ignored by the compiler

Used only for human readers

Namespaces
Groups related C# features into a categories

Allows the easy reuse of code

Many namespaces are found in the .NET framework library

Must be referenced in order to be used

White Space
Includes spaces, newline characters and tabs
.
5

Simple Program: Printing a line of text


Keywords
Words that cannot be used as variable or class names or any other capacity
Have a specific unchangeable function within the language
Example: class
All keywords are lowercase
Classes
Class names can only be one word long (i.e. no white space in class name )
Class names are capitalized, with each additional English word capitalized as well
(e.g., MyFirstProgram )
Each class name is an identifier
Can contain letters, digits, and underscores (_)
Cannot start with digits
Can start with the at symbol (@)

.
6

Simple Program: Printing a line of text


Class bodies start with a left brace ({)
Class bodies end with a right brace (})
Methods
Building blocks of programs
The Main method
Each console or windows application must have exactly one
All programs start by executing the Main method
Braces are used to start ({) and end (}) a method
Statements
Anything in quotes (“) is considered a string
Every statement must end in a semicolon (;)

.
7

Simple Program: Printing a line of text


Graphical User Interface
GUIs are used to make it easier to get data from the user as well as
display data to the user
Message boxes
Within the System.Windows.Forms namespace
Used to prompt or display information to the user

.
8
1 // Fig. 3.1: Welcome1.cs These are two single line comments. They are ignored by the compiler and are only used to aid
other programmers. They use the double slash (//)
2 // A first program in C#.
This is the using directive. It lets the compiler know that it should include the System Welcome1.cs
3
namespace.
4 using System;
5 This is a blank line. It means nothing to the compiler and is only used to add clarity to the program.

6 class Welcome1 This is the beginning of the Welcome1 class definition. It starts with the class keyword
and then the name of the class.
7 {
8 static void Main( string[] args ) This is the start of the Main method. In this case it instructs the program to do
everything
9 {
This is a string of characters that Console.WriteLine instructs the
10 Console.WriteLine( "Welcome to C# Programming!" ); compiler to output
11 }
12 }

Program
Welcome to C# Programming! Output

.
9
Simple Program: Printing a Line of Text

Visual Studio .NET-generated console application.

.
10

Simple Program: Printing a Line of Text

Execution of the Welcome1 program.

.
11
1 // Fig. 3.4: Welcome2.cs
2 // Printing a line with multiple statements. Welcome2.cs
3
Console.WriteLine will pick up where the line ends. This will
4 using System; cause the output to be on one line even though it is on two in
5 the code.
6 class Welcome2
7 {
8 static void Main( string[] args )
9 {
10 Console.Write( "Welcome to " );
11 Console.WriteLine( "C# Programming!" );
12 }
13 }

Program
Welcome to C# Programming! Output

.
12
1 // Fig. 3.5: Welcome3.cs
2 // Printing multiple lines with a single statement.
Welcome3.cs
3 The \n escape sequence is used to put output on the
4 using System; next line. This causes the output to be on several lines
even though it is only on one in the code.
5
6 class Welcome3
7 {
8 static void Main( string[] args )
9 {
10 Console.WriteLine( "Welcome\nto\nC#\nProgramming!" );
11 }
12 }

Welcome Program
To Output
C#
Programming!
.
Simple Program: Printing a Line of Text 13

Esc ape sequenc e Desc ription


\n Newline. Position the screen cursor to the beginning of the
next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning
of the current line; do not advance to the next line. Any
characters output after the carriage return overwrite the
previous characters output on that line.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote (") character.
FSome
ig. 3.6Some
common csequences.
escape ommon esc ape sequenc es.

.
14
1 // Fig. 3.7: Welcome4.cs
Welcome4.cs
2 // Printing multiple lines in a dialog Box.
3
The System.Windows.Forms namespace allows the programmer to
4 using System; use the MessageBox class.
5 using System.Windows.Forms;
6
This will display the contents in a message box as opposed to in the
7 class Welcome4
console window.
8 {
9 static void Main( string[] args )
10 {
11 MessageBox.Show( "Welcome\nto\nC#\nprogramming!" );
12 }
13 }

Program
Output

.
15
Simple Program: Printing a Line of Text

Add
Reference
dialogue
Adding a reference to an assembly in Visual Studio .NET (part 1).

.
Simple Program: Printing a Line of Text
16

Solution Explorer

System.Windows.Forms
reference

References folder

Adding a reference to an assembly in Visual Studio .NET (part 2).

.
Simple Program: Printing a Line of Text 17

Label Button Text field Menu Menu bar

Internet Explorer’s GUI.

.
Simple Program: Printing a Line of Text
18

Close box
OK button allows the
user to dismiss the
dialog.

Dialog is automatically sized to


accommodate its contents.

Mouse cursor

Dialog displayed by calling MessageBox.Show

.
19

Another Simple Program: Adding Integers

Primitive data types


Data types that are built into C#
String, Int, Double, Char, Long

15 primitive data types (chapter 4)

Each data type name is a C# keyword

Same type variables can be declared on separate lines or on one line

Console.ReadLine()
Used to get a value from the user input

Int32.Parse()
Used to convert a string argument to an integer

Allows math to be preformed once the string is converted


.
1 // Fig. 3.11: Addition.cs This is the start of class Addition 20
2 // An addition program.
3 Two string variables defined over two lines Addition.cs
4 using System;
5 The comment after the declaration is used to briefly state
6 class Addition the variable purpose
7 {
8 static void Main( string[] args ) These are three ints that are declared over
9 { several lines and only use one semicolon. Each is
10 string firstNumber, // first string entered by user
separated by a coma.
11 secondNumber; // second string entered by user
12
13 int number1, // first number to add
14 number2, // second number to add This line is considered a prompt because it asks the user to
15 sum; // sum of number1 and number2 input data.
16
17 // prompt for and read first number from user as string
18 Console.Write( "Please enter the first integer: " );
19 firstNumber = Console.ReadLine(); Console.ReadLine is used to take the users input and place it
20 into a variable.
21 // read second number from user as string
22 Console.Write( "\nPlease enter the second integer: " ); Int32.Parse is used to convert the given string into an integer.
23 secondNumber = Console.ReadLine(); It is then stored in a variable.
24
25 // convert numbers from type string to type int
26 number1 = Int32.Parse( firstNumber ); The two numbers are added and stored in the variable sum.
27 number2 = Int32.Parse( secondNumber );
28
29 // add numbers
30 sum = number1 + number2;
31

.
21
32 // display results
33 Console.WriteLine( "\nThe sum is {0}.", sum );
Addition.cs
34
35 } // end method Main
36
37 } // end class Addition

Putting a variable out through Console.WriteLine is done by placing the variable after
the text while using a marked place to show where the variable should be placed.

Please enter the first integer: 45

Please enter the second integer: 72


Program
Output
The sum is 117.

.
22

Memory Concepts
Memory locations
Each variable is a memory location
Contains name, type, size and value
When a new value is enter the old value is lost
Used variables maintain their data after use

.
23

Memory Concepts
45

number1

Memory location showing name and value of variable number1.

.
24

Arithmetic
Arithmetic operations
Not all operations use the same symbol
Asterisk (*) is multiplication
Slash (/) is division
Percent sign (%) is the modulus operator
Plus (+) and minus (-) are the same
Must be written in a straight line
There are no exponents
Division
Division can vary depending on the variables used
When dividing two integers the result is always rounded down to an
integer
To be more exact use a variable that supports decimals

.
25

Arithmetic
Order
Parenthesis are done first
Division, multiplication and modulus are done second
Left to right
Addition and subtraction are done last
Left to right

.
26

Memory Concepts
number1 45

number2 72

Fig. 3.13 Memory locations after values for variables number1 and number2 have been input.

.
27

Memory Concepts
number1 45

number2 72

sum 117

Fig. 3.14 Memory locations after a calculation.

.
28

Arithmetic
C #operation Arithmetic operator Algebraic expression C #expression
Addition + f+7 f + 7

Subtraction – p–c p - c

Multiplication * bm xy b * m

Division / x / y or x / y

Modulus % r mod s r % s
Fig. 3.15 Arithmetic
Arithmetic operators operators.

.
29

Arithmetic
O pera tor(s) O pera tion O rder of eva lua tion (prec edenc e)
( ) Parentheses Evaluated first. If the parentheses are nested,
the expression in the innermost pair is
evaluated first. If there are several pairs of
parentheses “on the same level” (i.e., not
nested), they are evaluated left to right.
*, / or % Multiplication Evaluated second. If there are several such
Division operators, they are evaluated left to right.
Modulus
+ or - Addition Evaluated last. If there are several such
Subtraction operators, they are evaluated left to right.
FPrecedence
ig. 3.16of arithmetic
Prec edenc e of a rithmetic opera tors.
operators.

.
30

Arithmetic
Step 1.
y = 2 * 5 * 5 + 3 * 5 + 7;
2 * 5 is 10 (Leftmost multiplication)

Step 2.
y = 10 * 5 + 3 * 5 + 7;
10 * 5 is 50 (Leftmost multiplication)

Step 3.
y = 50 + 3 * 5 + 7;
3 * 5 is 15 (Multiplication before addition)

Step 4.
y = 50 + 15 + 7;
50 + 15 is 65 (Leftmost addition)

Step 5.
y = 65 + 7;
65 + 7 is 72 (Last addition)

Step 6. y = 72; (Last operation—place 72 into y)

Order in which a second-degree polynomial is evaluated.


.
31
Decision Making: Equality and
Relational Operators

The if structure
Used to make a decision based on the truth of the condition
True: a statement is performed
False: the statement is skipped over
The start of an if statement should not end in a semicolon (;)
Fig. 3.18 lists the equality and rational operators
There should be no spaces separating the operators

.
32
Decision Making: Equality and
Relational Operators
Standard algebraic C # equality Example Meaning of
equality operator or or relational of C # C # c ondition
relational operator operator c ondition
Equality operators
 == x == y x is equal to y
 != x != y x is not equal to y
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
 >= x >= y x is greater than or equal to y
 <= x <= y x is less than or equal to y
FEquality
ig. 3.18 Equality
and relational and relational operators.
operators.

.
32
1 // Fig. 3.19: Comparison.cs
2 // Using if statements, relational operators and equality
3 // operators.
4 Comparison.cs
5 using System;
6
7 class Comparison
8 {
9 static void Main( string[] args )
10 {
11 int number1, // first number to compare
12 number2; // second number to compare
13
14 // read in first number from user
15 Console.Write( "Please enter first integer: " );
16 number1 = Int32.Parse( Console.ReadLine() ); Combining these two methods eliminates the need for a
17 temporary string variable.
18 // read in second number from user
19 Console.Write( "\nPlease enter second integer: " ); If number1 is the same as number2 this line is
20 number2 = Int32.Parse( Console.ReadLine() ); preformed
21
22 if ( number1 == number2 )
If number1 does not equal number2 this line of code is executed.
23 Console.WriteLine( number1 + " == " + number2 );
24
25 if ( number1 != number2 )
26 Console.WriteLine( number1 + " != " + number2 ); If number1 is less than number2 the program will use this line
27
28 if ( number1 < number2 )
29 Console.WriteLine( number1 + " < " + number2 ); If number1 is greater than number2 this line will be preformed
30
31 if ( number1 > number2 )
32 Console.WriteLine( number1 + " > " + number2 );
33 .
34 if ( number1 <= number2 ) 33

35 Console.WriteLine( number1 + " <= " + number2 );


36
If number1 is less than or equal to number2 then this code will be used Comparison.cs
37 if ( number1 >= number2 )
38 Console.WriteLine( number1 + " >= " + number2 );
39
Lastly if number1 is greater than or equal to number2 then this code will
40 } // end method Main
be executed
41
42 } // end class Comparison

Please enter first integer: 2000 Program


Please enter second integer: 1000 Output
2000 != 1000
2000 > 1000
2000 >= 1000

Please enter first integer: 1000

Please enter second integer: 2000


1000 != 2000
1000 < 2000
1000 <= 2000

Please enter first integer: 1000

Please enter second integer: 1000


1000 == 1000
1000 <= 1000
1000 >= 1000 .
35
Decision Making: Equality and
Relational Operators
O pera tors Assoc ia tivity Type
() left to right parentheses
* / % left to right multiplicative
+ - left to right additive
< <= > >= left to right relational
== != left to right equality
= right to left assignment
Fig. 3.20and associativity
Precedence Prec edenc e and
of operators a ssoc
discussed ia chapter.
in this tivity of opera tors disc ussed in this
c hapter.

.
Questions

You can ask your question

Thought of Day
‫ پیروزی بدون افتخار است‬، ‫بردن بدون ریسک‬

.
Thank you
.

You might also like