CMPE212 – Reminders The other four assignments are now posted.

Slides:



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

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.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
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.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Working with Loops, Conditional Statements, and Arrays.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
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.
Spring 2006CISC101 - Prof. McLeod1 Last Time Built “HelloWorld.java” in BlueJ (and Eclipse). Looked at Java keywords. Primitive types. Expressions: Variables,
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Review 1.
Java Programming Fifth Edition
Last Time Unary operators: Other Assignment operators
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The switch Statement, and Introduction to Looping
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Loop Structures.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Conditionals & Boolean Expressions
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Control Structures – Selection
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
3 Control Statements:.
CSC215 Lecture Control Flow.
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155.
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC101 Reminders All assignments are now posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
PROGRAM FLOWCHART Iteration Statements.
Additional control structures
Chap 7. Advanced Control Statements in Java
CISC101 Reminders Assignment 3 due today.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

CMPE212 – Reminders The other four assignments are now posted. Winter 2019 CMPE212 4/3/2019 CMPE212 – Reminders The other four assignments are now posted. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today Conditionals. Loops. Winter 2019 CMPE212 - Prof. McLeod

Conditionals or “Selection Statements” Winter 2019 CMPE212 Conditionals or “Selection Statements” We will consider if, if-else and switch statements. Simple if statement syntax: if (boolean_expression) statement_when_true; Example: if (capacitance < 0) System.out.println(“Illegal capacitance”); Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

if-else Statement Syntax of if-else statement: Example: if (boolean_expression) statement_when_true; else statement_when_false; Example: if (stress > maxStress / 1.5) result = “failure”; result = “pass”; Winter 2019 CMPE212 - Prof. McLeod

if-else Statement, Cont. With statement blocks: if (boolean_expression) { block_of_code_when_true } else { block_of_code_when_false Note indentation and bracket alignment. Winter 2019 CMPE212 - Prof. McLeod

Abbreviated if Statement Uses the “ternary operator” - ? expression1 ? expression2 : expression3 expression1 must evaluate to a boolean expression2 (when true) and expression3 (when false) must evaluate to the same type. You could use this with an assignment, for example: int smaller = a <= b ? a : b; stores the smaller of the two numbers in smaller. Winter 2019 CMPE212 - Prof. McLeod

“Chained” if Statements Syntax: if (condition1) { block1 } else if (condition2) { block2 } else if (condition3) { block3 } else if (condition4) { block4 } … else { blockn } Each condition is tested in turn, until one is evaluated to true. If none of them are true then the else block is executed. Winter 2019 CMPE212 - Prof. McLeod

Dangling else Problem It is not unusual to nest if statements inside each other. One issue that can arise is the “Dangling else” problem. See DanglingElse.java Indentation might give a visual clue, but has no syntactic meaning. The else should be associated with the closest if. Use { } if necessary. Winter 2019 CMPE212 - Prof. McLeod

switch Statement Syntax: switch (expression) { case val1: // statements if expression produces val1 break; case val2: // statements if expression produces val2 case val3: … default: // statements if none of the above is true } // end switch Winter 2019 CMPE212 - Prof. McLeod

switch Statement - Cont. The code to be run depends on which val# value matches expression. If none match, the statements after the default: clause are run. The expression and val# values (or “Case Labels”) must all be of the same char, integer or String type. The break; statements make sure that following cases are not executed after a match has been made. It is possible to do multiple cases on one line, but it is clumsy: Winter 2019 CMPE212 - Prof. McLeod

switch Statement - Cont. switch (expression) { case val1: case val2: case val3: // statements if expression is val1, val2 or val3 break; case val4: case val5: // statements if expression is val4 or val5 case val6: … default: // statements if none of the above is true } // end switch Winter 2019 CMPE212 - Prof. McLeod

switch Statement - Cont. Not too useful a construct. Menu coding is a possible use: Provide a number of options to the user, like “(A)dd, (E)dit or (D)elete”. The user presses a, e, d, A, E, D, or some other key. In a switch statement, you would have: Winter 2019 CMPE212 - Prof. McLeod

switch Statement - Cont. switch (userResponse) { // userResponse is a char case ‘a’: case ‘A’: // Add operation code break; case ‘e’: case ‘E’: // Edit operation code case ‘d’: case ‘D’: // Delete operation code default: // Tell user wrong key pressed } // end switch Winter 2019 CMPE212 - Prof. McLeod

switch Statement with Strings See Switch.java. Comparisons are case sensitive. As if .equals() is being used. Generates more efficient bytecode than what you would get from a chained if construct. Winter 2019 CMPE212 - Prof. McLeod

Repetition or Using “Loops” Fall 2013 CMPE212 Repetition or Using “Loops” Java has: while do/while for The “for each” loop Also will discuss the use of break and continue Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

“while” loop - Cont. while loop syntax: block_of_code } while ( boolean_expression ) { block_of_code } As long as boolean_expression evaluates to true the statements in the block_of_code continue to execute. One statement inside the loop does not need { }. By mistake, you might write the following - what would happen? while ( boolean_expression ); line_of_code Winter 2019 CMPE212 - Prof. McLeod

“do/while” loop Syntax: do { block_of_code } while ( boolean_expression ); Note the “;” at the end of the while statement. Since the conditional test is at the end of the loop, it will always execute the loop at least once. Winter 2019 CMPE212 - Prof. McLeod

“for” loop The kind of while loop shown below: int i = 1; while (i < 21) { // other statements i = i + 1; } is used so often, that Java has provided another looping structure that does all that is shown above, but needs only one line: for (int i = 1; i < 21; i = i + 1) { or i++ Winter 2019 CMPE212 - Prof. McLeod

“for” loop - Cont. Syntax: for (initialization; boolean_expression; update) { block_of_code } for loops are used when you know, in advance, the number of repetitions desired. If there is only one statement inside the loop you don’t need the { } brackets. Winter 2019 CMPE212 - Prof. McLeod

“for” loop - Cont. You don’t have to declare the counter inside the for loop, if you have declared it earlier in your program. But if you do declare it in the for statement then the scope of that variable will only be inside the loop block. Winter 2019 CMPE212 - Prof. McLeod

“for each” Loop in Java 5+ Often, you will want to visit every element in a collection, not just a part. Syntax of the “for each” loop: for (type_variable : collection) { // statements } Winter 2019 CMPE212 - Prof. McLeod

“for each” Loop in Java 5+, Cont. For example, suppose we have an array called data, containing a collection of double type numbers, and you want to add them all up: double sum = 0; for (double e : data) sum = sum + e; or sum += e; Winter 2019 CMPE212 - Prof. McLeod

“for each” Loop in Java 5+, Cont. Equivalent normal for loop: double sum = 0; for (int i = 0; i < data.length; i++) sum = sum + data[i]; The for each loop is a bit easier with arrays, but is even better suited for other kinds of collections. A for each loop does not allow you to change elements in the collection. Use [ ] and an index value instead in the LHS of an assignment. Winter 2019 CMPE212 - Prof. McLeod

Loops - Misc. Don’t declare variables inside loops, as the repeated declaration process uses up time and memory unnecessarily. There is no limit in Java to how many levels you can nest loops. It is customary, but not necessary, to use the variables i, j, k as loop counters, when the counter has no intrinsic meaning. Winter 2019 CMPE212 - Prof. McLeod

Other Java Keywords Used With Loops break and continue The continue statement interrupts the execution of a loop, and returns control to the top of the loop. The break statement interrupts the execution of a loop, and transfers control to the first statement after the loop. Winter 2019 CMPE212 - Prof. McLeod

Use of continue For example: Displays: for (i = 1; i <= 5; i++) { if ( i == 3 ) continue; System.out.println(“i = ” + i); } System.out.println(“End of Loop!”); Displays: i = 1 i = 2 i = 4 i = 5 End of Loop! Winter 2019 CMPE212 - Prof. McLeod

Use of break For example: Displays: for (i = 1; i <= 5; i++) { if ( i == 3 ) break; System.out.println(“i = ” + i); } System.out.println(“End of Loop!”); Displays: i = 1 i = 2 End of Loop! Winter 2019 CMPE212 - Prof. McLeod

Use of “break” & “continue” Only use these keywords when it makes your code easier to read. Avoid the use of more than one break or continue inside a loop. If you use a simple condition to issue a break statement, then why can’t you put that condition in the loop test? Overuse of break statements can lead to “spaghetti” code - just like the use of “goto” statements! Winter 2019 CMPE212 - Prof. McLeod

Bad Code 1 What is “wrong” with this code? (It compiles fine.) boolean continueFlag = true; String userInput; while (continueFlag != false) { userInput = IOHelper.getString("Enter \"yes\" to stop loop: "); continueFlag = !userInput.equals("yes"); } Winter 2019 CMPE212 - Prof. McLeod

Bad Code 2 What is “wrong” with this kind of loop structure?: int i = 0; while (true) { System.out.println("i = " + i); if (i >= 20) break; i = i + 1; } Winter 2019 CMPE212 - Prof. McLeod

Bad Code 3 What is “wrong” with this structure?: String userInput; for (int i = 0; i < 100; i++) { System.out.println("i = " + i); userInput = IOHelper.getString("Enter \"yes\" to stop loop: "); if (userInput.equals("yes")) i = 100; } Winter 2019 CMPE212 - Prof. McLeod