Answers for "how to make a basic for loop in java"

31

for loop java

// Starting on 0:
for(int i = 0; i < 5; i++) {
  System.out.println(i + 1);
}

// Starting on 1:
for(int i = 1; i <= 5; i++) {
  System.out.println(i);
}


// Both for loops will iterate 5 times,
// printing the numbers 1 - 5.
Posted by: Guest on February-18-2020
20

Java for loop

int values[] = {1,2,3,4};
for(int i = 0; i < values.length; i++)
{
 	System.out.println(values[i]); 
}
Posted by: Guest on November-17-2019
5

how to make a basic for loop in java

package myForLoops

public class forLoop {
	public static void main(String[] args) {
    
		for (int i = j; i<=k; i++)  
        //variable "i" is the value checked in loop
        //variable j is the starting value for i
        //variable k is where we want the loop to end
        //i++ adds 1 to i each iteration of the loop until i == k
		{
        	//displays "i"
			System.out.println(i);
		}
        
        //this is a for loop in use
        for (int i = 1; i<=10; i++) {
        	System.out.println(i); //i will be displayed 10 times being 
            //increased by 1 every time
        }
        
        for (char ch = 'k'; ch<= 'q'; ch++) {
        	//this is a for loop with characters, it will
            //display every character from 'k' to 'q'
        	System.out.println(ch);
        }
        for (int i = 10; i>=1; i++) {
        	//this loop will be infinitely run since i 
            //will always be greater than 1, we do not want this
        	System.out.println(i);
        }
        for (int i = 10; i>=1; i--) { 
        	//this is the correct way of doing the previous code 
            //(if you do not want an infinite for loop)
        	System.out.println(i);
        }
	}
}
Posted by: Guest on June-15-2021
-1

for loop in java example complete programs

for loop
Posted by: Guest on April-22-2021

Code answers related to "how to make a basic for loop in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language