Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Java Control Statements
Control Structures.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
The if-else Statements
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Chapter 4: Control Structures I
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Control Structures.
Chapter 4: Control Structures I
In this class, we will cover:
CSC215 Lecture Flow Control.
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
Program Flow.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
In this class, we will cover:
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
Control Structure.
Presentation transcript:

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors

In this class, we will cover: Blocks and scope Writing decision-making statements Writing loops Declaring and accessing arrays and vectors

Blocks and Scope All classes and methods contain code within curly brackets called blocks. Where is the error in this class. class ScopeExample { public static void main(String[] args) { // aNumber comes into existence int aNumber = 22; System.out.println("Number is " + aNumber); { // anotherNumber comes into existence int anotherNumber = 99; System.out.println("aNumber is " + aNumber); System.out.println("anothreNumber is " + anotherNumber); } // End of block - anotherNumber ceases to exist System.out.println("aNumber is " + aNumber); System.out.println("anotherNumber is " + anotherNumber); } // End of outer block - anumber ceases to exist }

Writing Decision-Making Statements Decision-Making Statements –Determine whether a condition is true, and take some action based on determination –What are the three ways to write a decision-making statement in Java?

Three ways to write decision-making statements: –if statement –switch statement –conditional operator

Writing Decision-Making Statements Writing if Statements –if statement: Interrogates logical expression enclosed in parentheses –boolean expressions can use just one word –example: boolean isFound=true; if (isFound) System.out.println(“The object is found.”); Determines whether it is true or false Uses logical operators to compare values

Writing Decision-Making Statements Writing if Statements –You don’t need the brackets for only one statement. –if statements can contain compound expressions Two expressions joined using logical operators –OR  || –AND  && –Nested if statement if statement written inside another if statement the else statement always corresponds with the closest if statement

Writing Decision-Making Statements Using the Conditional Operator –Conditional operator (?) Provides a shortcut to writing an if-else statement Structure: –variable = expression ? value1:value2; example int smallerNumber = (a < b) ? a : b;

Writing Decision-Making Statements Writing switch Statements –Acts like a multiple-way if statement –Transfers control to one of several statements or blocks depending on the value of a variable –Used when there are more than two values to evaluate –Restrictions: Each case evaluates a single variable for equality only Variable being evaluated must be: char, byte, short, or int

Example of Switch Statement char eventType; … switch (eventType) { case ‘A’: eventCoordinator = “Dustin”; break; case ‘B’: eventCoordinator = “Heather”; break; case ‘C’: eventCoordinator = “Will”; break; default: eventCoordinator = “Invalid Entry”; }

Writing Loops Loops –Provides for repeated execution of one or more statements until a terminating condition is reached –Three basic types: while do for –What is the difference between the while and do loops?

Writing Loops Writing while Loops –Loop counter Counts number of times the loop is executed –Two kinds of loops Pre-test loop –Tests terminating condition at the beginning of the loop Post-test loop –Tests terminating condition at the end of the loop –Example of a while loop while (count <= 10) { System.out.println(“count = “ + count); count++; }

Writing Loops Writing do Loops –Loop counter Counts number of times the loop is executed –Post-test loop –Tests terminating condition at the end of the loop –Forces execution of statements in the loop body at least once –Example: do { System.out.println(“count = “ + count); count++; } while (count <= 10);

Writing Loops Writing for Loops –Loop counter Counts number of times the loop is executed –Pre-test loop –Tests terminating condition at the beginning of the loop –Includes counter initialization and incrementing code in the statement itself –Example: for (int count=1; count<=10; count++) { System.out.println(“count = “ + count); }

Writing Loops Writing Nested Loops –A loop within a loop –Useful for processing data arranged in rows and columns –Can be constructed using any combinations of while, do, and for loops

Declaring and Accessing Arrays Arrays –Allows the creation of a group of variables with the same data type –Consist of elements: Each element behaves like a variable –Can be: One dimensional Multi-dimensional

Declaring and Accessing Arrays Using One-Dimensional Arrays –Keyword new –Used to create a new array instance –int testScores[] = new int[10]; –Use brackets ([]) and indices to denote elements: testScores[5] = 75; –Note: Arrays in Java begin with 0 not 1 –Used in the main method of applications.

Declaring and Accessing Arrays Using Multidimensional Arrays –Array of arrays Three dimensions  cube Four dimensions  ??? –Each dimension has its own set of brackets: testScoreTable[5][5] = 75;

Vectors Similar to arrays but do not need to declare size Can contain a list of different object types Structure Vector vec = new Vector(); Vector class has many helpful methods such as: –isEmpty() –indexOf(Object arg) –contains(Object arg) –See java docs for more info

Vectors Use the Enumeration class to iterate through a vector –Example: Vector vec = new Vector(); vec.addElement(“one”); vec.addElement(“two”); vec.addElement(“three”); Enumeration e = vec.elements(); while (e.hasMoreElements()) { System.out.println(“element = “ + e.nextElement()); } –Result: element = one element = two element = three