Control Statements Lecture 6 from Chapter 5.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Introduction to C Programming
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
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.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Statement-Level Control Structures Sections 1-4
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120: Introduction to Computer Science Decision Making in Programs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Application development with Java Lecture 6 Rina Zviel-Girshin.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch 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.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 6: Loops.
Control Structures.
Engineering Problem Solving with C++, Etter/Ingber
Branching Constructs Review
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecturer CS & IT Department UOS MBDIN
Programming Fundamentals
The University of Texas – Pan American
Conditional Statements
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
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.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
By Hector M Lugo-Cordero September 3, 2008
PROGRAM FLOWCHART Iteration Statements.
Additional control structures
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Control Statements Lecture 6 from Chapter 5

Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It is because one can be confident that 'outsiders' are not relying on too many details. It has definition Creating a module to contain some algorithm or data structure, thus hiding its details behind the module's interface. http://www.csi.uottawa.ca:4321/java/index.html#nestedif-elsestatement

3 principles of OOP- Inheritance and Polymorphism Inheritance: It is a method for extending existing classes by adding methods and fields. Polymorphism: The behaviour varies depending on the actual type of an object.

Review Selection Statements Iteration Statements Jump Statements If and switch Iteration Statements While, do-while, for and nested loops Jump Statements Break, continue and return

Selection Statements - If If (condition) statement1; else statement2 If condition is true, it will execute statement1 else statement2. Example int a = 3, b = 4; if (a >b) a =1; // since a>b is false, it else b = 1 // will set b = 1

Selection Statements – Nested Ifs It is an if statement that is the target of another if or else. Example int a = 3, b = 4, c = 5; if (a >b) { //a >b if (b > c) b = 1; //b > c, b = 1 else c = 1; //b < c, c = 1 } else a =1; // b > a, a = 1

Example result

Example result

Selection Statement - switch It is a multiway branch statement. It is similar to if-else-is, but is more well organized. Switch (expression) { Case value1: //statement1 Break; Case value2: //satement2 Break … Default; //default statement } if expression is value1, it will jump to statement 1 until it hits break

Example – three cases

Example with random value the value is random

Example with advanced Switch the value is random

Iteration Statements While Do-while For Nested loop

Iteration Statements - While while(condition) { // statements to keep executing while condition is true .. .. } Example //Increment n by 1 until n is greater than 100 while (n > 100) { n = n + 1; }

Example – while result

Iteration Statements – do while Do { // statements to keep executing while condition is true } while(condition) It will first executes the statement and then evaluates the condition. Example int n = 5; Do { System.out.println(“ n = “ + n); N--; } while(n > 0);

Example – difference between while and do-while i = 1: no difference i = 6: difference

Iteration Statements – for for(initializer; condition; incrementer) { // statements to keep executing while condition is true }   Example int i; int length = 10; for (i = 0; i < length; i++) { . . . // do something to the (up to 9 ) . . . }  

args[0]: Peter args[1]: Suana args[2]: John Example – for-loop args[0]: Peter args[1]: Suana args[2]: John

Multiple Initializers and Incrementers Sometimes it's necessary to initialize several variables before beginning a for loop. Similarly you may want to increment more than one variable. Example for (int i = 1, j = 100; i < 100; i = i+1, j = j-1) { System.out.println(i + j); }

Nested Loop – Java allows loops to be nested

Modification of nested loop j depends on i

Jump Statement Three jump statements Break, continue and return

Break and Continue with the nested loop result: j 1 and 2) only break

Break as a form goto A break statement exits a loop before an entry condition fails. The break statement can also be employed by itself to provide a form of “goto”. The format is: break label:

Example – use break to exit from loop break once it becomes negative

Continue - Explanation Sometimes it is necessary to exit from the middle of a loop. Sometimes we want to start over at the top of the loop. Sometimes we want to leave the loop completely. For Example for (int i = 0; i < length; i++) { if (m[i] % 2 == 0) continue; // process odd elements... } it will process the odd case

Example - continue 1, 3, 5, 7 are missing

Return It has purpose to exit from the current method It jumps back to the statement within the calling method that follows the original method call. Example return expression

Example

Summary Selection Statements Iteration Statements Jump Statements If and switch Iteration Statements While, do-while, for and nested loops Jump Statements Break, continue and return