SlideShare a Scribd company logo
1 of 27
Data Types 
Lecture 2 
Dr. Hakem Beitollahi 
Computer Engineering Department 
Soran University
Objectives of this lecture 
 In this chapter you will learn: 
 Increment and Decrement (++ and --) 
 Assignment Operators 
 Basic data types in C# 
 Constant values 
 Conditional logical operators 
Data Types— 2
Main Mathematic Operations 
Operator Action 
+ Addition 
- Subtraction 
* Multiply 
/ Division 
% Reminder 
++ Increment 
-- Decrement 
Data Types— 3
Increment & decrement (I) 
 C/C++/C# includes two useful operators not found in some other 
computer languages. 
 These are the increment and decrement operators, ++ and - -  
 The operator ++ adds 1 to its operand, and − − subtracts 1. 
 i++ = i + 1 
 i-- = i - 1 
 Both the increment and decrement operators may either precede 
(prefix) or follow (postfix) the operand 
 i = i+1 can be written as i++ or ++i 
 i = i -1 can be written as i-- or --i 
 Please note: There is, however, a difference between the prefix and 
postfix forms when you use these operators in an expression 
Data Types— 4
Increment & decrement (II) 
// Increment and Decrement Operations ++/-- 
using System; 
class Comparison 
{ 
static void Main( string[] args ) 
{ 
int a = 10; 
int b = 10; 
int x, y; 
x = a++; 
y = ++b; 
Console.WriteLine("x = {0}, a ={1}.", x, a); 
Console.WriteLine("y = {0}, b ={1}.", y, b); 
}// end method Main 
} // end class Comparison 
x = 10 a = 11 
y = 11 b = 11 
Data Types— 5
Increment & decrement (III) 
// Increment and Decrement Operations ++/-- 
using System; 
class Comparison 
{ 
static void Main( string[] args ) 
{ 
int a = 10; 
int b = 10; 
Console.WriteLine(a++); 
Console.WriteLine(a); 
Console.WriteLine(++b); 
Console.WriteLine(b); 
}// end method Main 
} // end class Comparison 
10 
11 
11 
11 
Data Types— 6
Common Programming Error 1 
Attempting to use the increment or 
decrement operator on an expression 
other than a variable reference is a syntax 
error. A variable reference is a variable or 
expression that can appear on the left side 
of an assignment operation. For example, 
writing ++(x + 1) is a syntax error, 
because (x + 1) is not a variable 
reference 
Data Types — 7
Assignment Operator 
Data Types — 8
Assignment Operator (I) 
 C# provides several assignment operators for 
abbreviating assignment expressions. 
 Example: 
 c = c + 3; 
 c += 3; 
 where operator is one of the binary operators +, 
-, *, / or %, can be written in the form 
 variable operator= expression; 
Data Types — 9
Assignment Operator (II) 
Data Types — 10
Common Programming Error 1 
Placing a space character between symbols 
that compose an arithmetic assignment 
operator is a syntax error. 
A += 6; True A + = 6; False 
Data Types — 11
Basic Data Types in C# 
Data Types— 12
Basic data types in C# (I) 
 Programming languages store and 
process data in various ways depending 
on the type of the data; consequently, all 
data read, processed, or written by a 
program must have a type 
 A data type is used to 
 Identify the type of a variable when the 
variable is declared 
 Identify the type of the return value of a 
function (later) 
 Identify the type of a parameter expected by 
a function (later) 
Data Types— 13
Basic data types in C# (II) 
 There are 7 major data types in C++ 
 char e.g., ‘a’, ‘c’, ‘@’ 
 string e.g., “Zagros” 
 int e.g., 23, -12, 5, 0, 145678 
 float e.g., 54.65, -0.004, 65 
 double e.g., 54.65, -0.004, 65 
 bool only true and false 
 void no value 
Data Types— 14
Basic data types in C# (III) 
Type Range Size (bits) 
char U+0000 to U+ffff Unicode 16-bit character 
sbyte -128 to 127 Signed 8-bit integer 
byte 0 to 255 Unsigned 8-bit integer 
short -32,768 to 32,767 Signed 16-bit integer 
ushort 0 to 65535 Unsigned 16-bit integer 
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer 
uint 0 to 4,294,967,295 Unsigned 32-bit integer 
long -9,223,372,036,854,775,808 to 
9,223,372,036,854,775,807 
Signed 64-bit integer 
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer 
float -3.402823e38 .. 3.402823e38 Signed 32 bits 
double -1.79769313486232e308 .. 
1.79769313486232e308 
Signed 64 bits 
decimal -79228162514264337593543950335 .. 
79228162514264337593543950335 
Signed 128 bits 
Data Types— 15
Basic data types in C# (IV) 
 The general form of a declaration is 
 Type variable-list 
 Examples 
 int I, j, k; 
 char ch, a; 
 float f, balance; 
 double d; 
 bool decision; 
 string str; 
 Variables name 
Correct incorrect 
Count 3count 
test23 hi!there 
high_balance high...balance 
_name Test? 
@count co@nt 
 The first character must be a letter 
 -an underscore or @ 
 The subsequent characters must be either letters, digits, or 
underscores 
Data Types— 16
Constants in C# 
Data Types— 17
Const variables (I) 
 Variables of type const may not be changed by your 
program 
 The compiler is free to place variables of this type into 
read-only memory (ROM). 
 Example: 
 const int a = 10; 
Data Types— 18
// Constant learning 
using System; 
class Constant 
{ 
static void Main( string[] args ) 
{ 
const int c = 999; 
// c = 82; Error becuase c is constant and you cannot change it 
// c = 999; Error becuase c is constant and you cannot change it 
Console.WriteLine(c); 
}// end method Main 
} // end class Constant 
Data Types— 19
Conditional Logical operators 
Data Types— 20
Conditional Logical operators (I) 
 Conditional logical operators are useful when we want to 
test multiple conditions. 
 There are 3 types of conditional logical operators and 
they work the same way as the boolean AND, OR and 
NOT operators. 
 && - Logical AND 
 All the conditions must be true for the whole 
expression to be true. 
 Example: if (a == 10 && b == 9 && d == 1) 
means the if statement is only true when a == 10 and 
b == 9 and d == 1. 
Data Types— 21
expression1 expression2 expression1 && expression2 
false false false 
false true false 
true false false 
true true true 
Data Types— 22
Conditional Logical operators (II) 
 || - Conditional logical OR 
 The truth of one condition is enough to make 
the whole expression true. 
 Example: if (a == 10 || b == 9 || d == 1) 
means the if statement is true when either 
one of a, b or d has the right value. 
 ! – Conditional logical NOT (also called 
logical negation) 
 Reverse the meaning of a condition 
 Example: if (!(points > 90)) 
means if points not bigger than 90. 
Data Types— 23
expression1 expression2 expression1 || expression2 
false false false 
false true true 
true false true 
true true true 
Expression !expression 
false true 
true false 
Data Types— 24
Data Types — 25 
// Conditional logical operator 
using System; 
class Conditional_logical 
{ 
static void Main( string[] args ) 
{ 
int a = 10; 
int b = 15; 
if ((a == 10) && (b == 15)) 
Console.WriteLine("Apple"); 
else 
Console.WriteLine("Orange"); 
if ((a == 10) || (b == 15)) 
Console.WriteLine("Apple"); 
else 
Console.WriteLine("Orange"); 
if (!(a > 20)) 
Console.WriteLine("Apple"); 
else 
Console.WriteLine("Orange"); 
}// end method Main 
} // end class 
Apple 
Apple 
Apple
Operator preferences 
Operators Preferences 
() 1 
!,~,--,++ 2 
*,/,% 3 
+, - 4 
<, <=, >=, > 5 
==, != 6 
&& 7 
|| 8 
Note: in a+++a*a, the * has preference over ++ 
Note: in ++a+a*a, the ++ has preference over * 
Data Types— 26
Common Programming Error Tip 
Although 3 < x < 7 is a mathematically correct 
condition, it does not evaluate as you might expect in 
C#. Use ( 3 < x && x < 7 ) to get the proper 
evaluation in C#. 
Using operator == for assignment and using operator = 
for equality are logic errors. 
Use your text editor to search for all occurrences of = in 
your program and check that you have the correct 
assignment operator or logical operator in each place. 
Data Types— 27

More Related Content

What's hot

What's hot (19)

CP Handout#3
CP Handout#3CP Handout#3
CP Handout#3
 
C++
C++C++
C++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
CP Handout#1
CP Handout#1CP Handout#1
CP Handout#1
 
CSharp Language Overview Part 1
CSharp Language Overview Part 1CSharp Language Overview Part 1
CSharp Language Overview Part 1
 
Getting started with c++.pptx
Getting started with c++.pptxGetting started with c++.pptx
Getting started with c++.pptx
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C Token’s
C Token’sC Token’s
C Token’s
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Cs211 module 1_2015
Cs211 module 1_2015Cs211 module 1_2015
Cs211 module 1_2015
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 

Viewers also liked

Bingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answerBingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answer
JulianDraxler
 
Student organization president and vice president training
Student organization president and vice president trainingStudent organization president and vice president training
Student organization president and vice president training
BelmontSELD
 

Viewers also liked (19)

British food
British foodBritish food
British food
 
Administrative
AdministrativeAdministrative
Administrative
 
Bingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answerBingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answer
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
Arabic Translation: نظام مكافحة التجسس الاستباقي بوصفه جزءاً من استمرارية الأ...
 
інтернет і соц. сеті
інтернет і соц. сетіінтернет і соц. сеті
інтернет і соц. сеті
 
Student organization president and vice president training
Student organization president and vice president trainingStudent organization president and vice president training
Student organization president and vice president training
 
OSAC: Personal Digital Security Presentation
OSAC: Personal Digital Security PresentationOSAC: Personal Digital Security Presentation
OSAC: Personal Digital Security Presentation
 
Pengukuran aliran a.(differential)
Pengukuran aliran a.(differential)Pengukuran aliran a.(differential)
Pengukuran aliran a.(differential)
 
Who says 'everything's alright' (3)
Who says 'everything's alright'  (3)Who says 'everything's alright'  (3)
Who says 'everything's alright' (3)
 
Manual de Arborizacao Urbana
Manual de Arborizacao UrbanaManual de Arborizacao Urbana
Manual de Arborizacao Urbana
 
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
 
Tagmax_ebooklet
Tagmax_ebookletTagmax_ebooklet
Tagmax_ebooklet
 
Anti inflammatory agents
Anti inflammatory agentsAnti inflammatory agents
Anti inflammatory agents
 
Math Project for Mr. Medina's Class
Math Project for Mr. Medina's ClassMath Project for Mr. Medina's Class
Math Project for Mr. Medina's Class
 
Dental arts davis square
Dental arts davis squareDental arts davis square
Dental arts davis square
 
огюст роден
огюст роденогюст роден
огюст роден
 
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
Cyber Espionage and Insider Threat: House of Commons Panel DiscussionCyber Espionage and Insider Threat: House of Commons Panel Discussion
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
 
Proactive Counterespionage as a Part of Business Continuity and Resiliency
Proactive Counterespionage as a Part of Business Continuity and ResiliencyProactive Counterespionage as a Part of Business Continuity and Resiliency
Proactive Counterespionage as a Part of Business Continuity and Resiliency
 

Similar to Lecture 2

C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 

Similar to Lecture 2 (20)

the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
C program
C programC program
C program
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
 
What is c
What is cWhat is c
What is c
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressions
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
C material
C materialC material
C material
 
Programming in C
Programming in CProgramming in C
Programming in C
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 

More from Soran University (6)

Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 

Recently uploaded

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Recently uploaded (20)

Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 

Lecture 2

  • 1. Data Types Lecture 2 Dr. Hakem Beitollahi Computer Engineering Department Soran University
  • 2. Objectives of this lecture  In this chapter you will learn:  Increment and Decrement (++ and --)  Assignment Operators  Basic data types in C#  Constant values  Conditional logical operators Data Types— 2
  • 3. Main Mathematic Operations Operator Action + Addition - Subtraction * Multiply / Division % Reminder ++ Increment -- Decrement Data Types— 3
  • 4. Increment & decrement (I)  C/C++/C# includes two useful operators not found in some other computer languages.  These are the increment and decrement operators, ++ and - -  The operator ++ adds 1 to its operand, and − − subtracts 1.  i++ = i + 1  i-- = i - 1  Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand  i = i+1 can be written as i++ or ++i  i = i -1 can be written as i-- or --i  Please note: There is, however, a difference between the prefix and postfix forms when you use these operators in an expression Data Types— 4
  • 5. Increment & decrement (II) // Increment and Decrement Operations ++/-- using System; class Comparison { static void Main( string[] args ) { int a = 10; int b = 10; int x, y; x = a++; y = ++b; Console.WriteLine("x = {0}, a ={1}.", x, a); Console.WriteLine("y = {0}, b ={1}.", y, b); }// end method Main } // end class Comparison x = 10 a = 11 y = 11 b = 11 Data Types— 5
  • 6. Increment & decrement (III) // Increment and Decrement Operations ++/-- using System; class Comparison { static void Main( string[] args ) { int a = 10; int b = 10; Console.WriteLine(a++); Console.WriteLine(a); Console.WriteLine(++b); Console.WriteLine(b); }// end method Main } // end class Comparison 10 11 11 11 Data Types— 6
  • 7. Common Programming Error 1 Attempting to use the increment or decrement operator on an expression other than a variable reference is a syntax error. A variable reference is a variable or expression that can appear on the left side of an assignment operation. For example, writing ++(x + 1) is a syntax error, because (x + 1) is not a variable reference Data Types — 7
  • 9. Assignment Operator (I)  C# provides several assignment operators for abbreviating assignment expressions.  Example:  c = c + 3;  c += 3;  where operator is one of the binary operators +, -, *, / or %, can be written in the form  variable operator= expression; Data Types — 9
  • 10. Assignment Operator (II) Data Types — 10
  • 11. Common Programming Error 1 Placing a space character between symbols that compose an arithmetic assignment operator is a syntax error. A += 6; True A + = 6; False Data Types — 11
  • 12. Basic Data Types in C# Data Types— 12
  • 13. Basic data types in C# (I)  Programming languages store and process data in various ways depending on the type of the data; consequently, all data read, processed, or written by a program must have a type  A data type is used to  Identify the type of a variable when the variable is declared  Identify the type of the return value of a function (later)  Identify the type of a parameter expected by a function (later) Data Types— 13
  • 14. Basic data types in C# (II)  There are 7 major data types in C++  char e.g., ‘a’, ‘c’, ‘@’  string e.g., “Zagros”  int e.g., 23, -12, 5, 0, 145678  float e.g., 54.65, -0.004, 65  double e.g., 54.65, -0.004, 65  bool only true and false  void no value Data Types— 14
  • 15. Basic data types in C# (III) Type Range Size (bits) char U+0000 to U+ffff Unicode 16-bit character sbyte -128 to 127 Signed 8-bit integer byte 0 to 255 Unsigned 8-bit integer short -32,768 to 32,767 Signed 16-bit integer ushort 0 to 65535 Unsigned 16-bit integer int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer uint 0 to 4,294,967,295 Unsigned 32-bit integer long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer float -3.402823e38 .. 3.402823e38 Signed 32 bits double -1.79769313486232e308 .. 1.79769313486232e308 Signed 64 bits decimal -79228162514264337593543950335 .. 79228162514264337593543950335 Signed 128 bits Data Types— 15
  • 16. Basic data types in C# (IV)  The general form of a declaration is  Type variable-list  Examples  int I, j, k;  char ch, a;  float f, balance;  double d;  bool decision;  string str;  Variables name Correct incorrect Count 3count test23 hi!there high_balance high...balance _name Test? @count co@nt  The first character must be a letter  -an underscore or @  The subsequent characters must be either letters, digits, or underscores Data Types— 16
  • 17. Constants in C# Data Types— 17
  • 18. Const variables (I)  Variables of type const may not be changed by your program  The compiler is free to place variables of this type into read-only memory (ROM).  Example:  const int a = 10; Data Types— 18
  • 19. // Constant learning using System; class Constant { static void Main( string[] args ) { const int c = 999; // c = 82; Error becuase c is constant and you cannot change it // c = 999; Error becuase c is constant and you cannot change it Console.WriteLine(c); }// end method Main } // end class Constant Data Types— 19
  • 20. Conditional Logical operators Data Types— 20
  • 21. Conditional Logical operators (I)  Conditional logical operators are useful when we want to test multiple conditions.  There are 3 types of conditional logical operators and they work the same way as the boolean AND, OR and NOT operators.  && - Logical AND  All the conditions must be true for the whole expression to be true.  Example: if (a == 10 && b == 9 && d == 1) means the if statement is only true when a == 10 and b == 9 and d == 1. Data Types— 21
  • 22. expression1 expression2 expression1 && expression2 false false false false true false true false false true true true Data Types— 22
  • 23. Conditional Logical operators (II)  || - Conditional logical OR  The truth of one condition is enough to make the whole expression true.  Example: if (a == 10 || b == 9 || d == 1) means the if statement is true when either one of a, b or d has the right value.  ! – Conditional logical NOT (also called logical negation)  Reverse the meaning of a condition  Example: if (!(points > 90)) means if points not bigger than 90. Data Types— 23
  • 24. expression1 expression2 expression1 || expression2 false false false false true true true false true true true true Expression !expression false true true false Data Types— 24
  • 25. Data Types — 25 // Conditional logical operator using System; class Conditional_logical { static void Main( string[] args ) { int a = 10; int b = 15; if ((a == 10) && (b == 15)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); if ((a == 10) || (b == 15)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); if (!(a > 20)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); }// end method Main } // end class Apple Apple Apple
  • 26. Operator preferences Operators Preferences () 1 !,~,--,++ 2 *,/,% 3 +, - 4 <, <=, >=, > 5 ==, != 6 && 7 || 8 Note: in a+++a*a, the * has preference over ++ Note: in ++a+a*a, the ++ has preference over * Data Types— 26
  • 27. Common Programming Error Tip Although 3 < x < 7 is a mathematically correct condition, it does not evaluate as you might expect in C#. Use ( 3 < x && x < 7 ) to get the proper evaluation in C#. Using operator == for assignment and using operator = for equality are logic errors. Use your text editor to search for all occurrences of = in your program and check that you have the correct assignment operator or logical operator in each place. Data Types— 27