© 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II.

Slides:



Advertisements
Similar presentations
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 05 (Part III) Control Statements: Part II.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Introduction to Computer Programming
Chapter 4 Control Statements: Part 2
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Chapter 5- Control Structures: Part 2
Control Statements: Part 2
Control Structures: Part 2
JavaScript: Control Statements I
Chapter 5- part 2 Control Statements: Loops 2
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 5 Control Statements: Loops 2
Chapter 6 Control Statements: Part 2
Chapter 4 - Program Control
Chapter 5 Control Statements: Part 2; Logical Operators
Presentation transcript:

© 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Summary of topics covered: for repetition statement do … while repetition statement switch multiple-selection statement break statement continue statement Logical operators Pseudocode Practice Number conversions Binary Hexadecimal Decimal

We previously learned… if if…else while More control statements… for repetition statement do … while repetition statement switch multiple-selection statement break statement continue statement © by Pearson Education, Inc. All Rights Reserved. More Control Statements

Counter-controlled repetition requires – a control variable (or loop counter) – the initial value of the control variable – the increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop) – the loop-continuation condition that determines if looping should continue. © by Pearson Education, Inc. All Rights Reserved. Repetition with Counters

Counter Controlled Repetition while

Counter Controlled Repetition for

for ( int counter = 1 ; counter <= 10 ; counter++) { //block of code that will loop } for keyword for keyword counter variable starting value of counter variable required semicolons increments the counter variable increments the counter variable loop continuation condition

All three expressions in a for header are optional. – If the loopContinuationCondition is omitted, the condition is always true, thus creating an infinite loop. – You might omit the initialization expression if the program initializes the control variable before the loop. – You might omit the increment if the program calculates it with statements in the loop’s body or if no increment is needed. The increment expression in a for acts as if it were a standalone statement at the end of the for ’s body, so counter = counter + 1 counter += 1 ++counter counter++ are equivalent increment expressions in a for statement. for Repetition Statement

a)Vary the control variable from 1 to 100 in increments of 1. for ( int i = 1; i <= 100; i++ ) b)Vary the control variable from 100 to 1 in decrements of 1. for ( int i = 100; i >= 1; i-- ) c)Vary the control variable from 7 to 77 in increments of 7. for ( int i = 7; i <= 77; i += 7 ) © by Pearson Education, Inc. All Rights Reserved. for Repetition Statement examples:

d)Vary the control variable from 20 to 2 in decrements of 2. for ( int i = 20; i >= 2; i -= 2 ) e)Vary the control variable over the values 2, 5, 8, 11, 14, 17, 20. for ( int i = 2; i <= 20; i += 3 ) f)Vary the control variable over the values 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. for ( int i = 99; i >= 0; i -= 11 ) © by Pearson Education, Inc. All Rights Reserved. for Repetition Statement examples:

for Repetition Statement example:

Compound interest application A person invests $1000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to determine the amounts: a = p (1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., use 0.05 for 5%) n is the number of years a is the amount on deposit at the end of the nth year. for Repetition Statement Sample Problem:

Loop performs the indicated calculation for each of the 10 years the money remains on deposit. Java treats floating-point constants like and 0.05 as type double. Java treats whole-number constants like 7 and -22 as type int. © by Pearson Education, Inc. All Rights Reserved. Strategy and tips…

String substitution with printf System.out.printf(“%s%20s\n”,”Year”,”Amount on deposit”); %s %20s %-20s Insert String at this point Insert right justified String at this point Fill will spaces (if necessary) to occupy 20 character spaces. Insert left justified String at this point. Fill will spaces (if necessary) to occupy 20 character spaces.

Integer substitution with printf System.out.printf(“%4d%,20.2f\n”,year,amount); %d %4d %-,4d Insert integer at this point Insert right justified integer at this point fill will spaces (if necessary) to occupy 4 spaces. Insert left justified integer at this point fill will spaces (if necessary) to occupy 4 spaces. Format with commas. %,4d Insert right justified integer at this point fill will spaces (if necessary) to occupy 4 spaces. Format with commas at thousandth’s place.

Floating point substitution with printf System.out.printf(“%4.2f%”,price); %5.2f %-,5.2f Include up to two digits to the right of the decimal. Fill with zeros, round if needed. All of the previous formatting, left justified. %,5.2f Include commas at the thousanth’s. %5f Insert double or float at this point with up to 5 digits to the left of the decimal and six (default) digits to the right of the decimal. Round if needed.

Math.pow( x, y ) calculates the value of x raised to the y th power. The method receives two double arguments and returns a double value. Classes provide methods that perform common tasks on objects. Most methods must be called on a specific object. Many classes also provide methods that perform common tasks and do not require objects. These are called static methods. Java does not include an exponentiation operator— Math class static method pow can be used for raising a value to a power. You can call a static method by specifying the class name followed by a dot (. ) and the method name, as in ClassName. methodName ( arguments ) Utilitarian Class Math double myResult = Math.pow(10,2);

The do…while repetition statement is similar to the while statement. In the while, the program tests the loop-continuation condition at the beginning of the loop, before executing the loop’s body; if the condition is false, the body never executes. The do … while statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once. When a do … while statement terminates, execution continues with the next statement in sequence. do..while Repetition Statement

switch multiple-selection statement performs different actions based on the possible values of a constant integral expression of type byte, short, int or char. © by Pearson Education, Inc. All Rights Reserved. switch Multiple Selection Statement

Scanner method hasNext determine whether there is more data to input. This method returns the boolean value true if there is more data; otherwise, it returns false. As long as the end-of-file indicator has not been typed, method hasNext will return true. © by Pearson Education, Inc. All Rights Reserved. switch Multiple Selection Statement

The switch statement consists of a block that contains a sequence of case labels and an optional default case. The program evaluates the controlling expression in the parentheses following keyword switch. The program compares the controlling expression’s value (which must evaluate to an integral value of type byte, char, short or int ) with each case label. If a match occurs, the program executes that case ’s statements. The break statement causes program control to proceed with the first statement after the switch. © by Pearson Education, Inc. All Rights Reserved. switch Multiple Selection Statement

switch does not provide a mechanism for testing ranges of values—every value must be listed in a separate case label. Note that each case can have multiple statements. switch differs from other control statements in that it does not require braces around multiple statements in a case. Without break, the statements for a matching case and subsequent cases execute until a break or the end of the switch is encountered. This is called “falling through.” If no match occurs between the controlling expression’s value and a case label, the default case executes. If no match occurs and there is no default case, program control simply continues with the first statement after the switch. © by Pearson Education, Inc. All Rights Reserved. switch Multiple Selection Statement

The break statement, when executed in a while, for, do … while or switch, causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch. break and continue Statements

Skips the remaining statements in the loop body and proceeds with the next iteration of the loop. Evaluates the loop-continuation test immediately after the continue statement executes. continue Statement with while or do..while: int i = 1; while(int i < 10) { if(i%2 == 0) System.out.print(i); else continue; System.out.print("X"); }

Skips the remaining statements in the loop body and proceeds with the next iteration of the loop. The increment expression executes, then the program evaluates the loop-continuation test. continue Statement with for for(int i = 1; i < 10; i++) { i++; if(i%2 == 0) System.out.print(i); else continue; System.out.print("X"); }

Java’s logical operators enable you to form more complex conditions by combining simple conditions. The logical operators are – && (conditional AND) – || (conditional OR) – & (boolean logical AND) – | (boolean logical inclusive OR) – ^ (boolean logical exclusive OR) – ! (logical NOT). [Note: The &, | and ^ operators are also bitwise operators when they are applied to integral operands.] Logical Operators

Ensures that two conditions are both true before choosing a certain path of execution. Java evaluates to false or true all expressions that include relational operators, equality operators or logical operators. Conditional AND && short circuits

Ensures that either or both of two conditions are true before choosing a certain path of execution. Operator && has a higher precedence than operator ||. Both operators associate from left to right. Conditional OR II Which will short circuit?

Identical to the && and || operators, except that the & and | operators always evaluate both of their operands (i.e., they do not perform short-circuit evaluation). This is useful if the right operand of the boolean logical AND or boolean logical inclusive OR operator has a required side effect—a modification of a variable’s value. Boolean Logical Operators | &

Is true if and only if one of its operands is true and the other is false. If both are true or both are false, the entire condition is false. This operator is guaranteed to evaluate both of its operands. Boolean Logical Exclusive OR ^

Reverses the meaning of a condition. The logical negation operator (!) is a unary operator that has only a single condition as an operand. The logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is false. In most cases, you can avoid using logical negation by expressing the condition differently with an appropriate relational or equality operator. Logical NOT !

Boolean substitution with printf int x = 5; int y = 10; boolean myBoolean = (x < y); System.out.printf(“x < y is %b”,myBoolean); %b Insert boolean (true or false at this point

Pseudocode is an informal language that helps you develop algorithms without having to worry about the strict details of Java language syntax. Particularly useful for developing algorithms that will be converted to structured portions of Java programs. Similar to everyday English. Helps you “think out” a program before attempting to write it in a programming language, such as Java. You can type pseudocode conveniently, using any text-editor program. Carefully prepared pseudocode can easily be converted to a corresponding Java program. Pseudocode normally describes only statements representing the actions that occur after you convert a program from pseudocode to Java and the program is run on a computer. – e.g., input, output or calculations. Pseudocode

Write pseudocode for a routine that will calculate the average age of the students in this class

Implement a simple cash register program for a Dallas toy store. A customer will bring a basket of toys to the cashier to check out. Each item has a price tag with the price on it. All items of the same type will have the same price. The system should prompt the cashier for the quantity and price of each product. When there are no more items to enter, the cashier will enter a quantity of zero. When this happens, the system will calculate and display the subtotal, sales tax (8.25%) and grand total owed. The customer hands payment to the cashier, who enters the payment amount in the system. The system calculates and displays change due to the customer. Together we’ll convert the pseudocode into a Java application. Write pseudocode for the process of handling a sales transaction.

Numbering Systems 16Dec mal Decimal Base 10 Binary Base 2 On/off 0/1 Accommodates electronic pulses Hexadecimal Base 16 Accommodates large numbers with fewer digits than Base 10 Uses digits 0-9 then A-F to represent decimal 0-16

etc… ASCII Translator Java

Translation 16Dec mal Morse Code (e.g.) converts pulses to letters The next step is to organize the letters into words.

Translation and Computers 16Dec mal Operating systems require a translation system. ASCII is a popular translation system. Binary pulses are translated to characters.

Translation and Computers 16Dec mal Hexadecimal numbers are more efficient. More information can be communicated in each pulse. Shorter word length allows faster communication.

Binary Numbers Base 2 8 Bits = 1 Byte Each bit contains 0 or 1 (off or on) What is the highest possible value in a byte?

Value?

Converting Decimal to Binary / 2 = 37 rem 1 37 / 2 = 18 rem 1 18 / 2 = 9 rem 0 9 / 2 = 4 rem 1 4 / 2 = 2 rem 0 2 / 2 = 1 rem 0 1/ 2 = 0 rem Converting to Base 2 (binary) Converting to Base 2 (binary)

Hexadecimal Numbers Base 16 Shorthand for expressing numbers ABCDEF ABCDEF ABCDEF ABCDEF ABCDEF ABCDEF ABCDEF ABCDEF Each digit contains 0- F (values 0 – 15)

Hexadecimal Numbers Base 16 Shorthand for expressing numbers ,536’ s 4096’s256’s16’s1’s Each bit contains 0 – F

65,536’ s 4096’s256’s16’s1’s F2A4C

Converting Decimal to Hex / 16 = 4 rem 11 (B) 4 / 16 = 0 rem 4 4 B Converting to Base 16 (binary) Converting to Base 16 (binary)

Converting Decimal to Hex / 16 = 122 rem 10 (A) 122 / 16 = 7 rem 10 (A) 7 / 16 = 0 rem 7 7 A A Converting to Base 16 (binary) Converting to Base 16 (binary)

Selection is implemented in one of three ways: – if statement (single selection) – if … else statement (double selection) – switch statement (multiple selection) The simple if statement is sufficient to provide any form of selection—everything that can be done with the if … else statement and the switch statement can be implemented by combining if statements. Summary

Repetition is implemented in one of three ways: – while statement – do … while statement – for statement The while statement is sufficient to provide any form of repetition. Everything that can be done with do … while and for can be done with the while statement. Summary

Combining these results illustrates that any form of control ever needed in a Java program can be expressed in terms of – sequence – if statement (selection) – while statement (repetition) and that these can be combined in only two ways—stacking and nesting. Summary