CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Introduction to Python
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Chapter 2 Variables.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables, Expressions, and IO
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to C Programming
Presentation transcript:

CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Variables  A variable is a single datum or an accumulation of data attached to a name  The datum is (or data are) stored in memory  The name is mostly arbitrary but should be chosen wisely Variables can have almost any name Names should improve the readability of the code (c) 2012 Ophir Frieder et al

Variables in Ruby  Use the format variable_name = value  This format also initializes variable data irb(main):001:0> a = 4 => 4 irb(main):002:0> b = 3 => 3  The equal sign (=) assigns the right-hand side to the variables in the left hand side (c) 2012 Ophir Frieder et al

Common Standards for Variable Names  Cannot start with an integer  Ex: bank1, not 1bank  Should avoid having special characters  Ex: money_spent, not $_spent  Special characters have specific uses in many languages, including Ruby  Should explain the data they stand for  Ex: balance, not b  Should complement the programming language style  Ex: check_balance, not checkBalance or checkbalance Names with underscores match Ruby’s style Last two names are different because names are case sensitive (c) 2012 Ophir Frieder et al

Variables  Most programming languages assign the variable’s data to an address in memory  The programmer does not need to decide the location Memory – Figure 3.2 variable_2 variable_1 (c) 2012 Ophir Frieder et al

Variables  Constants are “variables” that are assigned a value that “cannot” be changed  Constant names contain only capital letters  Ex: PI or PAI for (  ; C for speed of light constant (c) 2012 Ophir Frieder et al

Data Classes  Variables can represent words, numbers, and other entities depending on their data classes  A data class indicates the properties of the data stored in a variable  The nomenclature “Data Type” is used in non-object oriented languages  The notion of “Class” has far more reaching meaning than “Type” (c) 2012 Ophir Frieder et al

Data Classes  Specify the domain of valid values of variables  Determine the amount of memory allocated  Determine the operations allowed for (or on) it  Integers (Fixnum and Bignum)  Floats  Strings  Boolean  Many more… Stay tuned Data classes can:Data classes in Ruby include: (c) 2012 Ophir Frieder et al

Data Classes in Ruby: Fixnum  Natural numbers in integer range and their negatives  Integer values range from -2,147,483,648 to 2,147,483,647 in a 32-bit system  Standard in almost all languages  Note: asymmetry between the positive and negative numbers (c) 2012 Ophir Frieder et al

Data Classes in Ruby: Integers  Stores values within the 32-bit range irb(main):01:0> x = 5 => 5  Stores values outside the 32-bit range irb(main):02:0> x=1_000_000_000_000_ 000 =>  Note: No use of commas with the numbers FixnumBignum (c) 2012 Ophir Frieder et al

Data Classes in Ruby: Float  A decimal number that includes positive and negative values  Can be defined using decimal places or scientific notation  3.5e2 indicates 3.5 x 10 2 in scientific notation Float Examples: irb(main):001:0> x = 5.0 => 5.0 irb(main):002:0> x = => irb(main):003:0> x = 3.5e2 => (c) 2012 Ophir Frieder et al

Data Classes in Ruby: Strings  Character sequence surrounded by quotes  Both double (“) and single (‘) quotes can be used, but double quotes must be used if a single quote is inside a string irb(main):001:0> x = ‘hello world’ => hello world irb(main):002:0> y = “hello,‘world’” => hello ‘world’ (c) 2012 Ophir Frieder et al

Basic Arithmetic Operators  Used to perform mathematical operations  Most are binary operators and require two operands SymbolOperation +Addition -Subtraction *Multiplication /Division %Modulus **Power Table 3.1 (c) 2012 Ophir Frieder et al

Basic Arithmetic Operators  The modulus operator, %, is used to find the remainder when diving two integers irb(main):001:0> x = 5%2 => 1 (c) 2012 Ophir Frieder et al

Advanced Mathematical Functions  Ruby’s Math Module provides advanced mathematical functions, referred to as Methods (Table 3.2)  Math Module methods are used in the following format: Math.Function_name(Value) irb(main):001:0> x = Math.sqrt(16) => 4 MethodOperation sqrt()Square Root sin()Sine cos()Cosine tan()Tangent log()Natural Log (ln) log10()Log (Base 10) (c) 2012 Ophir Frieder et al

Use of Methods  Ruby’s Math Module provides advanced mathematical functions, referred to as Methods  There is a way to include a whole module (like Math), without the need to specify it with every use (c) 2012 Ophir Frieder et al

Input & Output: Direct Output  The puts instruction displays text on the screen (i.e., standard out) irb(main):001:0> puts “Hello World”  Variables are displayed on the screen using puts  To use puts for a variable, enter the variable name without quotations irb(main):002:0> text = “Hello World” => “Hello World” irb(main):003:0> puts text => Hello World (c) 2012 Ophir Frieder et al

Input & Output: Input Using Variables  The gets instruction stores values that are entered from the keyboard (i.e., standard input device)  Its format is very similar to puts irb(main):001:0> age_input = gets  gets stops the program and waits for the user to type  Type the input, then press enter (c) 2012 Ophir Frieder et al

Input & Output: Input Using Variables  gets will store values as character strings  To change the data from one class to another (i.e., a string into an integer), you need to explicitly perform a type (class) conversion, usually creating a new variable of the appropriate class (c) 2012 Ophir Frieder et al

Input & Output: Conversion  gets will store character strings irb(main):001:0> age_input = gets  If you typed 19, age_input will be the string “19”, NOT the number 19  To convert “19” to 19, perform the following: irb(main):002:0> age = age_input.to_i.to_i converts the contents of a variable to an integer (c) 2012 Ophir Frieder et al

Common Programming Errors  Syntax errors refer to code that Ruby cannot execute irb(main):001:0> x = 1 + “hello” Type Error: String can’t be coerced into Fixnum from (irb):1:in ‘+‘ from (irb):1  Ruby stops execution and tells the location where it had to stop (c) 2012 Ophir Frieder et al

Common Programming Errors  Error messages can seem unrelated to the problem irb(main):002:0> x = hello NameError: undefined local variable or method ‘hello’ for main: Object from (irb):2  Ruby assumed that hello was a variable since strings have quotes (c) 2012 Ophir Frieder et al

Common Programming Errors  Ruby cannot catch logic errors  The program runs, but the results are incorrect  Logic errors are often harder to find because the error’s location is not given  A common logic error involves integer division  Ruby performs integer division correctly, but many casual users expect a different result irb(main):003:0> 5/2 => 2 A result of 2.5 may be expected, but it would not be an integer (c) 2012 Ophir Frieder et al

Mixing Data Classes  Ruby always tries to keep the same data class for all of its operands  Ruby will convert data classes when it has different ones in the same arithmetic operation  To get a decimal from the previous example, add a float or perform an explicit conversion irb(main):003:0> 1.0*5/2 => 2.5  However, some data classes cannot be converted  Ruby will either create an error condition, or worse, produce an incorrect result irb(main):002:0> x = “hello”.to_i => 0 NOTE possible version dependency!!! (c) 2012 Ophir Frieder et al

Summary  A variable is data attached to a name  There are common guidelines to follow when creating variable names  Constants are “variables” (really values) that never change  Programs use various methods (operators and functions) available in each of the data classes to perform operations  Ruby has many classes of operators and methods to perform math and other operations (c) 2012 Ophir Frieder et al

Summary  The puts command is used to generate output on the screen (i.e., standard out)  The gets command is used to obtain information from the keyboard (i.e., standard in) putsgets Three types of programming errors are syntax errors, logic errors, and type errors (c) 2012 Ophir Frieder et al