CS100A, Fall 1998 This lecture covers the declaration of variables, assignment, and the if statement. While discussing the declaration of variables,

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Chapter 4 - Control Statements
Python Programming Language
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
True or false A variable of type char can hold the value 301. ( F )
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
IAT 800 Foundations of Computational Art and Design Lecture 2.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
String Escape Sequences
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CPS120: Introduction to Computer Science
1 CS1110 Fall 2011 David Gries, Steve Marschner Reading for this lecture and previous lecture: Sections 1.1, 1.2, 1.3. Lab 1 will give you practice with.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CS100A, Fall Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 After discussing the notion of an algorithm and (computer) program briefly, the first lecture.
CPS120: Introduction to Computer Science Variables and Constants.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Fundamentals 2.
Relational Operator and Operations
Chapter 10 Programming Fundamentals with JavaScript
The Machine Model Memory
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 6 JavaScript: Introduction to Scripting
Working with Java.
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
Debugging and Random Numbers
Variables and Primative Types
ECE Application Programming
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Identifiers - symbolic names
Chapter 10 Programming Fundamentals with JavaScript
Unit-1 Introduction to Java
Chapter 1: Computer Systems
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
CMSC 202 Java Primer 2.
Chapter 2: Java Fundamentals
Coding Concepts (Data- Types)
Control Structures Part 2
Python Programming Language
Concepts for this lecture: Class Instance of a class, (an object)
Chapter 2: Java Fundamentals
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
CS2011 Introduction to Programming I Selections (I)
Introduction to Primitives
Unit 3: Variables in Java
IAT 800 Foundations of Computational Art and Design
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Variables and Constants
The boolean type and boolean operators
Presentation transcript:

CS100A, Fall 1998 This lecture covers the declaration of variables, assignment, and the if statement. While discussing the declaration of variables, it will be important to mention that variables have meaning and that writing down that meaning at the point of declaration is extremely important for developing correct programs and for understanding programs. At this point in the course, the only types the students have seen is int, float, long, and the like; they don’t know what a class is. We will mention the modifier final, which is used for constants. The debugger will be used in the lecture to demonstrate execution of the assignment. For example, after slide 5 is discussed, we’ll illustrate execution of that program within the debugger, showing carefully how variable timeOfDay changes during execution. The debugger will be used to illustrate assignment, declaration of a final variable (a constant), and the if statement. CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

CS100A, Fall 1998 Concepts for this lecture: Variables and constants Declaration of a variable Assignment to variable Declaration of a constant The conditional (if) statement Reading: in Holmes: Sec. 1.4 (p. 19..); Sec.1.5 (p. 21..); Sec. 3.1 (p. 80..). CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

Variable: a box into which a value can be placed. Examples of declarations int x; int timeOfDay; x -49 TimeOfDay 1115 CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

Variable names (identifiers). Begin with a letter, _, or $ Contain letters, digits, _ and $ Generally, we don’t use _ or $ Convention for variable names: first letter small; capitalize words within the identifier. timeOfDay f12 f0 restOfInput CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

New statement (or command): Assignment to a variable. int timeOfDay = 3; // timeOfDay timeOfDay= 1200; timeOfDay= 3 + timeOfDay + 2; 3 1200 1205 CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

<variable> = <expression> ; Form of assignment: <variable> = <expression> ; Here, = does not mean equality! In Java, Read x= y; as “Assign y to x” or “x becomes y” Read “x==y as “x equals y” x==y (yields true if x and y contain the same value and false otherwise) CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

Algol 60 used := for assignment, = for equality = introduced as a sign for equality by Robert Recorde (1510-1558). He chose = because nothing could be more equal than two parallel lines. = for equality has been a worldwide convention for over 100 years. The use of = for assignment was started in FORTRAN in the early 1950s. C (about 1970) and then C++ (1980s) continued to use = for assignment and introduced == for equality; thus bringing great confusion. This use of = for assignment and requirement of == for equality has caused more confusion and waste of time in the whole world than any other notational convention. Algol 60 used := for assignment, = for equality CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

Short names make program shorter, perhaps more manageable. Long names can be used to convey something about the meaning of a variable. However, a name can never (almost) give the full, precise meaning, so it is best to give a comment at the declaration of a variable to give its full meaning. You’ll see many examples of this as the course progresses. CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

The conditional statement (if statement). Allows a choice of command based on some condition. // Store the maximum of x,y in z z= x; if (y>x) z:= y; syntax: if ( <boolean expression> ) statement CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

Using a block statement: // if x != y, store 0 in x and store y in z if (y!=x) {x= 0; z= y;} or if (y!=x) { x= 0; z= y; } CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

The block statement {<statement 0> <statement 1> … } { and } are used to delimit a sequence of statements that are to act together as a single statement, in the same way that parentheses are used to aggregate in arithmetic expressions. CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.

Second form of if statement // store maximum of x and y in z if (x >=y) {z= x;} else {z= y;} The rest of the lecture will demonstrate the use of the assignment and if statements in Codewarrior on the Macintosh. CS100A, Fall 1998. Lec. 2, Thur., 3 Sept.