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.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Introduction to C Programming
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
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.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Statement-Level Control Structures Sections 1-4
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
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,
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
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,
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
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.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
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.
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.
Chapter 6: Loops.
Unit-1 Introduction to Java
Loops in Java.
Control Structures.
Engineering Problem Solving with C++, Etter/Ingber
Control Statements Lecture 6 from Chapter 5.
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecturer CS & IT Department UOS MBDIN
Programming Fundamentals
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
The University of Texas – Pan American
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
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.
PROGRAM FLOWCHART Iteration Statements.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

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 is because one can be confident that 'outsiders' are not relying on too many details. code It has definition Creating a module to contain some algorithm or data structure, thus hiding its details behind the module's interface.

3 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.

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

5 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

6 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

7 Example

8

9 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 }

10 Example – three cases

11 Example with random value

12 Example with advanced Switch

13 Iteration Statements While Do-while For Nested loop

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

15 Example – while

16 Iteration Statements – do while Do { // statements to keep executing while condition is truecondition true } while(condition)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);

17 Example – difference between while and do-while

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

19 Example – for-loop

20 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); }

21 Nested Loop – Java allows loops to be nested

22 Modification of nested loop

23 Jump Statement Three jump statements Break, continue and return

24 Break and Continue with the nested loop

25 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:

26 Example – use break to exit from loop

27 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... }

28 Example - continue

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

30 Example

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