Repetition Control Structure PDF
Document Details
Uploaded by FavoriteWillow
New Era University
null
null
Tags
Summary
This document explains repetition control structures in Java, including while, do-while, and for loops. It provides examples of how these structures can be used to write programs that simulate activities in the real world and offers learning outcomes.
Full Transcript
New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: computerstudies...
New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: [email protected] Week 10-12 REPETITION CONTROL STRUCTURE In this module, you will learn about loop statements in Java, as well as techniques for writing programs that process input and simulate activities in the real world. Learning Outcomes: At the end of the module, the student should be able to: 1. Understand how looping statement shorten codes on repetitive tasks; 2. Use do-while, while and for loop statements; 3. Simulate program with looping statement; 4. Construct program with looping statement within another looping statement. Looping Looping or repetition control structure is Java statements that allows to execute blocks of code in a number of times. A looping statement have three types. The while, do- while and for loops. On a normal approach of the beginner in programming, there are some cases, for example you need to display “I love you!” 3 times. A beginner who is just learning a program will use 3 System.out.println(“I love you!”); to meet the requirements. The program will looks like this: How about if the requirements are to display 50 or 100 “I love you”? Are the programmer write 50-100 System.out.println(“I love you!”);? Inefficient approach right? That is why there are looping statements to address those issues. New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: [email protected] The while Statement The while loop is a statement or block of statements that is repeated as long as the condition is satisfied. The while statement has the following syntax: In general, looping statement has 3 parts. The first part is the declaration of a variable or a counter to control the number of times the loop executes. The while loop statement test the condition (the second part) before executing the statement/s under the statement block. The statement/s inside the while loop including the update (the last part of a loop which is to increment or decrement the variable) are executed as long the as the condition is true. After executing the statement/s inside the block, it will loop back to the while condition and test again. If the condition turns out to false, it will disregard the statement block then it immediately proceed to outside the block. Let us now apply the while loop in the ILoveYou program: From the example source code, let’s study how the looping occurs in the program: New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: [email protected] The ctr was declared to be our control variable in our program and has an initial value of zero. The next step will evaluate the while condition. In the condition, it will ask if the value of ctr is less than 3. Since, ctr=0, it is less than 3, the while condition turns out to true and it will execute the statement System.out.println("I love you!");, and display the message "I love you!" then update by incrementing by 1 the value of ctr. The variable ctr now, has 1 value. Once it reach the closing curly brace of while, it will force to loop back and evaluate again the while condition. Since the present value of ctr=1 and still less than 3 (1=1?) 3 I love you! 2 t I love you! 1 t I love you! 0 f There is no True or False result here because the while condition has not been encountered New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: [email protected] Assessment 2: CHECK THE ASSESSMENT MODULE New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: [email protected] The for statement The for looping statement is similar to while statement that allows execution of statements under the block of statement as long as the condition is satisfied , and it has the following syntax: Here are a few examples on how for loop works: Next is a for loop statement but in a while statement format: Based on the program ForILoveYou1, let us try to analyze how this for loop works. Step 1: The first statement under the for loop statement that will be evaluated is the ctr=0; which is the assignment of the variable/identifier ctr. New Era University College of Computer Studies Rm. 247-B, High School Annex B, New Era University Tel. No.: (+632) 981-4221 loc 3825 E-mail: [email protected] Step 2: From reading of the assigned variable, it will proceed to the for condition which is the ctr