SlideShare a Scribd company logo
1 of 18
Lab 2
CS106 - Fundamentals of Computer Science
Presented by TA. Nada Kamel
Agenda
 Data types
 Variables declaration
 Character escapes
 Strings
 Literals
 Constants
Presented by TA. Nada Kamel
Data Types
Presented by TA. Nada Kamel
Data Types
Data Type C++ Keyword Byte(s) Typical range
Integer int 4 -2,147,483,648 to
2,147,483,647
Unsigned integer unsigned int 4 0 to 4,294,967,295
Long integer long 8 ~ -9 Billion to ~ 9 Billion
Floating point float 4 +/- 3.4e +/- 38 (~7 digits)
Double floating point double 8 +/- 1.7e +/- 308 (~15 digits)
Character char 1 128 to 127 or 0 to 255
String/Text string ∞ -
Presented by TA. Nada Kamel
Data Types (Cont.)
 Boolean  bool
 Void  void
Presented by TA. Nada Kamel
Variable Declaration
Presented by TA. Nada Kamel
Variable Declaration
 C++ requires every variable to be declared with its type BEFORE its first
use.
 Examples:
int height;
char letter;
float radius, diameter;
 In these examples, variables have an undetermined value.
Presented by TA. Nada Kamel
Variable Initialization
 Allows variable to have a specific value from the moment it is declared.
 Example:
int depth = 67;
(data type) (identifier) = initial_value;
Presented by TA. Nada Kamel
Character Escapes
Presented by TA. Nada Kamel
Character Escapes
Keyword Meaning
n or endl newline
r carriage return
t tab
v vertical tab
b backspace
a alert (beep sound)
’ single quote
” double quote
? question mark
 backslash
Presented by TA. Nada Kamel
Strings
Presented by TA. Nada Kamel
Strings
 Strings are able to store sequences of characters.
 To utilize strings we must include a directive
#include <string>
 Can be initialized with any of the three valid initialization formats:
string mystring = “Hello World!”;
string mystring(“Hello World!”);
string mystring {“Hello World!”};
Presented by TA. Nada Kamel
Strings (Cont.)
 To input a string you will need to use:
getline(cin, mystring);
 If the user will enter one word only, cin >> mystring can be used.
 Example:
#include <iostream>
#include <string>
void main()
{
string mystring;
getline(cin, mystring);
std:: cout << “The string entered is ” << mystring << endl;
}
Presented by TA. Nada Kamel
Literals
Presented by TA. Nada Kamel
Literals
 Used to express particular values.
 Literal constants can be classified into: integers, floating-point, character,
string, boolean.
 Examples:
707
-20589
3.14
1.6e-19
‘z’
“Hello World!”
true
Presented by TA. Nada Kamel
Typed Constants
Presented by TA. Nada Kamel
Constants
 Can be used to give names to constant values.
 Example:
const float pi = 3.14159;
 The value of the constant variable cannot be changed after being
initialized.
Presented by TA. Nada Kamel
Any Questions?
Presented by TA. Nada Kamel

More Related Content

What's hot (20)

Strings IN C
Strings IN CStrings IN C
Strings IN C
 
String in c
String in cString in c
String in c
 
Strings in C
Strings in CStrings in C
Strings in C
 
C programming - String
C programming - StringC programming - String
C programming - String
 
C# Strings
C# StringsC# Strings
C# Strings
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings
StringsStrings
Strings
 
Strings1
Strings1Strings1
Strings1
 
Managing I/O & String function in C
Managing I/O & String function in CManaging I/O & String function in C
Managing I/O & String function in C
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Strings in c
Strings in cStrings in c
Strings in c
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Strings
StringsStrings
Strings
 
string in C
string in Cstring in C
string in C
 
String functions in C
String functions in CString functions in C
String functions in C
 
String.ppt
String.pptString.ppt
String.ppt
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 

Similar to CS106 Lab 2 - Variables declaration

Similar to CS106 Lab 2 - Variables declaration (20)

JAVA PROGRAMMING : Data types
JAVA PROGRAMMING : Data typesJAVA PROGRAMMING : Data types
JAVA PROGRAMMING : Data types
 
Chapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfChapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdf
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
U4.ppt
U4.pptU4.ppt
U4.ppt
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Strings
StringsStrings
Strings
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Java Object Orientend Programming 1.pptx
Java Object Orientend Programming 1.pptxJava Object Orientend Programming 1.pptx
Java Object Orientend Programming 1.pptx
 
Unit 2
Unit 2Unit 2
Unit 2
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
STRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxSTRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptx
 
Data type in c
Data type in cData type in c
Data type in c
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 

More from Nada Kamel

CS106 Lab 8 - Nested loops
CS106 Lab 8 - Nested loopsCS106 Lab 8 - Nested loops
CS106 Lab 8 - Nested loopsNada Kamel
 
CS106 Lab 7 - For loop
CS106 Lab 7 - For loopCS106 Lab 7 - For loop
CS106 Lab 7 - For loopNada Kamel
 
CS106 Lab 9 - 1D array
CS106 Lab 9 - 1D arrayCS106 Lab 9 - 1D array
CS106 Lab 9 - 1D arrayNada Kamel
 
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)Nada Kamel
 
CS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loopCS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loopNada Kamel
 
CS106 Lab 4 - If statement
CS106 Lab 4 - If statementCS106 Lab 4 - If statement
CS106 Lab 4 - If statementNada Kamel
 
CS106 Lab 5 - Switch case
CS106 Lab 5 - Switch caseCS106 Lab 5 - Switch case
CS106 Lab 5 - Switch caseNada Kamel
 
CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)Nada Kamel
 
CS106 Lab 3 - Modulus
CS106 Lab 3 - ModulusCS106 Lab 3 - Modulus
CS106 Lab 3 - ModulusNada Kamel
 
CS106 Lab 1 - Introduction
CS106 Lab 1 - IntroductionCS106 Lab 1 - Introduction
CS106 Lab 1 - IntroductionNada Kamel
 

More from Nada Kamel (10)

CS106 Lab 8 - Nested loops
CS106 Lab 8 - Nested loopsCS106 Lab 8 - Nested loops
CS106 Lab 8 - Nested loops
 
CS106 Lab 7 - For loop
CS106 Lab 7 - For loopCS106 Lab 7 - For loop
CS106 Lab 7 - For loop
 
CS106 Lab 9 - 1D array
CS106 Lab 9 - 1D arrayCS106 Lab 9 - 1D array
CS106 Lab 9 - 1D array
 
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)
 
CS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loopCS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loop
 
CS106 Lab 4 - If statement
CS106 Lab 4 - If statementCS106 Lab 4 - If statement
CS106 Lab 4 - If statement
 
CS106 Lab 5 - Switch case
CS106 Lab 5 - Switch caseCS106 Lab 5 - Switch case
CS106 Lab 5 - Switch case
 
CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 11 - Functions (passing by reference)
 
CS106 Lab 3 - Modulus
CS106 Lab 3 - ModulusCS106 Lab 3 - Modulus
CS106 Lab 3 - Modulus
 
CS106 Lab 1 - Introduction
CS106 Lab 1 - IntroductionCS106 Lab 1 - Introduction
CS106 Lab 1 - Introduction
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsPallavi Parmar
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfElizabeth Walsh
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 

Recently uploaded (20)

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell Tolls
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 

CS106 Lab 2 - Variables declaration

  • 1. Lab 2 CS106 - Fundamentals of Computer Science Presented by TA. Nada Kamel
  • 2. Agenda  Data types  Variables declaration  Character escapes  Strings  Literals  Constants Presented by TA. Nada Kamel
  • 3. Data Types Presented by TA. Nada Kamel
  • 4. Data Types Data Type C++ Keyword Byte(s) Typical range Integer int 4 -2,147,483,648 to 2,147,483,647 Unsigned integer unsigned int 4 0 to 4,294,967,295 Long integer long 8 ~ -9 Billion to ~ 9 Billion Floating point float 4 +/- 3.4e +/- 38 (~7 digits) Double floating point double 8 +/- 1.7e +/- 308 (~15 digits) Character char 1 128 to 127 or 0 to 255 String/Text string ∞ - Presented by TA. Nada Kamel
  • 5. Data Types (Cont.)  Boolean  bool  Void  void Presented by TA. Nada Kamel
  • 7. Variable Declaration  C++ requires every variable to be declared with its type BEFORE its first use.  Examples: int height; char letter; float radius, diameter;  In these examples, variables have an undetermined value. Presented by TA. Nada Kamel
  • 8. Variable Initialization  Allows variable to have a specific value from the moment it is declared.  Example: int depth = 67; (data type) (identifier) = initial_value; Presented by TA. Nada Kamel
  • 10. Character Escapes Keyword Meaning n or endl newline r carriage return t tab v vertical tab b backspace a alert (beep sound) ’ single quote ” double quote ? question mark backslash Presented by TA. Nada Kamel
  • 12. Strings  Strings are able to store sequences of characters.  To utilize strings we must include a directive #include <string>  Can be initialized with any of the three valid initialization formats: string mystring = “Hello World!”; string mystring(“Hello World!”); string mystring {“Hello World!”}; Presented by TA. Nada Kamel
  • 13. Strings (Cont.)  To input a string you will need to use: getline(cin, mystring);  If the user will enter one word only, cin >> mystring can be used.  Example: #include <iostream> #include <string> void main() { string mystring; getline(cin, mystring); std:: cout << “The string entered is ” << mystring << endl; } Presented by TA. Nada Kamel
  • 15. Literals  Used to express particular values.  Literal constants can be classified into: integers, floating-point, character, string, boolean.  Examples: 707 -20589 3.14 1.6e-19 ‘z’ “Hello World!” true Presented by TA. Nada Kamel
  • 17. Constants  Can be used to give names to constant values.  Example: const float pi = 3.14159;  The value of the constant variable cannot be changed after being initialized. Presented by TA. Nada Kamel
  • 18. Any Questions? Presented by TA. Nada Kamel