• A.razaq

T13- for loop

in Tutorial number 10 https://www.exological.com/post/t10-if-if-else-statement previously we discussed selection and repetition structures

in this tutorial we will cover the repetition structures and the basics of it.


in java we have three repetition structures

  1. for loop

  2. while loop

  3. do..while loop

lets begin with ( for loop ).


what is repetition structures of statement ?

  1. A repetition statement allows you to specify that a program should repeat an action.

  2. Loops can execute a block of code as long as a specified condition is reached.

  3. Loops can save time, reduce errors, and they make the code more readable.




so as we understood from above, we use the loop statement to repeat the code execution until a condition is met ( true ).


so lets begin first with the ( for loop ) and understand its structure.


for loop syntax :


for (part 1 ; part 2 ; part 3)
 {
 // code to be executed
 }
 
  1. part 1 : is called a control variable or ( loop counter ) it set a variable value before the loop start as a beginning for example : (int i =1) so that means the loop will start from 1

  2. part 2 : the condition for the loop , for example : ( i must be less than 10 ) if i <10 less than 10, the condition is met so the code will execute until ( i is not less than 10 ) and the loop will end.

  3. part 3 : is for increment, it increase the value every time the code is executed.

example of a for loop :



for (int i = 0; i < 5; i++) {
			  System.out.println("hello, this is a loop example");

	}
  • as we explained above , we begin in part 1 with a variable so in this example variable value is =0 so the loop will start from 0

  • i<5 is our loop condition.

  • i++ is for increment , it increase the value by 1 until the condition is met , so the loop start from 0 then it becomes 1 then 2 until it becomes 5 , this is done by the increment.

lets run this code and see the result


as you see the code to print the message was executed 5 times because our condition was i<5 , if we make the condition i<10 it will execute the code

and print the message 10 time until (i) is no longer < 10

-------

now you understand why for loop can make your coding easy , imagine you want to type this message 10 times or 20 or even 1000 times

do you think its practical to type this 20 times ?


System.out.println("hello, this is a loop example");
System.out.println("hello, this is a loop example");
System.out.println("hello, this is a loop example");
System.out.println("hello, this is a loop example");
System.out.println("hello, this is a loop example");

or 1000 time ?

you can make this code execute as how many times you one by using the loops and this is how they become useful.


now lets try another example :




	for (int i = 0; i < 10; i++) {
			  System.out.println("value of i now is " + i);

	}

this example is the same as the previous one but it will make you see how the loop works better

lets run the program and see the result





as you see , the code


System.out.println("value of i now is " + i);

will print the message ( value of i now is ) + (i) variable value

until the condition is met which is (i < 10)

so you can see once (i) reaches 10 the condition is no longer met and the code will stop .

9 views0 comments

Recent Posts

See All