S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution SPARCS JAVA Study.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
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.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
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.
1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It.
1 Repetition structures Overview while statement for statement do while statement.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Intermediate Java Sakir YUCEL MISM/MSIT Carnegie Mellon University Program Control Slides adapted from Steven Roehrig.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Control Structures if else do while continue break switch case return for.
Chapter 4 คำสั่งควบคุม (control statement) If statement Switch statement While loop and do-while loop For loop and for-each loop Break and continue 1.
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators SPARCS JAVA Study.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
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.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Repetition Statements while and do while loops
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
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.
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.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
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.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Object-Oriented Programming Ramzi Saifan Program Control Slides adapted from Steven Roehrig.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
Unit-1 Introduction to Java
Loops in Java.
Control Structures.
Repetition-Sentinel,Flag Loop/Do_While
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
null, true, and false are also reserved.
LRobot Game.
An Introduction to Java – Part I, language basics
CSC215 Lecture Flow Control.
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
PROGRAM FLOWCHART Iteration Statements.
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
Presentation transcript:

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution SPARCS JAVA Study

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems true and false == / != /review/ Java doesn’t allow you to use a number as a boolean.. //! if(a){ … } if(a != 0){ … }

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems If-else test(10, 5) → 1 test(5, 10) → -1 test(5, 5) → 0 static void test(int testval, int target) { if(testval > target)//() 안의 값이 true 일 때 result = +1; else if(testval < target) result = -1; else result = 0; }

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration Looping control –while / do-while / for while public class WhileTest { static boolean condition(){ boolean result = Math.random() < 0.99; System.out.print(result + “, “); return result; } public static void main(String[] args) { while(condition())// ( condition() == true ) System.out.println(“Inside ‘while’”); System.out.println(“Exited ‘while’”); }

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration do – while a = 2; a = 0; do{ a++; }while(a == 1); a = 0; while(a == 1){ a++; }

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration for for ( initialization; Boolean-expression; step ) value: 97 character: a value: 98 character: b value: 99 character: c … public static void main (String[] args) { for(char c = 0; c < 128; c++){ if(Character.isLowerCase(c)) System.out.println(“value: “ + (int)c + “ character: “ + c); }

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Iteration for – The comma operator for ( initialization; Boolean-expression; step ) for(int i = 1, j = i + 10 ; i < 5 ; i++, j = i * 2 ){ System.out.println(“i = “ + i + “ j = “ + j); } → i=1j=11 i=2 j=4 i=3 j=6 i=4 j=8

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Foreach syntax Java SE5 의 새로운 기능 pubic class ForEachFloat { public static void main (String[] args) { Random rand = new Random(); float f[] = new float[10]; for(int i = 0; i < 10; i++) f[i] = rand.nextFloat(); for(float x : f) System.out.println(x); } for(char c: “An African Swallow”.toCharArray()) System.out.print(c + “ ”); → A n A f r i c a n S w a l l o w

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems return 1.method will return any value 2.current method will exit static int test(int testval, int target) { if(testval > target)//() 안의 값이 true 일 때 return +1; else if(testval < target) return -1; else return 0; } slide 3 (if-else) test(10, 5) → 1 test(5, 10) → -1 test(5, 5) → 0

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems break and continue Can control the flow of the loop inside the body of iteration statement. i = 0; while(true) {// infinite loop // for(;;) i++; if(i > 10) break;// out of loop System.out.print(“b ”); if(i > 3) continue;// top of loop System.out.print(“a ”); } => b a b a b a b b b b b

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems The infamous “goto” Can control the flow of the loop inside the body of iteration statement. outer: outer-iteration { inner-iteration { break; // … continue; // … continue outer; // break out inner-iteration // … // and back to outer-iteration break outer; // break out inner & outer-iteration }

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems switch Multiway selection –Selector : only int / char switch(selector){ case ‘a’: case ‘e’: System.out.print(“e”); case ‘u’: System.out.print(“u”); break; default : System.out.print(“else”); } selector 가 ‘a’ / ‘e’ 였다면 :eu selector 가 ‘u’ 였다면 : u selector 가 그 외의 값이면 : else

S ystem P rogrammers' A ssociation for R esearching C omputer S ystems Homework Exercise 4 / 7 / 9 ch 5. Initialization & Cleanup 읽어오기.