Download presentation
Presentation is loading. Please wait.
1
7 – Variables, Input and Output
CSCI N201 Programming Concepts and Database 7 – Variables, Input and Output Lingma Acheson Department of Computer and Information Science, IUPUI
2
Task: Ask the user his or her name, and repeat that name to the user
E.g. Comp: What is your name? User: Lingma Comp: Hi Lingma!
3
Input Requires A question asked by computer An answer to the question
E.g. What is your name? How old are you? Where do you live? How much is two plus two? An answer to the question E.g. Lingma 12 Indiana
4
Output Requires Output the answer entered by the user
The answer is stored in computer to be used for output. “Lingma” Hi, Lingma! Lingma
5
Variable A place in memory to hold things Like a container
Different containers for different stuff Need to plan what and how many variables are needed, and create variables first
6
Variables require: Name – descriptive Init – starting value
Type – kind of data (string, numeric) Purpose – or description You can't use a variable if you haven't created the variable Miracle – “What is your name?”
7
String Data Text Series of alphanumeric characters
Usually enclosed in quotes (“”) Size depends on length of text
8
Numeric Data Integer (+/-,0, no decimals) Long Integer
Float (includes decimal values) Double Size depends on type of number Miracle – “How old are you?”
9
String Concatenation Technique for combining text “a” + “b” = “ab”
Frequently uses the plus sign Can be used to combine variables and literals: Miracle - “Hi,” + userName + “!”
10
Inside the Code Understand the code function main(){ …
} – main structure // - comments, codes behind this won’t be executed ; - end of a statement Code is executed line by line
11
Play with Miracle Answer 2 or 3 questions Output a longer sentence
A simple calculator Adding two numbers and output the result Ask the user to enter the first number Ask the user to enter the second number Change the first variable to integer Change the second variable to integer Add the two variables and store in the third variable Output – “The result is …”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.