In this class, we will cover:

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Arrays.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
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.
Control Structures Corresponds with Chapters 3 and 4.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
VB .NET Programming Fundamentals
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
The switch Statement, DecimalFormat, and Introduction to Looping
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
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.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Information and Computer Sciences University of Hawaii, Manoa
Core Java Statements in Java.
Lecture 4b Repeating With Loops
Java Language Basics.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Control Structures.
User input We’ve seen how to use the standard output buffer
JavaScript: Control Statements.
Chapter 8 Repetition Statements
Decision statements. - They can use logic to arrive at desired results
Outline Altering flow of control Boolean expressions
Java - Data Types, Variables, and Arrays
In this class, we will cover:
Java Language Basics.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
Seating “chart” Front - Screen rows Back DOOR.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
Control Structure.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
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 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 Two kinds of loops 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 Post-test loop Example: 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 Pre-test loop 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 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