SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Introduction to Visual Programming
Lecture #2:

C# Program Structure
identifiers, variables, inc & dec
C# Program Structure
Program specifications (optional)
//==========================================================
//
// File: HelloWorld.cs CS112 Assignment 00
//
// Author: Muhammad Adeel Abid
Email: m_adeel_dcs@yahoo.com
//
// Classes: HelloWorld
// -------------------// This program prints a string called "Hello, World!”
//
//==========================================================

Library imports
using System;

Class and namespace definitions
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine(“Hello, World!”);
}
}
2
Comments
Comments

Comments are ignored by the compiler: used only for
human readers (i.e., inline documentation)
Two types of comments

• Single-line comments use

//…
// this comment runs to the end of the line

• Multi-lines comments use /*

… */

/* this comment runs to the terminating
symbol, even across line breaks
*/

3
Identifiers
Identifiers are the words that a programmer uses in a program
An identifier can be made up of letters, digits, and the underscore
character
They cannot begin with a digit
C# is case sensitive, therefore args and Args are different
identifiers
Sometimes we choose identifiers
ourselves when writing a program
(such as HelloWorld)
using System;
class HelloWorld
Sometimes we are using another {
static void Main(string[] args)
programmer's code, so we use
{
Console.WriteLine(“Hello World!”);
the identifiers that they chose
}
(such as WriteLine)
}
4
Identifiers: Keywords
Often we use special identifiers called keywords that
already have a predefined meaning in the language
Example: class

A keyword cannot be used in any other way
C# Keywords

abstract
byte
class
delegate
event
fixed
goto
interface
namespace
out
public
sealed
static
throw
ulong
value

as
case
const
do
explicit
float
if
internal
new
override
readonly
set
string
true
unchecked
virtual

base
catch
continue
double
extern
for
implicit
is
null
params
ref
short
struct
try
unsafe
void

bool
char
decimal
else
false
foreach
in
lock
object
private
return
sizeof
switch
typeof
ushort
volatile

break
checked
default
enum
finally
get
int
long
operator
protected
sbyte
stackalloc
this
uint
using
while

All C# keywords are lowercase!
5
C# Program Structure: Class
//

comments about the class

class HelloWorld
{

class header
class body

Comments can be added almost anywhere
}

6
C# Classes
Each class name is an identifier
• Can contain letters, digits, and underscores (_)
• Cannot start with digits
• Can start with the at symbol (@)

Convention: Class names are capitalized, with
each additional English word capitalized as well
(e.g., MyFirstProgram )

Class bodies start with a left brace ({)
Class bodies end with a right brace (})
7
C# Program Structure: Method
//

comments about the class

class HelloWorld
{
//

comments about the method

static void Main (string[] args)
{

Console.Write(“Hello World!”);
Console.WriteLine(“This is from CS112!”);

}
}

8
Console Application vs. Window Application
Console Application
No visual component
Only text input and output
Run under Command Prompt or DOS Prompt

Window Application
Forms with many different input and output types
Contains Graphical User Interfaces (GUI)
GUIs make the input and output more user friendly!
Message boxes
• Within the System.Windows.Forms namespace
• Used to prompt or display information to the user
9
Variables
A variable is a typed name for a location in memory
A variable must be declared, specifying the variable's
name and the type of information that will be held in it
data type

variable name numberOfStudents:

int numberOfStudents;
…
int total;
…
int average, max;

9200
total: 9204
9208
9212
9216
average: 9220
max: 9224
9228
9232

Which ones are valid variable names?
myBigVar
99bottles

VAR1
_test
@test
namespace
It’s-all-over
10
Assignment
An assignment statement changes the value of a variable
The assignment operator is the = sign
int total;
…
total = 55;
The value on the right is stored in the variable on the left
The value that was in total is overwritten

You can only assign a value to a variable that is consistent with the
variable's declared type (more later)
You can declare and assign initial value to a variable at the same
time, e.g.,
int total = 55;

11
Example
static void Main(string[] args)
{
int total;
total = 15;
System.Console.Write(“total = “);
System.Console.WriteLine(total);
total = 55 + 5;
System.Console.Write(“total = “);
System.Console.WriteLine(total);
}
12
Constants
A constant is similar to a variable except that it holds
one value for its entire existence
The compiler will issue an error if you try to change a
constant
In C#, we use the constant modifier to declare a
constant
constant int numberOfStudents = 42;

Why constants?
give names to otherwise unclear literal values
facilitate changes to the code
prevent inadvertent errors

13
C# Data Types
There are 15 data types in C#
Eight of them represent integers:
byte, sbyte, short, ushort, int, uint, long,ulong

Two of them represent floating point numbers
float, double

One of them represents decimals:
decimal

One of them represents boolean values:
bool

One of them represents characters:
char

One of them represents strings:
string

One of them represents objects:
object

14
Numeric Data Types
The difference between the various numeric types is their size,
and therefore the values they can store:
Type

Storage

Range

byte
sbyte
short
ushort
int
uint
long
ulong

8 bits
8 bits
16 bits
16 bits
32 bits
32 bits
64 bits
64 bits

0 - 255
-128 - 127
-32,768 - 32767
0 - 65537
-2,147,483,648 – 2,147,483,647
0 – 4,294,967,295
-9×1018 to 9×1018
0 – 1.8×1019

decimal

128 bits

±1.0×10-28; ±7.9×1028 with 28-29 significant digits

float
double

32 bits
64 bits

±1.5×10-45; ±3.4×1038 with 7 significant digits
±5.0×10-324; ±1.7×10308 with 15-16 significant digits

Question: you need a variable to represent world population. Which type do you use?

15
Examples of Numeric Variables
int x = 1;
short y = 10;
float pi = 3.14f; // f denotes float
float f3 = 7E-02f; // 0.07
double d1 = 7E-100;
// use m to denote a decimal
decimal microsoftStockPrice = 28.38m;

Example: TestNumeric.cs

16
Boolean
A bool value represents a true or false
condition
A boolean can also be used to represent any
two states, such as a light bulb being on or off
The reserved words true and false are
the only valid values for a boolean type
bool doAgain = true;

17
Characters
A char is a single character from the a character set
A character set is an ordered list of characters; each character is
given a unique number
C# uses the Unicode character set, a superset of ASCII

Uses sixteen bits per character, allowing for 65,536 unique characters
It is an international character set, containing symbols and characters
from many languages
Code chart can be found at:
http://www.unicode.org/charts/

Character literals are represented in a program by delimiting with
single quotes, e.g.,
'a‘ 'X‘ '7' '$‘ ',‘
char response = ‘Y’;

18
Common Escape Sequences
Escape sequence Description
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.
’
Used to print a single quote

Backslash. Used to print a backslash character.
"
Double quote. Used to print a double quote (") character.

19
string
A string represents a sequence of
characters, e.g.,
string message = “Hello World”;

20
Data Input
Console.ReadLine()

Used to get a value from the user input
Example
string myString = Console.ReadLine();

Convert from string to the correct data type
Int32.Parse()

• Used to convert a string argument to an integer
• Allows math to be preformed once the string is converted
• Example:
string myString = “1023”;
int myInt = Int32.Parse( myString );

Double.Parse()
Single.Parse()

//for double type variable
//for float type variable

21
Sample Input
//for String
String s;
Console.Write("Enter a string =");
s = Console.ReadLine();
Console.WriteLine("You enter =" + s);
//-----------------------------------//for int
int a;
Console.Write("Enter an integer =");
a = Int32.Parse(Console.ReadLine());
Console.WriteLine("You enter =" + a);
//-----------------------------------//for float
float flt;
Console.Write("Enter float value =");
flt = Single.Parse(Console.ReadLine());
Console.WriteLine("You enter =" + flt);
//-----------------------------------//for double
double dbl;
Console.Write("Enter Double type value =");
dbl = Double.Parse(Console.ReadLine());
Console.WriteLine("You enter =" + dbl);
22
Increment and Decrement
++ is used to denote Increment
Prefix increment(++a)
Postfix increment(a++)

-- is used to denote Decrement
Prefix decrement(--a)
Postfix decrement(a--)

23

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
C++ language
C++ languageC++ language
C++ language
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Oops
OopsOops
Oops
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
C# in depth
C# in depthC# in depth
C# in depth
 
Visual c sharp
Visual c sharpVisual c sharp
Visual c sharp
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
2 expressions (ppt-2) in C++
2 expressions (ppt-2) in C++2 expressions (ppt-2) in C++
2 expressions (ppt-2) in C++
 
LEARN C#
LEARN C#LEARN C#
LEARN C#
 
C++
C++C++
C++
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 

Andere mochten auch

Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Abou Bakr Ashraf
 
Dependency parsing (2013)
Dependency parsing (2013)Dependency parsing (2013)
Dependency parsing (2013)Craig Trim
 
Deep Parsing (2012)
Deep Parsing (2012)Deep Parsing (2012)
Deep Parsing (2012)Craig Trim
 
Learn C# at ASIT
Learn C# at ASITLearn C# at ASIT
Learn C# at ASITASIT
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
SAS University Edition - Getting Started
SAS University Edition - Getting StartedSAS University Edition - Getting Started
SAS University Edition - Getting StartedCraig Trim
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp PresentationVishwa Mohan
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial Jm Ramos
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course IntroductionIntro C# Book
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 

Andere mochten auch (19)

Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1
 
C# basics
 C# basics C# basics
C# basics
 
D1 Overview of C# programming
D1 Overview of C# programmingD1 Overview of C# programming
D1 Overview of C# programming
 
Project Roslyn: Exposing the C# and VB compiler’s code analysis
Project Roslyn: Exposing the C# and VB compiler’s code analysisProject Roslyn: Exposing the C# and VB compiler’s code analysis
Project Roslyn: Exposing the C# and VB compiler’s code analysis
 
Dependency parsing (2013)
Dependency parsing (2013)Dependency parsing (2013)
Dependency parsing (2013)
 
Deep Parsing (2012)
Deep Parsing (2012)Deep Parsing (2012)
Deep Parsing (2012)
 
C# for beginners
C# for beginnersC# for beginners
C# for beginners
 
Learn C# at ASIT
Learn C# at ASITLearn C# at ASIT
Learn C# at ASIT
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
SAS University Edition - Getting Started
SAS University Edition - Getting StartedSAS University Edition - Getting Started
SAS University Edition - Getting Started
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course Introduction
 
08. Numeral Systems
08. Numeral Systems08. Numeral Systems
08. Numeral Systems
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Ähnlich wie Visula C# Programming Lecture 2

Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdfSergiuMatei7
 
C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# programMAHESHV559910
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfssusera0bb35
 
IntroductionToCSharp.ppt
IntroductionToCSharp.pptIntroductionToCSharp.ppt
IntroductionToCSharp.pptRishikaRuhela
 
Introduction toc sharp
Introduction toc sharpIntroduction toc sharp
Introduction toc sharpSDFG5
 
IntroductionToCSharp.ppt
IntroductionToCSharp.pptIntroductionToCSharp.ppt
IntroductionToCSharp.pptReemaAsker1
 
IntroductionToCSharp.ppt
IntroductionToCSharp.pptIntroductionToCSharp.ppt
IntroductionToCSharp.pptReemaAsker1
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...ANUSUYA S
 

Ähnlich wie Visula C# Programming Lecture 2 (20)

unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
C#
C#C#
C#
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdf
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
Theory1&2
Theory1&2Theory1&2
Theory1&2
 
Basics1
Basics1Basics1
Basics1
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Cordovilla
CordovillaCordovilla
Cordovilla
 
IntroductionToCSharp.ppt
IntroductionToCSharp.pptIntroductionToCSharp.ppt
IntroductionToCSharp.ppt
 
Introduction toc sharp
Introduction toc sharpIntroduction toc sharp
Introduction toc sharp
 
IntroductionToCSharp.ppt
IntroductionToCSharp.pptIntroductionToCSharp.ppt
IntroductionToCSharp.ppt
 
IntroductionToCSharp.ppt
IntroductionToCSharp.pptIntroductionToCSharp.ppt
IntroductionToCSharp.ppt
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
C programming language
C programming languageC programming language
C programming language
 
Unit 2- Module 2.pptx
Unit 2- Module 2.pptxUnit 2- Module 2.pptx
Unit 2- Module 2.pptx
 
C programming notes
C programming notesC programming notes
C programming notes
 
Programming C Part 01
Programming C Part 01 Programming C Part 01
Programming C Part 01
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 

Kürzlich hochgeladen

Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Kürzlich hochgeladen (20)

Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 

Visula C# Programming Lecture 2

  • 1. Introduction to Visual Programming Lecture #2: C# Program Structure identifiers, variables, inc & dec
  • 2. C# Program Structure Program specifications (optional) //========================================================== // // File: HelloWorld.cs CS112 Assignment 00 // // Author: Muhammad Adeel Abid Email: m_adeel_dcs@yahoo.com // // Classes: HelloWorld // -------------------// This program prints a string called "Hello, World!” // //========================================================== Library imports using System; Class and namespace definitions class HelloWorld { static void Main(string[] args) { Console.WriteLine(“Hello, World!”); } } 2
  • 3. Comments Comments Comments are ignored by the compiler: used only for human readers (i.e., inline documentation) Two types of comments • Single-line comments use //… // this comment runs to the end of the line • Multi-lines comments use /* … */ /* this comment runs to the terminating symbol, even across line breaks */ 3
  • 4. Identifiers Identifiers are the words that a programmer uses in a program An identifier can be made up of letters, digits, and the underscore character They cannot begin with a digit C# is case sensitive, therefore args and Args are different identifiers Sometimes we choose identifiers ourselves when writing a program (such as HelloWorld) using System; class HelloWorld Sometimes we are using another { static void Main(string[] args) programmer's code, so we use { Console.WriteLine(“Hello World!”); the identifiers that they chose } (such as WriteLine) } 4
  • 5. Identifiers: Keywords Often we use special identifiers called keywords that already have a predefined meaning in the language Example: class A keyword cannot be used in any other way C# Keywords abstract byte class delegate event fixed goto interface namespace out public sealed static throw ulong value as case const do explicit float if internal new override readonly set string true unchecked virtual base catch continue double extern for implicit is null params ref short struct try unsafe void bool char decimal else false foreach in lock object private return sizeof switch typeof ushort volatile break checked default enum finally get int long operator protected sbyte stackalloc this uint using while All C# keywords are lowercase! 5
  • 6. C# Program Structure: Class // comments about the class class HelloWorld { class header class body Comments can be added almost anywhere } 6
  • 7. C# Classes Each class name is an identifier • Can contain letters, digits, and underscores (_) • Cannot start with digits • Can start with the at symbol (@) Convention: Class names are capitalized, with each additional English word capitalized as well (e.g., MyFirstProgram ) Class bodies start with a left brace ({) Class bodies end with a right brace (}) 7
  • 8. C# Program Structure: Method // comments about the class class HelloWorld { // comments about the method static void Main (string[] args) { Console.Write(“Hello World!”); Console.WriteLine(“This is from CS112!”); } } 8
  • 9. Console Application vs. Window Application Console Application No visual component Only text input and output Run under Command Prompt or DOS Prompt Window Application Forms with many different input and output types Contains Graphical User Interfaces (GUI) GUIs make the input and output more user friendly! Message boxes • Within the System.Windows.Forms namespace • Used to prompt or display information to the user 9
  • 10. Variables A variable is a typed name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name numberOfStudents: int numberOfStudents; … int total; … int average, max; 9200 total: 9204 9208 9212 9216 average: 9220 max: 9224 9228 9232 Which ones are valid variable names? myBigVar 99bottles VAR1 _test @test namespace It’s-all-over 10
  • 11. Assignment An assignment statement changes the value of a variable The assignment operator is the = sign int total; … total = 55; The value on the right is stored in the variable on the left The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type (more later) You can declare and assign initial value to a variable at the same time, e.g., int total = 55; 11
  • 12. Example static void Main(string[] args) { int total; total = 15; System.Console.Write(“total = “); System.Console.WriteLine(total); total = 55 + 5; System.Console.Write(“total = “); System.Console.WriteLine(total); } 12
  • 13. Constants A constant is similar to a variable except that it holds one value for its entire existence The compiler will issue an error if you try to change a constant In C#, we use the constant modifier to declare a constant constant int numberOfStudents = 42; Why constants? give names to otherwise unclear literal values facilitate changes to the code prevent inadvertent errors 13
  • 14. C# Data Types There are 15 data types in C# Eight of them represent integers: byte, sbyte, short, ushort, int, uint, long,ulong Two of them represent floating point numbers float, double One of them represents decimals: decimal One of them represents boolean values: bool One of them represents characters: char One of them represents strings: string One of them represents objects: object 14
  • 15. Numeric Data Types The difference between the various numeric types is their size, and therefore the values they can store: Type Storage Range byte sbyte short ushort int uint long ulong 8 bits 8 bits 16 bits 16 bits 32 bits 32 bits 64 bits 64 bits 0 - 255 -128 - 127 -32,768 - 32767 0 - 65537 -2,147,483,648 – 2,147,483,647 0 – 4,294,967,295 -9×1018 to 9×1018 0 – 1.8×1019 decimal 128 bits ±1.0×10-28; ±7.9×1028 with 28-29 significant digits float double 32 bits 64 bits ±1.5×10-45; ±3.4×1038 with 7 significant digits ±5.0×10-324; ±1.7×10308 with 15-16 significant digits Question: you need a variable to represent world population. Which type do you use? 15
  • 16. Examples of Numeric Variables int x = 1; short y = 10; float pi = 3.14f; // f denotes float float f3 = 7E-02f; // 0.07 double d1 = 7E-100; // use m to denote a decimal decimal microsoftStockPrice = 28.38m; Example: TestNumeric.cs 16
  • 17. Boolean A bool value represents a true or false condition A boolean can also be used to represent any two states, such as a light bulb being on or off The reserved words true and false are the only valid values for a boolean type bool doAgain = true; 17
  • 18. Characters A char is a single character from the a character set A character set is an ordered list of characters; each character is given a unique number C# uses the Unicode character set, a superset of ASCII Uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many languages Code chart can be found at: http://www.unicode.org/charts/ Character literals are represented in a program by delimiting with single quotes, e.g., 'a‘ 'X‘ '7' '$‘ ',‘ char response = ‘Y’; 18
  • 19. Common Escape Sequences Escape sequence Description 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. ’ Used to print a single quote Backslash. Used to print a backslash character. " Double quote. Used to print a double quote (") character. 19
  • 20. string A string represents a sequence of characters, e.g., string message = “Hello World”; 20
  • 21. Data Input Console.ReadLine() Used to get a value from the user input Example string myString = Console.ReadLine(); Convert from string to the correct data type Int32.Parse() • Used to convert a string argument to an integer • Allows math to be preformed once the string is converted • Example: string myString = “1023”; int myInt = Int32.Parse( myString ); Double.Parse() Single.Parse() //for double type variable //for float type variable 21
  • 22. Sample Input //for String String s; Console.Write("Enter a string ="); s = Console.ReadLine(); Console.WriteLine("You enter =" + s); //-----------------------------------//for int int a; Console.Write("Enter an integer ="); a = Int32.Parse(Console.ReadLine()); Console.WriteLine("You enter =" + a); //-----------------------------------//for float float flt; Console.Write("Enter float value ="); flt = Single.Parse(Console.ReadLine()); Console.WriteLine("You enter =" + flt); //-----------------------------------//for double double dbl; Console.Write("Enter Double type value ="); dbl = Double.Parse(Console.ReadLine()); Console.WriteLine("You enter =" + dbl); 22
  • 23. Increment and Decrement ++ is used to denote Increment Prefix increment(++a) Postfix increment(a++) -- is used to denote Decrement Prefix decrement(--a) Postfix decrement(a--) 23