FLOW CONTROL. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

Similar documents
Flow Control. Key Notion. Statement Categories. 28-Oct-10

CompSci 125 Lecture 11

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items

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

CS111: PROGRAMMING LANGUAGE II

Exceptions. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Algorithms Boaz Kantor, IDC Herzliya

Example: Monte Carlo Simulation 1

CONDITIONAL EXECUTION: PART 2

Midterm Examination (MTA)

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Theory of control structures

LECTURE 5 Control Structures Part 2

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Introduction. C provides two styles of flow control:

Advanced if/else & Cumulative Sum

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Loops. Repeat after me

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Chapter 10. Arrays and Collections. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Java Classes: Random, Character A C S L E C T U R E 6

BM214E Object Oriented Programming Lecture 4

Condi(onals and Loops

ITERATION WEEK 4: EXMAPLES IN CLASS

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Assignment 2.4: Loops

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Programming in the Small II: Control

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

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

AP COMPUTER SCIENCE A

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

Honors Computer Programming 1-2. Chapter 5 Exam Prep

C++ Programming: From Problem Analysis to Program Design, Third Edition

1 Short Answer (10 Points Each)

Computer Programming I - Unit 5 Lecture page 1 of 14

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

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

Repetition and Loop Statements Chapter 5

The Java language has a wide variety of modifiers, including the following:

204111: Computer and Programming

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

6 COMPUTER PROGRAMMING

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

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

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

18-Dec-09. Exercise #7, Joongle. Background. Sequential search. Consider the following text: Search for the word near

Loops. CSE 114, Computer Science 1 Stony Brook University

Last Class. While loops Infinite loops Loop counters Iterations

Controls Structure for Repetition

Arrays. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

CSE 114 Computer Science I

Repetition, Looping. While Loop

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

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Bjarne Stroustrup. creator of C++

APCS Semester #1 Final Exam Practice Problems

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

Chapter 3. Selections

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

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

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

CS111: PROGRAMMING LANGUAGE II

Chapter 5. Selection Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Top-Down Program Development

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Decision Making in C

Java Programming Language Mr.Rungrote Phonkam

CSE431 Translation of Computer Languages

STUDENT LESSON A12 Iterations

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Control Structures in Java if-else and switch

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Transcription:

Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter 2008-9 Semester FLOW CONTROL

Flow Control Hold 2 balls in left hand, 1 ball in right Throw ball from left hand to right Throw ball from right Throw ball from left Hold 2 balls in left hand, 1 ball in right Throw ball from left hand to right Push yourself forward, throw your legs up Balance yourself If you re losing balance Throw ball from right Throw ball from left Throw ball from right Press your arms to regain balance Throw ball from right Throw ball from left Throw ball from left If succeeded If not Stand up on your feet

if..else if (boolean expression) { Statements else if (boolean expression) { Statements else if (boolean expression) { Statements else { Statements int greater = 3+2; int smaller = 2*3; if (smaller < greater) { System.out.println( smaller is greater ); else if (smaller <= greater++) { System.out.println( smaller is greatest ); else { System.out.println( greater is smaller );

switch.. case switch(byte/short/char/int value) { case value1: statements case value2: statements default: statements Scanner scanner = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = scanner.nextint(); switch (month) { case 1: System.out.println( January ); case 2: System.out.println( February ); case 3: System.out.println( March );.. case 12: System.out.println( December ); default: System.out.println( Invalid input. );

switch..case vs. if..else Scanner input = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = input.nextint(); switch (month) { case 1: System.out.println( January ); case 2: System.out.println( February );.. case 12: System.out.println( December ); default: System.out.println( Invalid input. ); Scanner scanner = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = scanner.nextint(); if (month == 1) { System.out.println( January ); else if (month == 2) { System.out.println( February ); else.. else if (month == 12) { System.out.println( December ); else { System.out.println( Invalid input. );

switch..case : no breaks! Scanner scanner = new Scanner(System.in); System.out.print( Enter a number (1-12): ); int month = scanner.nextint(); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numdays = 31; case 4: case 6: case 9: case 11: numdays = 30; case 2: if (((year % 4 == 0) &&!(year % 100 == 0)) (year % 400 == 0) ) { else { default: numdays = 29; numdays = 28; System.out.println("Invalid month."); System.out.println("Number of Days = " + numdays);

while Loop On each iteration (including first), condition is reevaluated Each iteration is done only if condition is true while glass is not empty, take a sip while (boolean expression) { Statements int x = 1024; while (x > 2) { x /= 2; Or: while ((x /= 2) > 2) {

while vs. do..while while do..while Run iteration Check condition If true: run iteration Check condition If true: run iteration do..while iteration runs at least once while iteration may not run at all

for Loop A loop, usually used for counting steps Iteration is done only if condition is true (usually if counter hasn t reached goal) Allows action on each iteration (usually increasing counter) for (initializations; boolean expression; actions) { statements

for Loop, Example Count the sum of all numbers from 1 to 4 int sum = 0; for (int i = 1; i <= 4; i++) { sum += i; System.out.println( Sum of numbers 1-4: + sum); i sum i <= 4 Undefined 0 1 0 True 1 1 2 True 3 3 True 6 4 True 10 5 False Sum of numbers 1-4 is: 10

for vs. while for loop for (int x = 0; x < 10; x++) { System.out.println( x= + x); while loop int x = 0; while (x < 10) { System.out.println( x= +x); x++;

Flow Control Duality

Nested Loops Loops within loops Garbage truck: Go through every street Go through every house in the street Collect garbage Accumulate the garbage weight to the truck weight long garbageintons = 0; for (int currentstreet = 0; currentstreet < numofstreetsintown; currentstreet++) { for (int currenthouse = 0; currenthouse < numofhousesinstreet; currenthouse++) { garbageintons += getgarbage(currentstreet, currenthouse);

Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter 2008-9 Semester FLOW CONTROL