CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
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.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
4 Control Statements.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
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 Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Control Statements: Part 1
JavaScript: Control Statements I
JavaScript: Control Statements.
JavaScript: Control Statements I
Looping and Repetition
Outline Altering flow of control Boolean expressions
3 Control Statements:.
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Chap 7. Advanced Control Statements in Java
Review of Previous Lesson
Dale Roberts, Lecturer IUPUI
Control Statements:.
Presentation transcript:

CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05

Compound Assignment Operators Way of shortening code. Assume, c = 3. c = c + 7; c += 7; //same as above

Compound Operators OperatorUseEquivalent to +=c += 7;c = c + 7; -=d -= 4;d = d – 4; *=e *= 5;e = e * 5; /=f /= 3;f = f / 3; %=g %= 9;g = g % 9;

More Shorthand You can also use ++ and -- if you wish to only change the integer by 1. For example, d++ is the same as writing d=d+1. It is also the same as writing d += 1 The same holds true for the --.

Prefix vs. Postfix Prefix is ++b while postfix is b++. Prefix and postfix operators are used mainly in loops. Whether you use prefix or postfix will determine the outcome of your program.

Prefix Increment Example: ++a It would increment by 1, then use the new value of a in the expression in which a resides. int a = 5; b = ++a; What is b? What is a?

Postfix Increment Example: a++ It uses the current value of a in the expression in which it resides, then increments by 1. int a = 5; b = a++; What is b? What is a?

Decrement The same holds true for the postfix and prefix decrement operators. Depending on whether or not it is pre or post depends on when it gets computed.

Ternary Operator Takes in 3 operands. The first leftmost operand is a boolean expression (it can only return true or false) The second (between ? and :) is what is done if it is true. The third is what is done if it returns false. Shorthand for if/else statements.

Ternary Operator 2 An example: System.out.println( grade>=60 ? “Passed” : “Failed” ); If the grade is >= 60, it prints out Passed. Else it prints out Failed. ( grade >= 60 ? “Passed” : “Failed”)

Precedence Some operators execute before others. This is because they have a higher precedence. Useful if you have more than one mathematical operation taking place on one line.

Precedence Chart (Postfix)High precedence (Prefix) * / % + - >= == != ?: = += -= /= */ %=Low precedence

Counter-Controlled Repetition Counter-controlled repetition requires: A control variable Initial Value The increment (or decrement) Loop-continuation condition

Counting a while loop int counter = 1; while (counter <=10 ) { System.out.println(counter); counter++; } //prints out integers 1 through 10 on a // separate line

The for statement for( int count = 1; count <=10; count++) { System.out.println(count); } //same output as the previous while loop

For Brackets Once again, the brackets are not required if there is just one line of code below the for statement. If there is more than one line, they are required.

For loops Is this legal? for( ; ; ) If so, what does it do? What does it not do?

General For format for ( initialization ; loopContinuationCondition ; increment) //should be 1 line { statement/algorithm }

Common errors When a for statement’s control variable is declared in the initialization section of the for loop, editing that same variable in the for body will produce an error. In other words, don’t alter the variable inside the body of the for loop. To sidestep this problem, use temp variables and set them equal if need be.

Finding Sum int total = 0; for( int i=2; i<=20 ; i+2 ) total += i; System.out.println(“Sum is “ + total ); /* Above finds the sum of all even numbers 2 through 20 and prints it out */

Do…while statement int counter = 1; do { System.out.println(counter); counter++; } while ( counter <= 10 ); //notice the ; // Same as while statement a few slides ago

Braces on do…while Although you do not need braces on a do while statement if it is only 1 line, it is common to add them to help readability and to separate between the while and the do.

Do…While Every do while statement can be written as a for statement or a while statement. Most people opt for the for loop instead of writing a do…while loop.

Switch statement Can only compare integer values Useful if there are several different conditions. See the SampleSwitch.java code

switch switch (integer value) case 1: case 2: do something here break; default: do something here break;

Switch Defaults Goes to default if it doesn’t match anything else. You should always have a default after the last case. The last case in a switch statement doesn’t need a default, although most programmers add it for clarity.

Break A break statement will “break” out of the loop it was in. for( int i=1; I <=10; i++) { if (i == 5) //if i == 5, break; // break out of for loop System.out.println( i ); }

Continue Continue statements can be avoided by using structured programming. Therefore, they will not be covered in this course.

Assignment #2 Tax withholding calculator Due Thursday May 26 th Description available online at course website

Questions…