UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Introduction to C Programming
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Computer Science 1620 Loops.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
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
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 7 Java Fundamentals Operators and Expressions.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Fundamentals of C and C++ Programming Control Structures and Functions.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Controlling Execution Dong Shao, Nanjing Unviersity.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
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.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
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
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Branching statements.
Lecture 4b Repeating With Loops
Control Structures.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JavaScript: Control Statements.
JavaScript: Control Statements I
Flow of Control.
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
PROGRAM FLOWCHART Iteration Statements.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri. 9/22/00

Homework #3 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date.

Java Fundamentals Control Structures

Program Control ä Sequential execution ä Instructions executed one after another ä Transfer of control ä Something other than the next instruction is executed

A Caution ä Flow of control has to be carefully thought out ä “spaghetti” code is hard to maintain ä No “goto” in Java ä goto is a keyword, but is not used

Single-entry/single-exit Programming ä Every logical “module” has a single defined entry point and a single defined exit point ä Control passes from the exit point of one “module” to the entry point of another ä Control structure stacking ä Control structure nesting

Selection (branching) ä if ä if/else ä switch

The “if” Structure ä Single selection ä If condition is true, do some special action ä If condition is false, continue

“if” Syntax ä boolean expression ä An expression that evaluates to true or false ä statement ä An arbitrary Java statement ä E.g., could be another if statement if (boolean expression) statement;

“if” Examples int j = 2; int a = 0; if (j < 3) { a = 4; } int j = 2; int a = 0; if ((j 10)) { a = 4; j = 55; if (a+j > 100) { // do something }

The “if/else” Structure ä Double selection ä If condition is true, do some special action ä If condition is false, do some other special action ä Then continue

“if/else” Syntax ä statement2 can be its own “if-statement” resulting in “if/else-if/else” nested logic ä An ‘else’ is always paired with a preceding ‘if’ ä The most recent previous one unless bracketed otherwise ä Similar to conditional operator (?:) ä boolean expression ? if-true-statement : if-false-statement ä It is good form to ALWAYS bracket statement1 and statement 2 (even when they are single-line statements) (even when they are single-line statements) if (boolean expression) statement1; else statement2;

“if/else” Examples int j = 2; int a = 0; if (j < 3) { a = 4; } else { a = 5; } int j = 2; int a = 0; if (j < 3) { a = 4; } else if (j = 3 { a = 5; } else // j >= 10 { a = 6; } cascading if

The “switch” Structure ä The switch structure embodies multiple selection logic (like a cascading if) ä Do one of an arbitrary number of actions based on the value of an expression ä NOTE: With object-oriented programming, we can use derived classes and polymorphism to get multiple selection behavior more elegantly ä Select on object type (class) ä Invoke the same named method of whatever class is involved at runtime ä More later in the course

“switch” Syntax switch (integer-expression) { case constant-expression : statement; … default : statement; } Requires condition to be expressible as an int (or convert to an int - e.g., byte, char, short) Uses labeled cases (also integers) Use break to ensure only one case is executed at a time Includes support for the C/C++ default label Can be nested

“switch” Examples int n = 10; final int ONE = 1, THREE = 3, FOUR = 4; switch (n) { case ONE : Sytem.out.println(“Value is 1”); break; case THREE : case FOUR : Sytem.out.println(“Value is 3 or 4); break; default : Sytem.out.println(“Value is not 1, 3, or 4”); break; // not strictly needed, but good form }

Repetition (iteration, or loops) ä for ä while ä do/while

The “for” structure ä Basic test-compute-increment control flow ä Includes “built-in” initialization and increment elements

“for” Syntax ä initialization is executed once upon entry ä test is evaluated each time through (including first time) ä If test passes, statement block is executed ä Else, execution continues after for statement ä increment is executed after each time through loop, then test is evaluated as above (“increment” can actually be a “decrement”) ä action-statement can be a single statement (or even an empty statement/no-op) ä Both initialization and increment can be comma-separated multiple statements ä E.g., for (i = 0, j = 0; i+j < 100; i++, j +=2) { // do something } ä loop variables can be declared in the initialization ä Unless you use a comma separated multiple-statement initialization ä For good form, ALWAYS bracket the action-statement, even if it is a single- line statement for (initialization; boolean test; increment) action-statement

“for” Examples for (int i = 10000; i < 0; --i); for (int i = 1; i < 10; i++) { System.out.println(“i = “ + i); } for (int i = 1; i < 10; i+=2) { System.out.println(“i = “ + i); } for (int i = 10000; i > 0; --i);

The “while” Structure ä Basic test-compute control flow ä Does not have built-in initialization or increment ä while loop “compute” statement(s) not guaranteed to execute

“while” Syntax ä boolean expression is evaluated ä if true, ä do statement (could be a compound statement) ä control flow then returns to boolean expression (for reevaluation) ä else, control flows to next statement after while statement ä No built-in increment!! ä For good form, ALWAYS bracket the statement, even if it is a single-line statement while (boolean expression) statement

“while” Examples int n = 10; while (n > 0) { System.out.println(“n = “ + n); n--; } int n = 10; while (true) { System.out.println(“n = “ + n); n--; if (n < 0) { break; } }

The “do/while” Structure ä Basic compute-test control flow ä Loop “compute” statement(s) guaranteed to execute at least once

“do/while” Syntax ä statement is executed ä boolean expression is evaluated ä if true, do statement block again ä else, control flows to next statement after do/while statement ä No built-in increment!! ä For good form, ALWAYS bracket the statement, even if it is a single- line statement ä Especially critical here for readability do statement while (boolean expression)

“do/while” Examples int n = 10; do { System.out.println(“n = “ + n); n--; } while (n > 0);

Related Statement Types ä labels ä break ä continue ä return

Labels ä Consists of an identifier followed by a colon ä Can be used to label: ä a repetition structure or switch ä a block that is delimited by { } ä Can work in conjunction with break and continue (to modify repetition structure control logic)

The ‘break’ Statement ä Transfer control out of a switch, for, do or while statement or out of a labeled structure ä Only break out of the innermost block (no label specified) or out of the labeled statement or compound statement identified by the break ä Use basically same as C, C++ except for use with labels ä Some programmers avoid the use of break

‘break’ Example foo: for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { // do something; if (j+k > 10) { break foo; } // do something else

The ‘continue’ Statement ä Similar to break (alters control flow in for, do, while loops) ä Can be used with a label ä With no label - return to the boolean expression and reevaluate it (the for loop’s iteration statement is executed first) ä With label - return to the labeled for, do, or while and reevaluate as above ä Some programmers avoid the use of continue

‘continue’ Example foo: for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { // do something; if (j+k > 10) { continue foo; } // do something else

The ‘return’ Statement ä Exit from a method invocation ä Can return a value (or void) ä Control flows back to the next statement after the method invocation