Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
UNIT II Decision Making And Branching Decision Making And Looping
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
DAT602 Database Application Development Lecture 5 JAVA Review.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Java Language Basics.
Chapter 4 Repetition Statements (loops)
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Loop Control Structure.
Looping and Repetition
Outline Altering flow of control Boolean expressions
LRobot Game.
Java Language Basics.
Repetition Control Structure
Chapter 6: Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Looping and Repetition
Presentation transcript:

Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent Room C7

Control Flow Statements The statements inside your source files are generally executed from top to bottom Control flow statements, break up the flow of execution by employing decision making, looping, and branching

Control Flow Statements Decision-making statements if-then if-then-else switch Looping statements for While do-while Branching statements break continue return

Why Looping? To automate the repetition of instructions. To iterate through data and test for certain condition To keep attempting for some operation (such as obtaining data from a remote computer over a network)

Loops: for Statement for statement provides a compact way to iterate over a range of values. Repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: keep in mind that: initialization expression initializes the loop; it's executed once, as the loop begins. When the Condition is checked before each iteration through the loop. When it evaluates to false, the loop terminates. increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value. for (initialization; Condition; increment) { statement(s) }

Loops: for Statement Initialization Condition Evaluation Statement (s) Increment true false for (int i=0; i<2; i++){ Statement (s); } i = 0 i = i = 2

for Statement The output of this program is: Count is: 1 Count is: 2 Count is: 3 Count is: 4 class ForCount { public static void main(String[] args){ for(int i=1; i<5; i++){ System.out.println("Count is: " + i); }

while Statement The while statement continually executes a block of statements while a particular condition is true. The expression must return a boolean value The while statement continues testing the expression and executing its block until the expression evaluates to false while (expression) { statement(s) }

while Statement Condition Evaluation Statement (s) true false boolean found = false; while (!found) { //code to search for a value in list //set found = true to exit } The statement(s) is executed over and over until the condition becomes false statement(s) is executed Zero or more times

while Statement class Count { public static void main(String[] args){ int count = 1; while (count < 5) { System.out.println("Count is: " + count); count++; } System.out.println(“Outside while loop"); }//main end }//class end The output of this program is: Count is: 1 Count is: 2 Count is: 3 Count is: 4 Outside while loop

do-while Statement The while statement continually executes a block of statements while a particular condition is true. The expression must return a boolean value do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once do { statement(s) }while (expression)

do-while Statement Condition Evaluation Statement (s) true false boolean exit = false; do { statement(s) //set exit = true to exit } while (!exit); The statement(s) is executed over and over until the condition becomes false statement(s) is executed at least once

do-while Statement class Count { public static void main(String[] args){ int count = 10; do{ System.out.println("Count is: " + count); count++; } while (count < 5) System.out.println(“Outside while loop"); }//main end }//class end The output of this program is: Count is: 10 Outside while loop

Infinite Loops infinite loop, will execute until the user interrupts the program. This is a common type of logical error. always double check your loop condition. for(int i = 2; i > 1; i++){ System.out.println("Count is: " + i); }

Notes If what you really want is to execute the loop 10 times, write the condition Number < 10 and not as Number <= 9 In general, specific values such as "10" should not appear within the body of your program. You should declare them as finals at the top of the program static final int COUNT = 10; In Java generally you would more likely want to loop not from 1 to 10, but from 0 to 9. All counting in Java tends to start at zero rather than one.

Course Work 2.1 Notes 11 marks for correct execution and meeting all requirements 4 marks on the Quality of your code Follow a logical Structure Define local Variables at the start of main method Use constants when needed Correct indentation Good use of comments

Summary Control Flow Statements Looping statements for While do-while