# CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Size: px
Start display at page:

Download "CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style"

Transcription

1 CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1

3 Example 2 - a slight problem with division public class Example2 int sum, average; int exam1, exam2; exam1 = 75; exam2 = 90; sum = exam1 + exam2; average = sum/2; // When program runs, average is 82, not 82.5 exam1 + exam2 is int + int which is an int The assignment to sum is int = int, which is okay. The division is int/int which is an int result is truncated!! The assignment is int = int, which is okay. Result: 82 3

4 Example 3 - change average to double public class Example3 int sum; double average; int exam1, exam2; exam1 = 75; exam2 = 90; sum = exam1 + exam2; average = sum/2; // When program runs, average is 82.0, not exam1 + exam2 is int + int which is an int The assignment to sum is int = int, which is okay. The division is int/int which is an int result is truncated!! The assignment is double = int, which is okay. double...which is why we get 82.0 and not 82. The int is automatically converted to a 4

5 Example 4 fix #1: change sum to double public class Example4 double sum; double average; int exam1, exam2; exam1 = 75; exam2 = 90; sum = exam1 + exam2; average = sum/2; // average is 82.5 when program runs exam1 + exam2 is int + int which is an int The assignment to sum is double = int, which is okay. sum now holds not 165, but rather, The division is double/int which is a double finally, result is not truncated!! The assignment is double = double, which is okay. The result:

6 Example 5 fix #2: change int literal to double literal public class Example5 int sum; double average; int exam1, exam2; exam1 = 75; exam2 = 90; sum = exam1 + exam2; average = sum/2.0; // average is 82.5 when program runs exam1 + exam2 is int + int which is an int The assignment to sum is int = int, which is okay. The division is int/double which is a double again, result is not truncated!! The assignment is double = double, which is okay. The result: 82.5 The rule is that, if both operands of an addition, subtraction, multiplication, or division, are of type int, then the result will be of type int and as we have seen, for division, that means that the fractional portion is truncated. However, if one or both of the operands is a double instead of an int, then the result will be a double (and thus division would not be truncated). 6

7 Recall our HelloWorld program: Outputting text using System.out.println public class HelloWorld System.out.println("Hello World!"); The code "Hello World!" in the code above is a literal of type String the first nonprimitive type we have encountered. We won t talk about variables of type String for a while yet, but we will use literals of type String. Any text that appears in between double quotes is a literal of type String. The general form is System.out.println(expr); where expr is some expression. The expression is evaluated, and the result is then printed to the screen. If you have only double-quoted text inside the parenthesis, then since that is a String literal, the expression in parenthesis is of type String. Output of variables In the previous example, the item we were sending to the screen was literal text. However, it is also possible to print out the values of our variables. public class Example6 int exam1, exam2, sum; exam1 = 60; exam2 = 70; sum = exam1 + exam2; System.out.println(sum); In the code above, the expression in parenthesis is of type int. The use of System.out.println activates some pre-written code already present in the virtual machine. There are many di erent pieces of pre-written code one to print a String, one to print an int, one to print a double, and so on. The compiler picks the right one based on the type of the expression in the parenthesis. 7

8 String concatenation Concatenation is the merging of two Strings into one by placing them one after the other. The type of the expression is String + String --> String One example: "Levia" + "than" evaluates to the String value "Leviathan" If one of the operands is a variable of a primitive type instead of a String, that operand s value is automatically converted to a String, just as in our earlier type expression examples, the value of an int variable got automatically converted to a double value if we used an assignment statement to write an int value into a double variable. For example, in the expression: System.out.println("Sum is: " + 20); we have a String + int operation, and that means that the + is a concatentation, and that means the value of the int should be converted to a String automatically and then the concatenation done. "Sum is: " > "Sum is: " + "20" -> "Sum is: 20" So, again, the meaning of an operator depends on the type of the operand(s). The operator + in an int + int expression has a di erent meaning than the operator + in a String + int expression. An example using concatenation public class Example7 int exam1, exam2, sum; exam1 = 60; exam2 = 70; sum = exam1 + exam2; System.out.println(exam1); System.out.println(exam2); System.out.println(); System.out.println("Sum is " + sum); When there is nothing at all in the parenthesis, all that is printed is a blank line. In the last line, the type of the expression in parenthesis is String, and that expression evaluates to the String value Sum is 130 (which is the String value that gets printed to the screen). 8

9 A bit more messing with types Consider the following code: public class Example8 int v1 = 5; int v2 = 7; System.out.println("Sum is " + v1 + v2); System.out.println(v1 + v2 + " is the sum"); System.out.println("Sum is " + (v1 + v2)); The + operator is read left-to-right, so in the first two output statements above, the leftmost + is done first. In the third output statement, the parenthesis take higher precedence and so the rightmost + is done first. Most operators are read left-to-right. A few, such as assignment, are read right-to-left, as with the statement a = b = c = d = 2;, where d is assigned the value 2 first, then c is assigned the value of d, and so on. A few other operators can t be used multiple times in a row to begin with. But most operators are read left-to-right. So, the first output line: String + int + int "Sum is " \./ String + int "Sum is 5" \./ String "Sum is 57" And the second output line: int + int + String " is the sum" \./ int + String 12 + " is the sum" \./ String "12 is the sum" 9

10 And the third output line: String + (int + int) "Sum is " + (5 + 7) \./ String + int "Sum is " \./ String "Sum is 12" One last note... Everything that is true for System.out.println(...); is also true for System.out.print(...); except that the latter doesn t start a new line after the printing, and the former does. System.out.println("Hello, "); System.out.println("world!"); will print: Hello, world! ^ and the next thing to be printed goes where the ^ is. (The ^ is not actually printed, it s just an indicator in our two examples on this page.) System.out.print("Hello, "); System.out.print("world!"); will print: Hello, world!^ and the next thing to be printed goes where the ^ is. This means that the statement System.out.print(); is meaningless. With System.out.println(), there is at least a new line started even if no other text is printed. But if you don t start a new line and you don t print anything, then you did no work whatsoever. So there s no reason to have a System.out.print(); command. 10

14 Better, but still not ideal: public class HelloWorld System.out.println("Hello World!"); The second thing to realize is that, likewise, it will be easier to read the code inside main if that code is indented three spaces from the curly braces that begin and end main. Again, the more code you have, the more important this becomes. Correct: public class HelloWorld System.out.println("Hello World!"); Now, it is very easy to visually pick out with one glance where the class begins and ends, what code is inside it, and where main begins and ends, and what code is inside it. A slight variation on this is that some people like to put the open brace on the same line as the Java syntax tool it is starting: Also correct: public class HelloWorld System.out.println("Hello World!"); Since the word public lines up with the closing brace, you still have the same kind of visual structure. It s a bit harder to see it, since the code is crushed together a bit more, but on the other hand, you can fit more code in the viewing window at once. Some people think this tradeo is worth it and others (myself among them) don t. You can make your own choice. That is what was meant by indentation standards there are slightly di erent ways of doing things. To give another example, some people indent three spaces, some prefer five. You should indent at least three but you don t want to indent so many spaces that your code runs o the end of the page all the time. You can use whichever variation you prefer just be consistent and keep your line lengths at about 80 characters or so. 14

16 One syntax for commenting Most of the time, the commenting syntax you will want to use is the use of the double slash : //. You can place that two-character symbol anywhere in your code, and anything from that point to the end of the line of text will be ignored by the compiler. Example: // Class: HelloWorld // - in control of greeting generations // - Written August 1999 by Jason Zych public class HelloWorld // Method: main // - controls main execution of program // line prints out a greeting System.out.println("Hello World!"); Another commenting syntax If you want to quickly comment out a large chunk of text or code, you can also surround the area as follows: /* <---- slash-asterisk to open comment whatever you want goes in here */ <---- asterisk-slash to close comment 16

17 Example 2: /* Class: HelloWorld - in control of greeting generations - Written August 1999 by Jason Zych */ public class HelloWorld System.out.println("Hello World!"); Example mixing the two kinds of comments: /* Class: HelloWorld - in control of greeting generations - Written August 1999 by Jason Zych */ public class HelloWorld // Method: main // - controls main execution of program // line prints out a greeting System.out.println("Hello World!"); 17

### CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

CS125 : Introduction to Computer Science Lecture Notes #11 Procedural Composition and Abstraction c 2005, 2004 Jason Zych 1 Lecture 11 : Procedural Composition and Abstraction Solving a problem...with

### First Java Program - Output to the Screen

First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

### Full file at

Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

### Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

### A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form

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

Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

### CS 177 Recitation. Week 1 Intro to Java

CS 177 Recitation Week 1 Intro to Java Questions? Computers Computers can do really complex stuff. How? By manipulating data according to lists of instructions. Fundamentally, this is all that a computer

### COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

### Intro. Scheme Basics. scm> 5 5. scm>

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

### CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday

### IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

### T H E I N T E R A C T I V E S H E L L

3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before

### COMP 110 Project 1 Programming Project Warm-Up Exercise

COMP 110 Project 1 Programming Project Warm-Up Exercise Creating Java Source Files Over the semester, several text editors will be suggested for students to try out. Initially, I suggest you use JGrasp,

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

CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

### Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Get JAVA To compile programs you need the JDK (Java Development Kit). To RUN programs you need the JRE (Java Runtime Environment). This download will get BOTH of them, so that you will be able to both

### CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want

### CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

CS125 : Introduction to Computer Science Lecture Notes #38 and #39 Quicksort c 2005, 2003, 2002, 2000 Jason Zych 1 Lectures 38 and 39 : Quicksort Quicksort is the best sorting algorithm known which is

### The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

### BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

### These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

### Lecture 4 CSE July 1992

Lecture 4 CSE 110 6 July 1992 1 More Operators C has many operators. Some of them, like +, are binary, which means that they require two operands, as in 4 + 5. Others are unary, which means they require

### Table of Laplace Transforms

Table of Laplace Transforms 1 1 2 3 4, p > -1 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Heaviside Function 27 28. Dirac Delta Function 29 30. 31 32. 1 33 34. 35 36. 37 Laplace Transforms

### List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs are divided into classes 7 Class: public class 8 Class:

### CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work

### COMP 202 Java in one week

CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

### 12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

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

Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

### CS 11 java track: lecture 1

CS 11 java track: lecture 1 Administrivia need a CS cluster account http://www.cs.caltech.edu/ cgi-bin/sysadmin/account_request.cgi need to know UNIX www.its.caltech.edu/its/facilities/labsclusters/ unix/unixtutorial.shtml

### COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016 Learn about cutting-edge research over lunch with cool profs January 18-22, 2015 11:30

### Program Fundamentals

Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

### Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

### The compiler is spewing error messages.

Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

### Formatting & Style Examples

Formatting & Style Examples The code in the box on the right side is a program that shows an example of the desired formatting that is expected in this class. The boxes on the left side show variations

### CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

CS 1313 010: Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June 20 2007 This second assignment will introduce you to designing, developing, testing

### What did we talk about last time? Examples switch statements

Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation

### 1007 Imperative Programming Part II

Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #17 Loops: Break Statement (Refer Slide Time: 00:07) In this session we will see one more feature that is present

### CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Assignment 1 Assignment 1 posted on WebCt and course website. It is due September 22nd

### CS11 Java. Fall Lecture 1

CS11 Java Fall 2006-2007 Lecture 1 Welcome! 8 Lectures Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7-8 Lab Assignments Made available on Mondays Due one week later Monday, 12 noon

### Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

### TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

### Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

### CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

### CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG 1 Notice Class Website http://www.cs.umb.edu/~jane/cs114/ Reading Assignment Chapter 1: Introduction to Java Programming

### Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

### Getting started with Java

Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

### COMP 202 Java in one week

COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

### 4. Java Project Design, Input Methods

4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

### CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

CS 1313 010: Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September 19 2018 This second assignment will introduce you to designing, developing, testing

### Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

### Divisibility Rules and Their Explanations

Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although

### 17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs are divided into classes 7 Class: public class 8 Class:

### Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

### CPS122 Lecture: From Python to Java

Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

### CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

### Primitive Data, Variables, and Expressions; Simple Conditional Execution

Unit 2, Part 1 Primitive Data, Variables, and Expressions; Simple Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Overview of the Programming Process Analysis/Specification

### Section 1. The essence of COBOL programming. Mike Murach & Associates

Chapter 1 Introduction to COBOL programming 1 Section 1 The essence of COBOL programming The best way to learn COBOL programming is to start doing it, and that s the approach the chapters in this section

### Math 25 and Maple 3 + 4;

Math 25 and Maple This is a brief document describing how Maple can help you avoid some of the more tedious tasks involved in your Math 25 homework. It is by no means a comprehensive introduction to using

### 5. Control Statements

5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

### Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking

### How to approach a computational problem

How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to

### COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few

### Chapter 3. Selections

Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

### Mobile App:IT. Methods & Classes

Mobile App:IT Methods & Classes WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. -

### Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

### Lecture Set 2: Starting Java

Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

### CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming Lecture 3 Express Yourself ( 2.1) 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline 1. Data representation and types 2. Expressions 9/16/2011

### Lecture Set 2: Starting Java

Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

### Introduction to Java Applications; Input/Output and Operators

www.thestudycampus.com Introduction to Java Applications; Input/Output and Operators 2.1 Introduction 2.2 Your First Program in Java: Printing a Line of Text 2.3 Modifying Your First Java Program 2.4 Displaying

### Shorthand for values: variables

Chapter 2 Shorthand for values: variables 2.1 Defining a variable You ve typed a lot of expressions into the computer involving pictures, but every time you need a different picture, you ve needed to find

### Conditionals, Loops, and Style

Conditionals, Loops, and Style yes x > y? no max = x; max = y; http://xkcd.com/292/ Fundamentals of Computer Science Keith Vertanen Copyright 2013 Control flow thus far public class ArgsExample public

### Chapter 2. Editing And Compiling

Chapter 2. Editing And Compiling Now that the main concepts of programming have been explained, it's time to actually do some programming. In order for you to "edit" and "compile" a program, you'll need

### Excel Basics: Working with Spreadsheets

Excel Basics: Working with Spreadsheets E 890 / 1 Unravel the Mysteries of Cells, Rows, Ranges, Formulas and More Spreadsheets are all about numbers: they help us keep track of figures and make calculations.

### CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 115 Data Types and Arithmetic; Testing Taken from notes by Dr. Neil Moore Statements A statement is the smallest unit of code that can be executed on its own. So far we ve seen simple statements: Assignment:

### Python for Non-programmers

Python for Non-programmers A Gentle Introduction 1 Yann Tambouret Scientific Computing and Visualization Information Services & Technology Boston University 111 Cummington St. yannpaul@bu.edu Winter 2013

### Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller Table of Contents Introduction!... 1 Part 1: Entering Data!... 2 1.a: Typing!... 2 1.b: Editing

### Programming for Engineers Introduction to C

Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

### Basics of Java Programming

Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

### CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

CS125 : Introduction to Computer Science Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals c 2005, 2004, 2003, 2002, 2001, 2000 Jason Zych 1 Lecture 6 : Compound Statements, Scope,

### The C++ Language. Arizona State University 1

The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

### Java Bytecode (binary file)

Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

### CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

### Information Science 1

Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

### First-Order Translation Checklist

CS103 Winter 2019 First-Order Translation Checklist Cynthia Lee Keith Schwarz In this handout, we ve distilled five specific points that you should check in your first-order logic statements before submitting

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

CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

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

COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

### Lab # 2. For today s lab:

1 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot 1 For today s lab: Go the course webpage Follow the links to the lab notes for Lab 2. Save all the java programs you

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

Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

### MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

### Part II Composition of Functions

Part II Composition of Functions The big idea in this part of the book is deceptively simple. It s that we can take the value returned by one function and use it as an argument to another function. By

### Oct Decision Structures cont d

Oct. 29 - Decision Structures cont d Programming Style and the if Statement Even though an if statement usually spans more than one line, it is really one statement. For instance, the following if statements

### Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Conditionals, Loops, and Style Control flow thus far yes no x > y? max = x; max = y; public class ArgsExample time 0 String product = args[0]; time 1 int qty = Integer.parseInt(args[1]); time 2 double

### Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

### 3. Simple Types, Variables, and Constants

3. Simple Types, Variables, and Constants This section of the lectures will look at simple containers in which you can storing single values in the programming language C++. You might find it interesting

### Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Chapter 1 Getting Started Introduction To Java Most people are familiar with Java as a language for Internet applications We will study Java as a general purpose programming language The syntax of expressions

### COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

### Use of scanf. scanf("%d", &number);

Use of scanf We have now discussed how to print out formatted information to the screen, but this isn't nearly as useful unless we can read in information from the user. (This is one way we can make a