The number of times the code is repeated is typically

Info icon This preview shows pages 4–7. Sign up to view the full content.

View Full Document Right Arrow Icon
- The number of times the code is repeated is typically dependent on some data. - We need a way to group the statements that are to be repeadted and a way to control how many times they are repeated. - Kinds of Loops While-Loop: Repeats a body of code as long as a boolean condition remains true. Do-While-Loop: Similiar to while-loop, but checks the boolean condition after executing the body of code. For-Loop: Counts each time an interaction of the body code is executed For-Each-Loop: - The simplest loop statement is java is the while loop: while (boolean expression) { // code to be repeated } *The code is repeated as long as the boolean is true - Var--; OR Var -=1; (Removes 1 from variable) - 4 Things you need to do 1) Figure out what needs to be repeated and what variables need to be changed 2) Write code to give initial values to those variables that will be changing 3) Figure out when the loop will stop 4) Make sure the some variable changes in the repeated code that eventually makes the boolean expression become false - EXAMPLE Use a while-loop to read from console I/O until the word "STOP" is read or there is nothing left to read |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| import java.util.Scanner; public class Week8 { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Enter something"); String word = scan.next(); System.out.println("Thank you!"); System.out.print("Enter Something:"); while (!word.equalsIgnoreCase( "STOP" )){ word = scan.next(); System.out.println("Thank you!"); System.out.print("Enter Something:"); } } } |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| public class Week8 {
Image of page 4

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
public static void main(String[] args){ int sum = 0; int count = 0; int stop = 100; while (count<=100){ if (count %3 ==0){ sum += count; } count++; } System.out.println("sum = " + sum); } } init; do { //body } while () for (init; test; step) { //body } * for(int i = 1; 1<= 100; i+=2){ System.out.println(i); } |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ------------------------- Do-While Loops 10/25/2017 - init is a statement that is executed once before anything else - step is a statement that is executed agfter the body ||||||||||||||||||||||||||||||||||||||||||||||||||||| import java.util.Scanner; public class Guess { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String answer = ""; do{ System.out.println("Enter Something: "); answer = scan.next(); } while (answer.length()!=3); } } |||||||||||||||||||||||||||||||||||||||||||||||||||||| For Loops - A for loop is another way of specifying a loop in java. - It is a little more complex than a while loop, but everything you can do with a for loop you can always fo with a while loop - Structure for (init;test;step) { //body //(i.e., reapeted stuff) } - Test is a boolean expression like a while loop - Equivilant to init;
Image of page 5
while (test) { //Repeated stuff step; } |||||||||||||||||||||||||||||||||||||||||||||||||| public class Numbers { public static void main(String[] args) { for (int i = 1; i<=100; i += 2){ System.out.println(i); } } } ||||||||||||||||||||||||||||||||||||||||||||||||| -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- - Modularity/Methods 11/6/2017 Software Engineering - The study of the techniques and theory that underlie the development of high-quality software - Right Solution? Break problem into smaller problems, testing, troubleshoot and find problems that are left, measure, consider alternative - Software Quality Characteristics Correctness - The degreee to which software adheres to its specific requirements Reliability - The frequency and critically of software failure Robustness - The degree to which erroneous situations are handled gracefully.
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern