Object-Oriented Programming in Java

Similar documents
Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Chapter 4: Loops and Files

Chapter 4: Loops and Files

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Chapter 5: Loops and Files

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

The for Loop. Lesson 11

Chapter Goals. Contents LOOPS

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Increment and the While. Class 15

Chapter 2: Functions and Control Structures

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction. C provides two styles of flow control:

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

DELHI PUBLIC SCHOOL TAPI

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

For Loop. Variations on Format & Specific Examples

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Computers Programming Course 7. Iulian Năstac

STUDENT LESSON A12 Iterations

Flow Control. CSC215 Lecture

Lecture 7 Tao Wang 1

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Programming Fundamentals

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Lecture 10. Daily Puzzle

Technical Questions. Q 1) What are the key features in C programming language?

Computer Programming I - Unit 5 Lecture page 1 of 14

CS110D: PROGRAMMING LANGUAGE I

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

LAB 5: SELECTION STATEMENTS

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Looping. Arizona State University 1

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Scope of this lecture. Repetition For loops While loops

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

In this lab, you will learn more about selection statements. You will get familiar to

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Iterative Languages. Scoping

Working with JavaScript

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

CS 106 Introduction to Computer Science I

Fundamentals of Programming Session 13

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Repetition and Loop Statements Chapter 5

Accelerating Information Technology Innovation

Chapter 3. More Flow of Control

AP CS Unit 3: Control Structures Notes

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Repetition CSC 121 Fall 2014 Howard Rosenthal

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Object-Oriented Programming in Java

CS1150 Principles of Computer Science Loops (Part II)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Recap: Assignment as an Operator CS 112 Introduction to Programming

ECE 122. Engineering Problem Solving with Java

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

Java Control Statements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to the Java Basics: Control Flow Statements

Information Science 1

Chapter 4 Introduction to Control Statements

Decision Making in C

Information Science 1

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

MORE ON LOOPS IN JAVA

Operators. Java operators are classified into three categories:

CT 229 Java Syntax Continued

Visual C# Instructor s Manual Table of Contents

More on control structures

CS 112 Introduction to Programming

Key Differences Between Python and Java

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

REPETITION CONTROL STRUCTURE LOGO

Prepared by: Shraddha Modi

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Lecture 5 Tao Wang 1

Programming for Engineers Iteration

Programming Languages

Loops and Expression Types

204111: Computer and Programming

CHAPTER 9 FLOW OF CONTROL

Transcription:

CSCI/CMPE 3326 Object-Oriented Programming in Java Loop Statements Dongchul Kim Department of Computer Science University of Texas Rio Grande Valley

The Increment and Decrement Operators There are numerous times where a variable must simply be incremented or decremented. number = number + 1; number = number 1; Java provide shortened ways to increment and decrement a variable s value. Using the ++ or -- unary operators, this task can be completed quickly. number++; or ++number; number--; or --number;

Differences Between Prefix and Postfix When an increment or decrement are the only operations in a statement, there is no difference between prefix and postfix notation. When used in an expression: prefix notation indicates that the variable will be incremented or decremented prior to the rest of the equation being evaluated. postfix notation indicates that the variable will be incremented or decremented after the rest of the equation has been evaluated.

The while Loop Java provides three different looping structures. The while loop has the form: while(condition) { statements; } While the condition is true, the statements will execute repeatedly. The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop.

The while Loop Care must be taken to set the condition to false somewhere in the loop so the loop will end. Loops that do not end are called infinite loops. A while loop executes 0 or more times. If the condition is false, the loop will not execute.

The while loop Flowchart boolean expression? true statement(s) false

Infinite Loops In order for a while loop to end, the condition must become false. The following loop will not end: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); } The variable x never gets decremented so it will always be greater than 0. Adding the x-- above fixes the problem.

Infinite Loops This version of the loop decrements x during each iteration: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); x--; }

Block Statements in Loops Curly braces are required to enclose block statement while loops. (like block if statements) while (condition) { statement; statement; statement; }

The while Loop for Input Validation Input validation is the process of ensuring that user input is valid. System.out.print("Enter a number in the " + "range of 1 through 100: "); number = in.nextint(); // Validate the input. while (number < 1 number > 100) { System.out.println("That number is invalid."); System.out.print("Enter a number in the " + "range of 1 through 100: "); number = in.nextint(); }

The do-while Loop The do-while loop is a post-test loop, which means it will execute the loop prior to testing the condition. The do-while loop (sometimes called called a do loop) takes the form: do { statement(s); }while (condition);

The do-while Loop Flowchart statement(s) boolean expression? true false 5-12

The for Loop The for loop is a pre-test loop. The for loop allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code. The for loop takes the form: for(initialization; test; update) { } statement(s);

The for Loop Flowchart boolean expression? true statement(s) update false

The Sections of The for Loop The initialization section of the for loop allows the loop to initialize its own control variable. The test section of the for statement acts in the same manner as the condition section of a while loop. The update section of the for loop is the last thing to execute at the end of each loop.

The for Loop Initialization The initialization section of a for loop is optional; however, it is usually provided. Typically, for loops initialize a counter variable that will be tested by the test section of the loop and updated by the update section. The initialization section can initialize multiple variables. Variables declared in this section have scope only for the for loop.

The Update Expression The update expression is usually used to increment or decrement the counter variable(s) declared in the initialization section of the for loop. The update section of the loop executes last in the loop. The update section may update multiple variables. Each variable updated is executed as if it were on a line by itself.

Modifying The Control Variable You should avoid updating the control variable of a for loop within the body of the loop. The update section should be used to update the control variable. Updating the control variable in the for loop body leads to hard to maintain code and difficult debugging.

Multiple Initializations and Updates The for loop may initialize and update multiple variables. for(int i = 5, int j = 0; i < 10 j < 20; i++, j+=2) { statement(s); } Note that the only parts of a for loop that are mandatory are the semicolons. for(;;) { statement(s); } // infinite loop If left out, the test section defaults to true.

Nested Loops Like if statements, loops can be nested. If a loop is nested, the inner loop will execute all of its iterations for each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) loop statements; The loop statements in this example will execute 100 times.

The break Statement The break statement can be used to abnormally terminate a loop. The use of the break statement in loops bypasses the normal mechanisms and makes the code hard to read and maintain. It is considered bad form to use the break statement in this manner.

The continue Statement The continue statement will cause the currently executing iteration of a loop to terminate and the next iteration will begin. The continue statement will cause the evaluation of the condition in while and for loops. Like the break statement, the continue statement should be avoided because it makes the code hard to read and debug.

Deciding Which Loops to Use The while loop: Pretest loop Use it where you do not want the statements to execute if the condition is false in the beginning. The do-while loop: Post-test loop Use it where you want the statements to execute at least one time. The for loop: Pretest loop Use it where there is some type of counting variable that can be evaluated.

Lab4 Please at the top of the program, as a comment include at a minimum your name, the date, and the lab number.

Lab4-1 Make a program to display the aesterisks below using a nested for loop. ***** ***** ***** ***** *****

Lab4-2 Make a program to display the aesterisks below using a nested for loop. * ** *** **** *****

Lab4-3 Make a program to display the aesterisks below using a nested for loop. ***** **** *** ** *

Lab4-4 Make a program to display the aesterisks below using a nested for loop. * *** ***** ******* *********

Lab4-5 Develop Rock-paperscissors Game. Please note that the program allows player to repeat the game using while loop. Start Game 1. Rock 2. Paper 3. Scissors What do you want to throw? 3(enter) Computer:Paper vs You:Scissor You win!!! Do you want to play again(y/n)? Y 1. Rock 2. Paper 3. Scissors What do you want to throw? 2(enter) Computer:Rock vs You:Paper You win!!! Do you want to play again(y/n)? N Thank you!!! Press any key to continue.

Submission Please give a demo to your TA or instructor.