CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG
|
|
- Sabrina Little
- 5 years ago
- Views:
Transcription
1 CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1
2 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the class on Tuesday and Thursday) Review the class quiz Review the class slides, especially the review part I will mark the important knowledge in red in the class slides Requests for the weekly quiz Open book, but no electronic devices allowed The first two weekly quiz are considered as practice, no impacting for your finial grading Assignments The Class 8 Exercise for today is posted here. The homework 2 and posted it here They are due this coming Sunday, at 11:59 PM. 2
3 Review The for loop In programming, we often do the same thing many times To make this possible programming languages provide loops The simplest kind of loop is the for loop Suppose you wanted to print the first 5 perfect squares We need to repeat something 5 times, changing a value each time 3
4 The for loop A for statement has two major parts Three statements inside parentheses that control the loop A series of statements executed with each pass through the loop The series of statements are called the loop body... and are enclosed in curly braces, { } Here is the format for the for loop for (INITIALIZATION; CONTINUATION_TEST; UPDATE) { } STATEMENT;... 4
5 The for loop The code inside the parentheses controls the loop It consists of three separate statements on the same line separated by semi- colon ; 5
6 The for loop Declare the variable Assign it its first value It s done before entering the loop An expression that returns a boolean value The loop will continue if the continuation test is true Otherwise, the loop will stop The update changes the value after the loop statements have been executed before the continuation test i is called control variable or loop variable It s used to count the number of pass trough the loop It must be an integer The traditional names for such variable is i, j, or k 6
7 The for loop in Action Here is the flow chart for a for loop Give the control variable its starting value This happens only once Evaluate the test condition If the test is true: Execute all statements. Then, update the control variable If the test is false, jump out of the loop 7
8 Common Programming Error: Forgetting Curly Braces One of the most common Java programming errors is forgetting to use curly braces The curly braces mark the beginning and end of the loop body The loop body is the series of statements... that are executed with each pass through the loop It prints Hi! 10 times and Ho! once 8
9 for Loop Patterns The three control statements in a for loop can be used many ways. But some patterns are more common than others The most common for loop pattern is the following for (int LOOP_VARIABLE = 1; LOOP_VARIABLE <= n; LOOP_VARIABLE++) { } STATEMENT;... This loop starts with the control variable set to 1 and executes the statements contained in the loop body n times So the following code will produce this output: 9
10 for Loop Patterns Often, it is more convenient to start counting from 0 instead of one Here is the pattern for (int LOOP_VARIABLE = 0; LOOP_VARIABLE < n; LOOP_VARIABLE++) { } STATEMENT;... There are two differences between this pattern and the previous one The control variable is initialized to 0 instead of 1 And the continuation test uses < instead of <= 10
11 for Loop Patterns Usually the update statement increases the value of the loop variable But you can also run a for loop backwards Here the the pattern for a decrementing loop for (int LOOP_VARIABLE = n; LOOP_VARIABLE >= 1; LOOP_VARIABLE- - ) { STATEMENT;... } 11
12 Nested for Loops The for loop executes one or more statements But the for loop is, itself, a statement That means you can have one for loop inside another for loop This is called a nested loop Here's an example Each loop has its own, distinct, loop variable It s easier to read nested loops from the inside out The innermost loop runs five times to print a message The outmost loop runs the innermost loop tem times Notice the control variable for each loop must be different 12
13 Nested for Loops One more example The inner loop prints the value of the control variable as it goes from 1 to 3 The output loop executes the inner loop 6 times The output is 13
14 New Material Outline Scope The Scope of for Loop Variable Code Block Pseudocode Reusing Existing Code Class Constants 14
15 Scope As programs get longer you have to be careful... that different part of the code do not conflict with each other One way programming languages deal with this problem... is using a concept call scope The scope of a an identifier is the part of the program where it can be used Scope applies to Variables Methods When an variable or method is outside its scope that the compiler does not know about it so it is invisible 15
16 Scope of Static Method The scope of a static method... is the entire class in which it appears So any static method can call any other static method... no matter which comes first... if they are in the same class They can also call static methods in other classes... but then they have to give the class name 16
17 Scope of Static Method In the following code, scope of the static method sayhello... is all the sections of the program shown in red 17
18 Scope of Variable The scope of variables is different In general, the scope of a variable starts where it is declared... and ends which the closing curly brace that encloses it The way to identify the scope of a variable is: find the pair of curly braces that directly encloses the variable declaration the scope is from the point where it s declared to the closing curly brace 18
19 Scope of Variable So in the in the code above the scope of the variable message is shown in light blue... The same variable name can be used in different methods without the compiler becoming confused A variable declared inside a method is called local variables It s a good programing practice to declare variables in the smallest scope possible. This is called localizing variables 19
20 Outline Scope The Scope of for Loop Variable Code Block Pseudocode Reusing Existing Code Class Constants 20
21 The Scope of for Loop Variable The general scope rule for variables does not work for loop variables The scope of a loop variable is The part of the code between the for loop curly braces And the part of the code between the parentheses that starts the loop So the scope of the loop variable i is 21
22 The Scope of for Loop Variable You will get a syntax error if the code is written as follows: 22
23 Outline Scope The Scope of for Loop Variable Code Block Pseudocode Reusing Existing Code Class Constants 23
24 Code Block The part of a Java program between an open curly brace, {... and the matching closing curly brace, }... is called a code block A code block is section of code that is surrounded... by a matched pair of curly braces, { } We can use the idea of a code block to describe the scope of a variable The scope of a variable starts with the line where it is declared... and continues to the end of the code block in which it is declared 24
25 Outline Scope The Scope of for Loop Variable Code Block Pseudocode Reusing Existing Code Class Constants 25
26 Pseudocode When you write more complex algorithms, it s hard to write the entire algorithm immediately One way to deal with this is to use pseudocode When we write pseudocode, we write steps to solve a problem in simple English Then we break down these steps into simpler steps We keep doing this until we have an English description of the steps that can then be translated into Java code 26
27 Pseudocode Suppose we wanted to write a program to print following triangle Our first crack at the pseudocode might look like this draw a downward pointing triangle of 5 lines using asterisks But if you think for a moment, you will realize that this is not enough The following figure is a downward pointing triangle but is not what we want 27
28 Pseudocode So let's refine our pseudocode: draw a downward facing triangle of 5 lines using asterisks with no space between them But the following triangle would also satisfy this description So let's try again draw a downward facing triangle of 5 lines using asterisks with no space between them and the downward sides are of equal length Up to now, we have just refined our description of what needs to be done This is an important first step 28
29 Pseudocode But now we have to start thinking about how we will describe this to the computer using actions it can perform It seems clear we will need a for loop, so let's start with that But each line is different Each line must print a different number of spaces before it prints the asterisks So we need to increase the number of spaces in each line starting with 0 So let's refine our pseudocode 29
30 Pseudocode But once we have the right number of spaces and asterisks... we need to go to the next line So let's try again The next step is to calculate the number of asterisks and spaces... needed for each line If we number the lines 1 through 5... we want the following values for each line 30
31 Pseudocode There is a simple algebraic relationship... between the line number... and the number of spaces and asterisks The number of spaces is equal to the line number minus 1 The number of asterisks is trickier Notice that the number of asterisks decreases by two with each line But what about the first line? If we add 2 to 9 we get 11 So if we start with and subtract 2 at each line... we get the right number of asterisks for each line 31
32 Pseudocode Let's add this to the for loop in our pseudocode The lines to write asterisks and spaces each need a for loop 32
33 Pseudocode Translating this into Java, we get When we run this code we get 33
34 Outline Scope The Scope of for Loop Variable Code Block Pseudocode Reusing Existing Code Class Constants 34
35 Reusing Existing Code Sometimes the easiest way to write a program... is to modify some existing code Let's say you want to write a program to draw the following We might be able to use DrawDownwardTriangle.java instead If you look carefully, you will see that the 1st line of DrawDownwardTriangle... is the same as the 5th line of the figure we want... And so on 35
36 Reusing Existing Code So all we have to do to get the figure we want is change the direction of counting in the outer for loop of DrawDownwardTriangle In DrawDownwardTriangle.java the outer loop counted from 1 to 5 For this code, we want the outer loop to count from 5 to 1 Here is how we do it 36
37 Outline Scope The Scope of for Loop Variable Code Block Pseudocode Reusing Existing Code Class Constants 37
38 Class Constants For the code in DrawCone.java for ( int i = 1; i <= (11 - (2 * line_number)); i++) Someone reading the code for the first time might ask "Why 11?" Programmers call values like this magic numbers They make the program work but it is not clear how Magic numbers make for bad code Whenever we can, we should replace such numbers with an expression... and those expressions should use constants A constant is like a variable except its value cannot be changed 38
39 Class Constants When creating a constant, you must use the keyword final... using the following format final DATA_TYPE CONSTANT_NAME = VALUE; like this final inthours_in_a_day = 24; By convention, constant names are spelled using ALL_CAPITALS The best place to put a constant is inside the class but outside all the methods. Such constants are called class constants. That way they are available to all methods in the class. The format is like this: public static final DATA_TYPE CONSTANT_NAME = VALUE; 39
40 Class Constants So our new version of DrawCone will look like this Add the class constant NUMBER_OF_LINES Change from 5 to NUMBER_OF_LINES Add the variable stars and change the magic number 11 to the expression using the new constant Use the new variable stars in continuation test 40
41 Class 8 Quiz 1. What do you call the series of statements that are executed with each pass through the loop? the loop body 2. What is a code block? all the statements between an open curly brace and the corresponding closing curly brace 3. What is scope? the part of a code in which an identifier (name) has meaning 4. What is the scope of a method? the entire class in which it is defined 41
42 Class 8 Quiz 5. What is the scope of a variable that is not a loop variable? from where it is defined to the end of the code block in which it is defined 6. What is the scope of a loop variable? the parentheses after the keyword for as well as the loop body 7. What is a constant? a "variable" whose value never changes 8. What is the scope of a constant that is not a class constant? the same as a variable. From where it is defined to the end of the code block in which it is defined 42
43 Class 8 Quiz 9. What is the scope of a class constant? The entire class in which it is declared 10. Write the statement that declares the integer constant DAYS_IN_WEEK. public static final int DAYS_IN_WEEK = 7; 43
44 Weekly Quiz1 1. What must the name of the source code file for the Java class CountToTenbe? CountToTen.java 2. If you want to run a Java program from the command line, what must the source code file contain inside the class definition? a main method 3. What is the first line of a method called? method header 4. What is a method? a group of statements that performs a specific task 44
45 Weekly Quiz1 5. Write the Java statement that prints "Hello world!"? System.out.println("Hello world!"); 6. In the previous statement, what does Java call "Hello world!"? a string literal 7. Write the escape sequence for the Tab character? \t 8. Can you use a Java keyword as an identifier? no 45
46 Weekly Quiz1 9. Is 1st_try a valid Java identifier? no 10. Write a one line comment that contains the text "This is a one line comment" // This is a one line comment 46
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 9 Exercise
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise
More informationCS/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
More informationInformation 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
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments No new homework this week. Please make up the homework 1 5 & class exercises this week.
More informationDecision Making in C
Decision Making in C Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed
More informationInformation Science 1
Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting
More informationFlow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationDefinite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting
Unit 2, Part 2 Definite Loops Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Let's say that we're using a variable i to count the number of times that
More informationSTUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode
STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level
More informationBuilding Java Programs Chapter 2
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set of data values. Constrains the operations that can
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationJava 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
More informationBuilding Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.
Building Java Programs Chapter 2 bug Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. 2 An Insect Software Flaw 3 4 Bug, Kentucky Bug Eyed 5 Cheesy Movie 6 Punch Buggy
More informationBuilding Java Programs Chapter 2
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. bug 2 An Insect 3 Software Flaw 4 Bug, Kentucky 5 Bug Eyed 6 Cheesy Movie 7 Punch Buggy
More informationBuilding Java Programs
Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007. They may not be rehosted, sold, or modified without expressed
More informationInformation Science 1
Topics covered Information Science 1 Fundamental Programming Constructs (1) Week 11 Terms and concepts from Week 10 Flow of control and conditional statements Selection structures if statement switch statement
More informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationCS/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
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationProgramming Lecture 4
Five-Minute Review 1. What are classes and objects? What is a class hierarchy? 2. What is an expression? A term? 3. What is a variable declaration? 4. What is an assignment? What is precedence? 5. What
More informationIntroduction. C provides two styles of flow control:
Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if
More informationSlide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto
CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style
More informationInformation Science 1
Information Science 1 Fundamental Programming Constructs (1) Week 11 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 10 l Flow of control
More informationCS110D: PROGRAMMING LANGUAGE I
CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More information5. 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
More informationChapter 17. Fundamental Concepts Expressed in JavaScript
Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program
More informationOperators. Java operators are classified into three categories:
Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.
More informationRecap: Assignment as an Operator CS 112 Introduction to Programming
Recap: Assignment as an Operator CS 112 Introduction to Programming q You can consider assignment as an operator, with a (Spring 2012) lower precedence than the arithmetic operators First the expression
More informationFull 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
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationCourse Outline. Introduction to java
Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.
More informationCpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting
CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationProgramming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs
Programming Logic and Design Chapter 2 Elements of High-Quality Programs Objectives In this chapter, you will learn about: Declaring and using variables and constants Assigning values to variables [assignment
More informationCS 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
More informationProgramming Lecture 4
Five-Minute Review 1. What is a class hierarchy? 2. Which graphical coordinate system is used by Java (and most other languages)? 3. Why is a collage a good methapher for GObjects? 4. What is a CFG? What
More informationThe for Loop. Lesson 11
The for Loop Lesson 11 Have you ever played Tetris? You know that the game never truly ends. Blocks continue to fall one at a time, increasing in speed as you go up in levels, until the game breaks from
More information12/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
More informationIT 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
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming (Spring 2012) Lecture #7: Variable Scope, Constants, and Loops Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112
More informationExpressions 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
More informationExpressions 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
More informationThe simplest way to evaluate the expression is simply to start at the left and work your way across, keeping track of the total as you go:
ck 12 Chapter 1 Order of Operations Learning Objectives Evaluate algebraic expressions with grouping symbols. Evaluate algebraic expressions with fraction bars. Evaluate algebraic expressions with a graphing
More informationMore Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4
More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationIntroduction to Software Development (ISD) Week 3
Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process
More informationRepetition with for loops
Repetition with for loops So far, when we wanted to perform a task multiple times, we have written redundant code: System.out.println( Building Java Programs ); // print 5 blank lines System.out.println(
More informationCSc Introduction to Computing
CSc 10200 Introduction to Computing Lecture 2 Edgardo Molina Fall 2011 - City College of New York Thursday, September 1, 2011 Introduction to C++ Modular program: A program consisting of interrelated segments
More informationLab # 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
More informationCSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this
More informationCS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon
CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon Groups of up to three students may submit common solutions for each problem in this homework and in all future homeworks You are responsible
More informationC: How to Program. Week /Mar/05
1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers
More informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions
More informationBASIC 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
More information1. The programming language C is more than 30 years old. True or False? (Circle your choice.)
Name: Section: Grade: Answer these questions while viewing the assigned videos. Not sure of an answer? Ask your instructor to explain at the beginning of the next class session. You can then fill in your
More informationTOPIC 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
More informationFollowing is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence
More informationLecture 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
More informationComputer Science II Lecture 1 Introduction and Background
Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationObject Oriented Software Design
Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 30, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction
More informationQUIZ Friends class Y;
QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They
More informationComputer Programming : C++
The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program
More informationCSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education
CSE 142 Lecture 1 Course Introduction; Basic Java Welcome Today: Course mechanics A little about computer science & engineering (CSE) And how this course relates Java programs that print text 2 Handouts
More informationCHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++
CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,
More information3 The L oop Control Structure
3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement
More informationQuiz 1: Functions and Procedures
Quiz 1: Functions and Procedures Outline Basics Control Flow While Loops Expressions and Statements Functions Primitive Data Types 3 simple data types: number, string, boolean Numbers store numerical data
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationswitch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++
Comp Sci 1570 Introduction to C++ Outline 1 Outline 1 Outline 1 switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2;
More informationCS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
More informationBasics 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
More informationRepetition CSC 121 Fall 2014 Howard Rosenthal
Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationDATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design
DATA AND ABSTRACTION Today you will learn : How to work with variables How to break a program down Good program design VARIABLES Variables are a named memory location Before you use a variable you must
More information} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.
Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3.
More informationDatatypes, Variables, and Operations
Datatypes, Variables, and Operations 1 Primitive Type Classification 2 Numerical Data Types Name Range Storage Size byte 2 7 to 2 7 1 (-128 to 127) 8-bit signed short 2 15 to 2 15 1 (-32768 to 32767) 16-bit
More informationChapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++
Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure
More informationDECISION STRUCTURES: USING IF STATEMENTS IN JAVA
DECISION STRUCTURES: USING IF STATEMENTS IN JAVA S o far all the programs we have created run straight through from start to finish, without making any decisions along the way. Many times, however, you
More informationIntroduction to C Programming
1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print
More informationSoftware Design & Programming I
Software Design & Programming I Starting Out with C++ (From Control Structures through Objects) 7th Edition Written by: Tony Gaddis Pearson - Addison Wesley ISBN: 13-978-0-132-57625-3 Chapter 4 Making
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationProgram Development. Java Program Statements. Design. Requirements. Testing. Implementation
Program Development Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering saksoy@cs.bilkent.edu.tr The creation of software involves four basic activities: establishing
More informationFull file at C How to Program, 6/e Multiple Choice Test Bank
2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)
More informationPrimitive data, expressions, and variables
How the computer sees the world Primitive data, expressions, and variables Readings:.. Internally, the computer stores everything in terms of s and 0 s Example: h 0000 "hi" 0000000 0 0000 How can the computer
More informationLoops (while and for)
Loops (while and for) CSE 1310 Introduction to Computers and Programming Alexandra Stefan 1 Motivation Was there any program we did (class or hw) where you wanted to repeat an action? 2 Motivation Name
More informationCONTENTS: 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
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.
More informationWe have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really
Week 9: Methods 1 We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and hard to read Sometimes you
More informationAssoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Dr. Marenglen Biba Before writing a program to solve a problem, have a thorough understanding of the problem and a carefully planned approach to solving it. Understand the types of building
More informationMobile 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.
More informationQUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?
QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The
More informationprintf( 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
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationJava How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 9/e Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Copyright 1992-2012 by Pearson Copyright 1992-2012 by Pearson Before writing a program to solve a problem, have
More informationSudoku-4: Logical Structure
Sudoku-4: Logical Structure CSCI 4526 / 6626 Fall 2016 1 Goals To use an STL vector. To use an enumerated type. To learn about tightly coupled classes. To learn about circular dependencies. To use the
More informationCT 229 Java Syntax Continued
CT 229 Java Syntax Continued 29/09/2006 CT229 Lab Assignments One Week Extension for Lab Assignment 1. Due Date: Oct 8 th Before submission make sure that the name of each.java file matches the name given
More informationControl Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur
Control Structures Code can be purely arithmetic assignments At some point we will need some kind of control or decision making process to occur C uses the if keyword as part of it s control structure
More information