Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
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.
ITEC113 Algorithms and Programming Techniques
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
sequence of execution of high-level statements
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
ITEC113 Algorithms and Programming Techniques
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
CSI 3125, Preliminaries, page 1 Control Statements.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
COMP Loop Statements Yi Hong May 21, 2015.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
Core Java Statements in Java.
Chapter 3: Decisions and Loops
Lecture 4: Program Control Flow
CS1010 Programming Methodology
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
The University of Texas – Pan American
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter8: Statement-Level Control Structures April 9, 2019
Program Flow.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Introduction to Computer Science I.
Chap 7. Advanced Control Statements in Java
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal

Recap Implementation –Coding – writing source code –Assignments1-2 –Circle class –Point class –Applications using these classes

Today Flow control –Decision making –Loops Thinking in Java – Chapter 3

Controlling Program Flow Branching flow based on certain criteria Conditional statements Repeating a process – Loops

Normal Flow A program is a series of commands Step by step, top-down execution of commands A pre-determined series is not adequate Based on state, a different route might need to be taken Need a branching mechanism - if

Including an optional additional path Executing a set of commands based on certain conditions Condition is of a boolean type in Java: either “true” or “false” truefalse Condition?

if … if (condition) { if condition is ‘true’ instructions to execute.... } instructions after merging of both branches (normal execution path)

Parallel Paths The either/or situation: choosing one path or the other based on a condition. if - else true false Condition?

if – else … if (condition) { instructions that are to be executed when the condition is true.... } else { instructions to be executed when the condition is false } instructions after merging of both branches

More than two paths When more than two alternatives exist for a condition, we go through the list of possibilities. Elminating them one- by-one true Condition2 false true Condition1 Condition3

if – elseif if (condition1) { instructions to be executed if condition1 is true.... } elseif (condition2) { instructions to be executed if condition1 is false but condition2 is true } elseif (condition3) { instructions to be executed if all conditions above are false and condition3 is true } else { instructions to be executed if all conditions are false } instructions after merging of all branches

A different “condition testing”.if – elseif command allows us to test conditions based on boolean value (ture or false) The switch statement allows us to test for various values of a particular variable type value4value1value2value3 test

switch switch (variable) { case value1: instructions; break; case value2: instructions; break; case value3: instructions; break; case value4: instructions; break; default: instructions; }

Repeating a process Often a series of commands need to be repeated Types of repetition: –An application itself is a mecahnism for repeating commands. –Function/method calls are repetitons based on demand –Loops: A certain number of repetitons Repetition until a condition is met

Loops truefalse Condition? true false Condition?

while while (test condition) { body of loop; } --- int i=0; while (i<10) { System.out.println(i); i++; }

do - while Do { body of loop; } while (test condition) ; --- int i=0; do { System.out.println(i); i++; } while (i<10) ;

for for (initialization[s]; test condition; counter update[s]) { body of loop; } for (int i=0; i<10; i++) { System.out.println(i); }

break - continue break –For immediate exit out of any loop body –Only one level exit in case of nested loops continue –To start the next iteration of the loop immediately. –Only one level If there is a need to go beyond one level, use “labels”.

break - continue while (true) { //sonsuz döngü komutlar... ; if (istisnai bir şart) { break; //döngüden çıkış } komutlar; }

Assignment 4 Write a class named ‘Assignment4’ that implements the following static methods. –static boolean isEven(int number) if number is even, return true, otherwise return false –static String resolveAreaCode (int code) Return the name of the area for the area codes below: 212: İstanbul-1; 216: İstanbul-2; 312: Ankara; 232: İzmir; 322: Adana; 266: Balikesir; 224: Bursa; 462: Trabzon; 532: Turkcell; 542:Vodafone; 505-Avea; other codes: Unknown_code (you are expected to use the switch statement)

Assignment 4 –static void fibonacciSeries(int n) Display the first n numbers in the Fibonacci sequence (The sequence of numbers that start with 0 and 1 and where each number thereafter is the sum of the two preceeding numbers.) –static void fibonacciUntil (int max) Display the fibonacci sequence that ends with the first number greater than max. –static void squares(int n) Display the squares of the first n integers starting with 0. (0 2,1 2,2 2,3 2,...) –static void squaresUntil(int max) Display the squares of integers that end with the first square greater than max.

Assignment 4 –static void randomTest() The Math.random() method generates a real number n (of type double) where, 0.00 <= n < Using this method, generate 1000 integers between 50 and 150 and display their average. –static void countdown(int n) Display integers (in reverse order) from n down to 0.

Assignment 4 –Write a class named ‘Assignment4App’ It should test all the static methods of the Assignment4 class and display the outputs on screen.

Next Week Preperation: Thinking in Java Chapter 4-6