SlideShare a Scribd company logo
1 of 8
Download to read offline
CS111 Lab
Variables
Instructor: Michael Gordon
What is a variable?
 Variables

are a means of storing data for
the program.
 Variables come in different types and
store different kinds of information:





Integers (whole numbers)
Decimal numbers
Characters
And more…
Common Types
 int

– a positive or negative integer
 double – a positive or negative decimal
 char – a single character
 bool – a true or false value
 string – a string of text
 The above names are case sensitive


Int, Double, Bool, or Char won’t work
Declaring a variable
 To

declare a variable, you write its type
and its name (which you give it).
Examples:




int students; could be an integer variable
that stores the number of students in a class
string name; could store the class title
bool full; could store whether the class is full
or not
Assigning values
 We

use the equal sign to assign values to
a variable. Examples:







students = 30;
name = “CS111”;
full = false;
Strings must be contained within double
quotes.
char variables must be contained within
single quotes.
Initializing
 Sometimes

we declare variables and then
later assign their values, but sometimes we
want to give an initial value right away.
This is called initializing. Examples:




int students = 30;
string name = “CS111”;
bool full = false;
More on variable names
 C++






rules for variable names:

Must start with a letter, not a number
Must have NO space in it
No special characters (only letters,
numbers, and underscore).
Must not be a C++ keyword, such as: return,
if, else, int, double, etc.
Variable initializations
Illegal examples
String name = 45;
int count = 2.5;
char grade = B;
double = ‘3.14’;
bool = “False”;

Legal examples
string name = “45”;
int count = 2;
char grade = ‘B’;
double = 3.14;
bool = False;

More Related Content

Viewers also liked

Recipe of a rockstar developer
Recipe of a rockstar developerRecipe of a rockstar developer
Recipe of a rockstar developer
Topu Newaj
 
Maximizing LinkedIn’s Potential
Maximizing LinkedIn’s PotentialMaximizing LinkedIn’s Potential
Maximizing LinkedIn’s Potential
Susanne Daniels
 
Kooperimi ne bujqesi
Kooperimi ne bujqesiKooperimi ne bujqesi
Kooperimi ne bujqesi
Erland Dalliu
 

Viewers also liked (19)

Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.
 
Recipe of a rockstar developer
Recipe of a rockstar developerRecipe of a rockstar developer
Recipe of a rockstar developer
 
Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2
 
Avoiding Share Market is RISK
Avoiding Share Market is RISKAvoiding Share Market is RISK
Avoiding Share Market is RISK
 
Maximizing LinkedIn’s Potential
Maximizing LinkedIn’s PotentialMaximizing LinkedIn’s Potential
Maximizing LinkedIn’s Potential
 
Mapa conceptual gerencia y ciclo de vida de los proyectos
Mapa conceptual gerencia y ciclo de vida de los proyectosMapa conceptual gerencia y ciclo de vida de los proyectos
Mapa conceptual gerencia y ciclo de vida de los proyectos
 
Reading (carr sanger)
Reading (carr sanger)Reading (carr sanger)
Reading (carr sanger)
 
The Daisy Cookbook
The Daisy CookbookThe Daisy Cookbook
The Daisy Cookbook
 
Retirement planning
Retirement planningRetirement planning
Retirement planning
 
Production deployment
Production deploymentProduction deployment
Production deployment
 
CQRS: Theory
CQRS: Theory CQRS: Theory
CQRS: Theory
 
For loops
For loopsFor loops
For loops
 
CQRS : Introduction
CQRS : IntroductionCQRS : Introduction
CQRS : Introduction
 
РИФ 2016. Дементьева
РИФ 2016. ДементьеваРИФ 2016. Дементьева
РИФ 2016. Дементьева
 
Thumb Rules for Personal Finance
Thumb Rules for Personal FinanceThumb Rules for Personal Finance
Thumb Rules for Personal Finance
 
Are you saving or investing
Are you saving or investingAre you saving or investing
Are you saving or investing
 
Planning for child future
Planning for child futurePlanning for child future
Planning for child future
 
Kooperimi ne bujqesi
Kooperimi ne bujqesiKooperimi ne bujqesi
Kooperimi ne bujqesi
 
PPF or ELSS Mutual Funds
PPF or ELSS Mutual FundsPPF or ELSS Mutual Funds
PPF or ELSS Mutual Funds
 

Similar to Variables

C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
PRASANYA K
 
02. Data Type and Variables
02. Data Type and Variables02. Data Type and Variables
02. Data Type and Variables
Tommy Vercety
 
Should begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdfShould begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdf
sudhinjv
 
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
MMRF2
 

Similar to Variables (20)

Versatile Variables
Versatile VariablesVersatile Variables
Versatile Variables
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
 
Introduction to Variables.pptx
Introduction to Variables.pptxIntroduction to Variables.pptx
Introduction to Variables.pptx
 
02. Data Type and Variables
02. Data Type and Variables02. Data Type and Variables
02. Data Type and Variables
 
Should begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdfShould begin with a letter and may contain additional letters and di.pdf
Should begin with a letter and may contain additional letters and di.pdf
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf
 
Variables&DataTypes.pptx
Variables&DataTypes.pptxVariables&DataTypes.pptx
Variables&DataTypes.pptx
 
Python - variable types
Python - variable typesPython - variable types
Python - variable types
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
JAVA LESSON-01.pptx
JAVA LESSON-01.pptxJAVA LESSON-01.pptx
JAVA LESSON-01.pptx
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C tokens
C tokensC tokens
C tokens
 
Data Types In Python.pptx
Data Types In Python.pptxData Types In Python.pptx
Data Types In Python.pptx
 
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptx
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptx
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 

More from Michael Gordon (17)

Raspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture classRaspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture class
 
Strings2
Strings2Strings2
Strings2
 
Strings1
Strings1Strings1
Strings1
 
Introduction to Computer Science 111 Lab
Introduction to Computer Science 111 LabIntroduction to Computer Science 111 Lab
Introduction to Computer Science 111 Lab
 
Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++
 
Strings
StringsStrings
Strings
 
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
 
Arrays
ArraysArrays
Arrays
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Functions
FunctionsFunctions
Functions
 
Shape logic 1
Shape logic 1Shape logic 1
Shape logic 1
 
While loops
While loopsWhile loops
While loops
 
If statements
If statementsIf statements
If statements
 
Arithmetic
ArithmeticArithmetic
Arithmetic
 
Output
OutputOutput
Output
 
Word cloud
Word cloudWord cloud
Word cloud
 
Millennial white paper
Millennial white paperMillennial white paper
Millennial white paper
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 

Variables

  • 2. What is a variable?  Variables are a means of storing data for the program.  Variables come in different types and store different kinds of information:     Integers (whole numbers) Decimal numbers Characters And more…
  • 3. Common Types  int – a positive or negative integer  double – a positive or negative decimal  char – a single character  bool – a true or false value  string – a string of text  The above names are case sensitive  Int, Double, Bool, or Char won’t work
  • 4. Declaring a variable  To declare a variable, you write its type and its name (which you give it). Examples:    int students; could be an integer variable that stores the number of students in a class string name; could store the class title bool full; could store whether the class is full or not
  • 5. Assigning values  We use the equal sign to assign values to a variable. Examples:      students = 30; name = “CS111”; full = false; Strings must be contained within double quotes. char variables must be contained within single quotes.
  • 6. Initializing  Sometimes we declare variables and then later assign their values, but sometimes we want to give an initial value right away. This is called initializing. Examples:    int students = 30; string name = “CS111”; bool full = false;
  • 7. More on variable names  C++     rules for variable names: Must start with a letter, not a number Must have NO space in it No special characters (only letters, numbers, and underscore). Must not be a C++ keyword, such as: return, if, else, int, double, etc.
  • 8. Variable initializations Illegal examples String name = 45; int count = 2.5; char grade = B; double = ‘3.14’; bool = “False”; Legal examples string name = “45”; int count = 2; char grade = ‘B’; double = 3.14; bool = False;