110 likes | 122 Views
CSCI N201 Programming Concepts and Database 7 – Variables, Input and Output Lingma Acheson linglu@iupui.edu. Department of Computer and Information Science, IUPUI. Task:. Ask the user his or her name, and repeat that name to the user E.g. Comp: What is your name? User: Lingma
E N D
CSCI N201 Programming Concepts and Database 7 – Variables, Input and Output Lingma Acheson linglu@iupui.edu Department of Computer and Information Science, IUPUI
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!
Input Requires • A question asked by computer • 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
Output Requires • Output the answer entered by the user • The answer is stored in computer to be used for output. “Lingma” Hi, Lingma! Lingma
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
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?” • http://www.cs.iupui.edu/~aharris/MirJS.html • http://www.cs.iupui.edu/~aharris/n301/miracleSum.html
String Data • Text • Series of alphanumeric characters • Usually enclosed in quotes (“”) • Size depends on length of text
Numeric Data • Integer (+/-,0, no decimals) • Long Integer • Float (includes decimal values) • Double • Size depends on type of number • Miracle – “How old are you?”
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 + “!”
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
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 …”