C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

Similar documents
CSE / ENGR 142 Programming I

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Display Input and Output (I/O)

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

What are the most likely declarations of "month" in the old and new versions of the program?

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Fundamental of Programming (C)

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

COMP 202 Java in one week

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Computing Seminar Introduction Oct

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

C: How to Program. Week /Mar/05

3. Simple Types, Variables, and Constants

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

printf( Please enter another number: ); scanf( %d, &num2);

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

a data type is Types

Introduction to Programming EC-105. Lecture 2

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

User Defined Functions

Fundamentals of Programming

IS12 - Introduction to Programming. Data Types and Variables

Fundamentals of Programming CS-110. Lecture 2

Pointers and scanf() Steven R. Bagley

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

3. Java - Language Constructs I

AP Computer Science Unit 1. Writing Programs Using BlueJ

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

ET156 Introduction to C Programming

Midterms Save the Dates!

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

A brief intro to pointers for the purposes of passing reference parameters

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Python Input, output and variables

Introduction to Scientific Computing Lecture 1

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Course Outline Introduction to C-Programming

Syntax and Variables

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2 - Introduction to C Programming

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

ET156 Introduction to C Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

BSM540 Basics of C Language

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Welcome to the Primitives and Expressions Lab!

Programming Lecture 3

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

EECE.2160: ECE Application Programming Fall 2017

Chapter 1 & 2 Introduction to C Language

More about Binary 9/6/2016

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Course Outline. Introduction to java

WARM UP LESSONS BARE BASICS

Variables, expressions and statements

CS102: Variables and Expressions

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Data types Expressions Variables Assignment. COMP1400 Week 2

3. Java - Language Constructs I

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

CSE 142 Wi01 Midterm 2 page 1 of 6

C Programming Basics

COMP-202: Foundations of Programming

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Variables and Constants

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 11 Introduction to Programming in C

Getting started with C++ (Part 2)

Decisions, Decisions. Testing, testing C H A P T E R 7

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

CSCI 2132 Software Development. Lecture 8: Introduction to C

Transcription:

CSE 142 Computer Programming I Variables Overview Concepts this lecture: Variables Declarations Identifiers and Reserved Words Types Expressions Assignment statement Variable initialization 2000 UW CSE C-1 C-2 Review: Computer Organization Review: Memory Memory is a collection of locations Disk Central Processing Unit Main Memory Monitor Keyboard Network mouse C-3 Within a program, the locations are called variables Each variable has A name (an identifier) A type (the kind of information it can contain) Basic types include int (integers whole numbers: 17, -42) double (floating-point numbers with a fraction or exponent or both: 3.14159, 6.02e23) char (character data: 'a', '', 'N', ' ', '9') C-4 Memory example Declaring Variables Variable declarations in C int i = 12; double gasprice = 1.799; char bang =! ; Picture: i gasprice bang 12 1.799! (int) (double) (char) C-5 int months; Integer variables represent whole numbers: 1, 17, -32, 0 Not 1.5, 2.0, A double pi; Floating point variables represent real numbers: 3.14, -27.5, 6.02e23, 5.0 Not 3 char first_initial, middle_initial, marital_status; Character variables represent individual keyboard characters: a, b, M, 0, 9, #, Not "Bill" C-6 C-1

Variable Names "Identifiers" are names for things in a program for examples, names of variables In C, identifiers follow certain rules: use letters, numerals, and underscore ( _ ) do not begin with a numeral cannot be reserved words are "case-sensitive" can be arbitrarily long but... Style point: Good choices for identifiers can be extremely helpful in understanding programs Often useful: noun or noun phrase describing Reserved words Certain identifiers have a "reserved" (permanent, special) meaning in C We ve seen int already Will see a couple of dozen more eventually These words always have that special meaning, and cannot be used for other purposes. Cannot be used names of variables Must be spelled exactly right Sometimes also called keywords C-7 C-8 variable contents Under the Hood All information in the CPU or memory is actually a series of bits: 1 s and 0 s Known as binary data Amazingly, all kinds of data can be represented in binary: numbers, letters, sounds, pictures, etc. The type of a variable specifies how the bits are interpreted Binary 0001 C type int char double (sample) value 161.73 Normally we ignore the underlying bits and work with C types A C-9 Assignment Statements An assignment statement stores a value into a variable. The assignment may specify a simple value to be stored, or an expression int area, length, width; length = 16; width = 32; area = length * width; /* declaration of 3 variables */ /* "length gets 16" */ /* "width gets 32" */ /* "area gets length times width" */ Execution of an assignment statement is done in two distinct steps: Evaluate the expression on the right hand side Store the value of the expression into the C- variable named on the left hand side my_age = my_age+1 This is a statement, not an equation. Is there a difference The same variable may appear on both sides of an assignment statement my_age = my_age + 1 ; balance = balance + deposit ; The old value of the variable is used to compute the value of the expression, before the variable is changed. It s not the same as in algebra! C-11 Program Execution A memory location is reserved by declaring a C variable You should give the variable a name that helps someone else reading the program understand what it is used for in that program Once all variables have been assigned memory locations, program execution begins The CPU executes instructions one at a time, C-12 in order of their appearance in the program (we will introduce more options later) C-2

An Example /* calculate and print area of x3 rectangle */ int rectanglelength; int rectanglewidth; int rectanglearea; rectanglelength = ; rectanglewidth = 3; rectanglearea = rectanglelength * rectanglewidth ; printf("%d", rectanglearea); Hand Simulation (Trace) A useful practice is to simulate by hand the operation of the program, step by step. This program has three variables, which we can depict by drawing boxes or making a table We mentally execute each of the instructions, in sequence, and refer to the variables to determine the effect of the instruction C-13 C-14 Tracing the Program Tracing the Program rectanglelength rectanglewidth rectanglearea rectanglelength rectanglewidth rectanglearea declaration statement 1 declaration statement 1 statement 2 statement 3 3 3 30 C-15 C-16 Initializing Variables Initialization means giving something a value for the first time. Initialization Rule General rule: variables have to be initialized before their value is used. Anything which changes the value of a variable is a potential way of initializing it. For now, that means an initial value in a declaration or an assignment statement C-17 Failure to initialize... is a common source of bugs is a semantic error, not a syntax error Variables in a C program are not automatically initialized to 0! C-18 C-3

Declaring vs Initializing int main (void) double income; /*declaration of income, not an assignment or initialization */ income = 35500.00; /*assignment to income, initialization of income, not a declaration.*/ printf ("Old income is %f", income); income = 39000.00; /*assignment to income, not a declaration,or initialization */ printf ("After raise: %f", income); Example Problem: Fahrenheit to Celsius Problem (specified): Convert Fahrenheit temperature to Celsius Algorithm (result of analysis): Celsius = 5/9 (Fahrenheit - 32) C-19 What kind of data (result of analysis): C-20 Fahrenheit to Celsius (I) An actual C program celsius = (fahrenheit - 32.0) * 5.0 / 9.0; C-21 Fahrenheit to Celsius (II) printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; printf("that equals %f degrees Celsius.", celsius); C-22 Running the Program Assignment step-by-step Enter a Fahrenheit temperature: 45.5 That equals 7.500000 degrees Celsius Program trace fahrenheit celsius declaration first printf scanf 45.5 assignment 45.5 7.5 second printf 45.5 7.5 C-23 celsius = (fahrenheit-32.0) * 5.0 / 9.0 ; 1. Evaluate right-hand side a. Find current value of fahrenheit 72.0 b. Subtract 32.0 40.0 b. Multiply by 5.0 200.0 c. Divide by 9.0 22.2 2. Assign 22.2 to be the new value of celsius (the old value of celsius is lost.) C-24 C-4

Fahrenheit to Celsius (III) printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = fahrenheit - 32.0 ; celsius = celsius * 5.0 / 9.0 ; printf("that equals %f degrees Celsius.", celsius); C-25 Does Terminology Matter Lots of new terminology today! "variable", "reserved word", "initialization", "declaration", statement, "assignment", etc., etc. You can write a complicated program without using these words But you can t talk about your programs without them! Learn the exact terminology as you go, and get in the habit of using it. C-26 Next Lecture: Expressions Each lecture builds on the previous ones, so... be sure you re solid with this material before going on! C-27 C-5