Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

4 Control Statements.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (Repetition)
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
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
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 – C Program Control
Chapter 5: Control Structures II
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
JavaScript: Control Statements.
Chapter 5- part 2 Control Statements: Loops 2
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
3 Control Statements:.
Chapter 5 Control Statements: Loops 2
Chapter 2.2 Control Structures (Iteration)
Chapter 6 Control Statements: Part 2
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Chapter 4 - Program Control
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements

 Essentials of Counter-Controlled Repetition  while Repetition Statement  Example: The average problem  for Repetition Statement  Example: Summing the Even Integers from 2 to 20  do...while Repetition Statement  break and continue Statements  Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra

 A repetition statement (also called a looping statement or a loop) allows the programmer to specify that a program should repeat an action while some condition remains true. 3 Presented & Prepared by: Mahmoud R. Alfarra What is repetition statement ? While there are more items on my shopping list Purchase next item and cross it off my list

 Counter-controlled repetition requires: 1. A control variable (or loop counter) 2. The initial value of the control variable 3. The increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop) 4. The loop-continuation condition that determines whether looping should continue. 4 Presented & Prepared by: Mahmoud R. Alfarra Essentials of Counter-Controlled Repetition

 A program can test multiple cases by placing if...else statements inside other if...else statements to create nested if...else statements. 5 Presented & Prepared by: Mahmoud R. Alfarra While Repetition Statement int x = number; while (Condition) { // actions x--; } int x = number; while (Condition) { // actions x--; } Initial variable The loop-continuation condition, Can be any other operators Has a close relation with the operators and the initial value of x

6 Presented & Prepared by: Mahmoud R. Alfarra Be care Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the loop never terminates. Set a semi colon after the condition results a logic error

7 Presented & Prepared by: Mahmoud R. Alfarra While Repetition Statement true false Actions Condition Start End  The while loop is used when you want to test a condition before entering into the loop.  The while loop is considered a pretest loop, this allows you to stop entering the loop even once if the condition is not true.

 A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. 8 Presented & Prepared by: Mahmoud R. Alfarra Example: The average problem Write the pseudo code and flowchart of the above example HW 8.1

9 Presented & Prepared by: Mahmoud R. Alfarra Example: The average problem

 If you want to execute a certain block of code a specified number of times, use a for loop.  The syntax of the for loop is as follows: 10 Presented & Prepared by: Mahmoud R. Alfarra for Repetition Statement

Presented & Prepared by: Mahmoud R. Alfarra11 How to perform repetition using for ? System.out.println (counter * 10); System.out.println (counter * 10);

12 Presented & Prepared by: Mahmoud R. Alfarra Be care Using an incorrect relational operator or an incorrect final value of a loop counter in the loop-continuation condition of a repetition statement can cause an off-by-one error. Using commas instead of the two required semicolons in a for header is a syntax error. When a for statement's control variable is declared in the initialization section of the for's header, using the control variable after the for's body is a compilation error. Placing a semicolon immediately to the right of the right parenthesis of a for header makes that for's body an empty statement. This is normally a logic error.

 Use a for statement to sum the even integers from 2 to 20 and store the result in an int variable called total. 13 Presented & Prepared by: Mahmoud R. Alfarra Example: Summing the Even Integers Write the pseudo code and flowchart of the above example HW 8.2

14 Presented & Prepared by: Mahmoud R. Alfarra Example: Summing the Even Integers Not using the proper relational operator in the loop- continuation condition of a loop that counts downward (e.g., using i = 1 in a loop counting down to 1) is usually a logic error.

15 Presented & Prepared by: Mahmoud R. Alfarra  A person invests $1,000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to determine the amounts: where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., use 0.05 for 5%) n is the number of years a is the amount on deposit at the end of the nth year. Home Work HW 8.3 a = p(1+r) n

 If you would prefer that the testing of the loop condition is done after executing the loop’s code, you would use the post-test version of the while loop, called the do … while loop. 16 Presented & Prepared by: Mahmoud R. Alfarra do...while Repetition Statement Using do.. While statement, the body always executes at least once, but using while statement does not.

17 Presented & Prepared by: Mahmoud R. Alfarra do...while Repetition Statement int x = number; do { // actions x--; } while (Condition) int x = number; do { // actions x--; } while (Condition) Initial variable The loop-continuation condition, Can be any other operators Has a close relation with the operators and the initial value of x

18 Presented & Prepared by: Mahmoud R. Alfarra do...while Repetition Statement true false Actions Condition Start End

19 Presented & Prepared by: Mahmoud R. Alfarra What is the difference between them ?  a pretest condition  can be executed for 0 or more times  does not end with semicolon !!!  a pretest condition  can be executed for 0 or more times  does not end with semicolon !!!  a posttest condition  can be executed for 1 or more times  must end with semicolon !!!  a posttest condition  can be executed for 1 or more times  must end with semicolon !!! while do…while while (condition) { // actions } while (condition) { // actions } do { // actions } while (condition); do { // actions } while (condition);

 Java provides statements break and continue to alter the flow of control. 20 Presented & Prepared by: Mahmoud R. Alfarra break and continue Statements. break;. break; Before loop’s block After loop’s block. continue ;. continue ; Before loop’s block After loop’s block

 The break statement, when executed in a while, for, do...while or switch, causes immediate exit from that statement.  Execution continues with the first statement after the control statement.  Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch. 21 Presented & Prepared by: Mahmoud R. Alfarra break Statement

22 Presented & Prepared by: Mahmoud R. Alfarra Example: break Statement All the iterations are completed from 1 to 4 and in 5’th iteration the loop is broken

 The continue statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. 23 Presented & Prepared by: Mahmoud R. Alfarra continue Statement In while and do...while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.

24 Presented & Prepared by: Mahmoud R. Alfarra Example: continue Statement All the iterations are completed except iteration number 5

قال بن تيمية: الإسناد من الدين و لولا الإسناد لقال من شاء ما شاء 25 Presented & Prepared by: Mahmoud R. Alfarra

Practices 26 Presented & Prepared by: Mahmoud R. Alfarra