Preview

For Loop

Good Essays
Open Document
Open Document
534 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
For Loop
Java For Loops
We'll start with For Loops, one of the most common types of loops. The "For" part of "For Loop" seems to have lost its meaning. But you can think of it like this: "Loop FOR a set number of times." The structure of the For Loop is this: for ( start_value; end_value; increment_number ) {
//YOUR_CODE_HERE
}

While Loops
Another type of loop you can use in Java is called the while loop. While loops are a lot easier to understand than for loops. Here's what they look like: while ( condition ) {
}
So you start with the word "while" in lowercase. The condition you want to test for goes between round brackets. A pair of curly brackets comes next, and the code you want to execute goes between the curly brackets. As an example, here's a while loop that prints out some text (Try the code out for yourself): int loopVal = 0; while ( loopVal < 5) {
System.out.println("Printing Some Text"); loopVal++; }

The condition to test is between the round brackets. We want to keep looping while the variable called loopVal is less than 5. Inside of the curly brackets our code first prints out a line of text. Then we need to increment the loopVal variable. If we don't we'll have an infinite loop, as there is no way for loopVal to get beyond its initial value of 0.
Although we've used a counter (loopVal) to get to the end condition, while loops are best used when you don't really need a counting value, but rather just a checking value. For example, you can keep looping while a key on the keyboard is not pressed. This is common in games programme. The letter "X" can pressed to exit the while loop (called the game loop), and hence the game itself. Another example is looping round a text file while the end of the file has not been reached. Do ... While
Related to the while loop is the do … while loop. It looks like this: int loopVal = 0; do {
System.out.println("Printing Some Text"); loopVal++; } while ( loopVal < 5 );
Again, Java will loop

You May Also Find These Documents Helpful

  • Satisfactory Essays

    a1 script win213

    • 380 Words
    • 2 Pages

    If the user does not enter a numeric value the program should prompt for a numeric value…

    • 380 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    5. In process window select check syntax for checking the syntax shown in figure 5. After successful completion of check syntax generate syntax report in…

    • 419 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    PT1420 Unit 8 Lab 8

    • 365 Words
    • 2 Pages

    Calculating a Running Total, page 201): Asks for input of a number 5 times then adds all numbers into one total sum, example 2,4,6,8,10 = 30…

    • 365 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework Unit 3

    • 354 Words
    • 2 Pages

    4. AND operator- combines more than one condition in a program. Makes a compunf Boolean operation.…

    • 354 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 7 assignment 1

    • 431 Words
    • 2 Pages

    3. What is a condition-controlled loop? A condition-controlled loop uses a true/false condition to control the number of times it repeats.…

    • 431 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Unit 3 network discovery

    • 1031 Words
    • 5 Pages

    The shell tries to find an executable program with the same name as the command. When it does, the shell executes the program. When finishing the command put an & ampersand symbol at the end of the command and the command will run in the background. Then a cursor will be displayed immediately to allow the next command to be entered.…

    • 1031 Words
    • 5 Pages
    Powerful Essays
  • Satisfactory Essays

    CEIS100 W5 ILab Report

    • 320 Words
    • 2 Pages

    In conclusion the code I entered was successful. I did not expect coding to be based more off the human language as much as it is. However the smallest mistake can create a world of trouble when writing code. I am still undecided if I like the IDLE tool or not. So far notepad++ is my choice for writing code with the python shell. Coding can be a long process but as long as you can remember the language and terminology such as format, method, variables, indexes and slices etc… then it appears to be less difficult.…

    • 320 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Nt1310 Unit 1

    • 4209 Words
    • 17 Pages

    If two steps are indented at the same level, the uppermost statement is executed first. ans)All of the above…

    • 4209 Words
    • 17 Pages
    Powerful Essays
  • Satisfactory Essays

    /*Class. Project 1*/ import java.io. BufferedReader; import java.io. FileReader; import java.util. StringTokenizer; import java.io.…

    • 779 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Prg 211

    • 393 Words
    • 2 Pages

    An if statement is basically a single block of instructions where the test condition have been met, then the statements will be executed sequentially. Otherwise, the statements are skipped over if the test condition failed.…

    • 393 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    COS wa 10

    • 297 Words
    • 2 Pages

    Create a set/list of enumerated constants called week that contains the days of the week. Have a variable called today that is of type week. Assign a value to today. If the day is Monday through Friday, print "Go to work!" If the day is Saturday or Sunday, print "You can rest today!"…

    • 297 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    IT Level 3

    • 1133 Words
    • 6 Pages

    The program tells us how to multiply the numbers that are put above, e.g. found coins = 20, magic coins =10 and stolen coins=3 which are from lines 1-3 and line4 shows us a question which consists of those numbers. Line 5 shows us that stolen coins value is now 2 instead of 3 and line7 shows us that magic coins has also changed its value from 10 to 13…

    • 1133 Words
    • 6 Pages
    Satisfactory Essays
  • Good Essays

    Psychology Key Terms

    • 540 Words
    • 3 Pages

    algorithms - An algorithm is a set of instructions for solving a problem or completing a process…

    • 540 Words
    • 3 Pages
    Good Essays
  • Good Essays

    9. Run the script again using the at command to schedule the job in the future. Wait for the job to run to make sure it executes.…

    • 268 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    The systems is controlled without feedback (without knowing what is the real output). There is no loop,…

    • 709 Words
    • 8 Pages
    Powerful Essays