Java for/whie/Do-While loop - Neuroon Networks

Breaking

Thursday, March 1, 2018

Java for/whie/Do-While loop

     Hello guys. I hope you Guys have read my previous tutorials. Today I'm going to discuss about the major loops in the programing. 


1) For loop

     The for-loop has four parts -- init, test, increment, and body -- separated by semi-colons (;) The for-loop is used specifically to step a variable through a sequence of values, such as 0, 1, 2, 3, ..9 In fact, most for-loops adhere to that pattern so closely, that they will look similar to the 0..99for-loop code below.  



    • Init 
     The init code runs once to set things up at the very start of the loop. Typically, this looks like "int i = 0", declaring a variable to use for the counting, and setting its start value.

    • Test

     The boolean test is evaluated. If it is true the loop body will execute (green light, just like a while-loop). Typically, the test is a boolean expression such as "i<100", showing what should be true for the loop to continue.

    • Loop-body
    If the test was true, the body runs once. The body is a series of statements, just like in a while-loop. The loop body will very often make some use of the loop variable, such as "i" in this example.

    • increment or decrement
    In this case, the increment is "i++" which increments the variable "i" by one(i-- decrements the variable i by one) . Since "i" is the common variable choice for a for-loop, it is very common for the increment to be "i++". It is idiomatic to write the increment as "i++" as opposed to the similar "i = i + 1".





2) while loop


      Since the while-loop checks the test before every iteration, it is possible for the body to not run even once. While loop is consists of three parts.

    • test
      Condition that says that each iteration can go ahead or can't go head.

    • work
      code in the body that does something for each iteration such as printing or drawing something. Some loops do not have any identifiable work in the body.

    • increment or decrement
      code in the body that advances things so we are closer to making the test false. At the end of the body, the code will loop around to the top of the body





3) Do-While loop

             The do-while loop executes a block of code while a boolean expression evaluates to true. It checks the boolean expression after each iteration. It terminates as soon as the expression evaluates to false. Do-while loop evaluates the boolean expression at the end of the iteration, the block of code within the loop is guaranteed to run at least once.



Fact given below is common for every loop.
  • boolean Expression results in either a true or false output. It is created using comparing operators (==, >, =, <=, !=).
  • There can also be multiple boolean expressions within the parentheses (boolean Expression). The boolean expressions are connected through logical operators (&&, ||, !).
Thats all about this tutorials. Write some new codes using loops and if else statements.

See you guys in next tutorials 

No comments:

Post a Comment