Introduction to Computers and Programming Lecture 7:

Slides:



Advertisements
Similar presentations
Chapter 04 (Part III) Control Statements: Part I.
Advertisements

1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Introduction to Computers and Programming Lecture 5 New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Introduction to Computers and Programming Class 9 Introduction to C Professor Avi Rosenfeld.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1 Outline 4.1Introduction 4.2Algorithms 4.3Pseudocode 4.4Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Computer Programming Control Structure
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 7 JavaScript: Control Statements, Part 1
while Repetition Structure
Chapter 4 – Control Structures Part 1
3 Control Statements:.
Chapter 4 - Control Structures: Part 1
Presentation transcript:

Introduction to Computers and Programming Lecture 7:

First Midterm March 7th I will tell you the exact material covered in the coming classes. I will also talk about the format of the exam. There will be no make up exam so your attendance is mandatory.

Road map char data type

Review True or False: An int variable can contain any integer. When must you use an if / else if / else statement instead of using a switch statement? When is it preferable to use a switch statement? What is a magic number? How should you deal with a magic number? Why? Explain what the exclusive or (^) operator tests. (exp a) ^ (exp b) Define the purpose of each part of this expression: (part a) ? (part b) : (part c)

Review continued What is the output of the following code fragment? int a = 100, b = 50; if ((a == 60) && (b <= 100)) System.out.println ("Yes"); What is the output of the following code fragment? int a = 100, b = 50; if ((a == 60) || (b <= 100)) System.out.println ("Yes");

Review continued What is the output of this switch statement? int a = 90; switch (a) { case 80: System.out.println (80); case 90: System.out.println (90); case 100: System.out.println (100); }

char data type

Java allows us to store "single" character values. char character = 'a'; // not the same as "a"; char character = '7'; char newline = '\n'; char tab = '\t'; char space = ' '; The characters are actually stored as integers (ascii values). See Note: chars use single quotes. We have seen that Strings use double quotes.

ASCII Table Source: Liang

char arithmetic Given: char letter = 'a'; The following code: letter = (char) (letter + 1); Would result in letter storing a 'b' character. If we add or subtract two char values, the result is an int value. –For example 'c' – 'a' would result in the value 2.

Casting between char and int values A char uses 16 bits of memory. –You can implicitly cast a char to an int char c = 'a'; int i = c; –You must explicitly cast an int to a char int i = 65; char = (char) i; Note: Even though chars are equal to shorts in the amount of memory they use, they do not hold the same values (shorts can hold negative numbers).

Reading char values from the user You should use charAt(0) to parse the character from user input. For example: char c; String cAsString; cAsString = JOptionPane.showInputDialog (null, "Enter a character"); c = cAsString.charAt(0); Will grab the first character from the user's input.

Warning about numeric digit characters The value of a the single digit numeric characters are not equivalent to the values themselves. In fact the ASCII value of '0' is 48, '1' is 49, …, '9' is 57. How do you think we could convert a numeric char to an int ?

Unicode In Java, there are many more characters available then in the basic ascii table. The ascii table only has 128 characters in it. Java uses 2 bytes to store characters which allows it to hold unique characters. Java can store any Unicode (see: unicode.org) character in a char variable.unicode.org That means you can print any character from any language on any platform. To print a Unicode character, use '\uxxxx' where xxxx is a hexadecimal representation of the Unicode for the desired character.

Assignment Operators Given the following: x = 2; x = x + 1; System.out.println ("x: " + x); There are actually several ways to rewrite this more concisely.

Short Cut Operator One option is to use the += operator x = 2; x += 1; // same as x = x + 1; System.out.println ("x: " + x); There are similar operators for *, -, /.% –x = x * 5 is equivalent to x *= 5; –x = x – 5;is equivalent to x -= 5; –x = x / 5;is equivalent to x /= 5; –x = x % 5;is equivalent to x %= 5; Good Practice: place a space before and after your short cut operators.

Increment Operator A second option is to use an increment operator: x++Post-Increment Operator ++xPre-Increment Operator Both operators will increment x by 1, but they do have subtle differences.

Pre v. Post Increment PostIncrement Operator (x++): –use the current value of x in the expression. Then, increment by 1. PreIncrement Operator (++x): –Increment x by 1. Then, use the new value of x in the expression.

How about a real example? // Preincrementing v. PostIncrementing public class PrePost { public static void main (String[] args) { int c = 5; System.out.println (c); System.out.println (c++); System.out.println (c); System.out.println(); c = 5; System.out.println (c); System.out.println (++c); System.out.println (c); } Output: Post Increment Pre Increment  2000 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

Pre v. Post Decrement PostDecrement Operator (x--): –use the current value of x in the expression. Then, decrease by 1. PreDecrement Operator (--x): –Decrease x by 1. Then, use the new value of x in the expression. Good practice: Place unary operators directly next to their operands, with no intervening spaces.

Introduction to While Loops

While Loops While Loop: Keep repeating an action while some condition remains true. Examples: –Every Stairmaster Machine contains a while loop (end condition is based on mode used). while the person is still climbing, keep displaying the status, e.g. number of stairs climbed, calories burned, etc. –Keep prompting for book orders until the user is done.

while loop (continued) For example (in pseudocode) while (some Boolean expression is true) { do this (again and again...) }

Parts of a While Loop Every while loop will always contain three main elements: 1)Priming: initialize your variables. 2)Testing: test against some known condition. 3)Updating: updates part (or all) of the expression that is tested.

Simple While Loop public class While1 { public static void main (String args[]) { int index = 1; while (index <= 10) { System.out.println ("Index: " + index); index++; } Index: 1 Index: 2 Index: 3... Index: 8 Index: 9 Index: Priming 2. Test Condition 3. Update: In this case, you can use either the pre or post increment operator.

While Loop Flowchart 1. Priming Set index=1 2. Test index <= Print value of index Update index++ TRUE FALSE

Infinite Loop Infinite Loop: A loop that never ends. –Generally, you want to avoid these! –There are special cases, however, when you do want to create infinite loops on purpose. Common Exam Questions: –Given a piece of code, identify the bug in the code. –You may need to identify infinite loops.

Infinite Loop Example #1 public class While2 { public static void main (String args[]) { int index = 1; while (index <= 10) { System.out.println ("Index: " + index); } Here, I have deleted part 3: The update statement (index++). Index: 1 … [forever]

Infinite Loop, Example #2 public class While3 { public static void main (String args[]) { int index = 1; while (index >= 0) { System.out.println ("Index: " + index); index++; } Here, I have changed Part 2: the test condition. Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 … [forever]

While Loops: Examples

While Loop Example Specification for the program: –Find the first power of 2 larger than For example: 2, 4, 8, 16, 32, etc. are powers of 2. –Which is the first power of 2 larger than 1000? –Finding the answer to this requires some kind of a while loop. –Let’s see how…  2000 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

While Loop Example Example: int product = 2; while ( product <= 1000 ) product = 2 * product; product <= 1000 product = 2 * product true false  2000 Prentice Hall, Inc. All rights reserved.

public class PowerOfTwoOver1000 { public static void main (String args[]) { int product = 2; while ( product <= 1000 ) product = 2 * product; System.out.println (product); }  2000 Prentice Hall, Inc. All rights reserved.

Counter-Controlled Repetition Counter-controlled repetition –Loop repeated until counter reaches a certain value –Definite repetition: number of repetitions is known –Example: A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz  2000 Prentice Hall, Inc. All rights reserved.

4.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Counter –Variable that controls number of times set of statements executes Average1.java calculates grade averages –uses counters to control repetition  2003 Prentice Hall, Inc. All rights reserved.

Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Fig. 4.6 Pseudocode algorithm that uses counter- controlled repetition to solve the class-average problem.  2003 Prentice Hall, Inc. All rights reserved.

1 // Fig. 4.7: Average1.java 2 // Class-average program with counter-controlled repetition. 3 import javax.swing.JOptionPane; 4 5 public class Average1 { 6 7 public static void main( String args[] ) 8 { 9 int total; // sum of grades input by user 10 int gradeCounter; // number of grade to be entered next 11 int grade; // grade value 12 int average; // average of grades String gradeString; // grade typed by user // initialization phase 17 total = 0; // initialize total 18 gradeCounter = 1; // initialize loop counter // processing phase 21 while ( gradeCounter <= 10 ) { // loop 10 times // prompt for input and read grade from user 24 gradeString = JOptionPane.showInputDialog( 25 "Enter integer grade: " ); // convert gradeString to int 28 grade = Integer.parseInt( gradeString ); 29  2003 Prentice Hall, Inc. All rights reserved.

30 total = total + grade; // add grade to total 31 gradeCounter = gradeCounter + 1; // increment counter } // end while // termination phase 36 average = total / 10; // integer division // display average of exam grades 39 JOptionPane.showMessageDialog( null, "Class average is " + 40 average, Class Average", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate the program } // end main } // end class Average1  2003 Prentice Hall, Inc. All rights reserved.