foreach loop executes a predetermined number of times eg. list of items, rows in a table, etc.
a for loop executes until a certain condition is met
The do loop is similar to the forloop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.
#include<stdio.h> int main () { int odd=1; int count=0; while (count++<10) { printf (%d\n", odd); odd+=2; } return 0; }
//Both loops will print out the alphabet {A-Z} int i = 65; while(i<65+26) { System.out.println((char)i); i++; } ... for(int i=65; i<65+26; i++) System.out.println((char)i); As you can see, both loops accomplish the same thing. A while loop simply has a conditional statement that tells the loop what needs to be true for it to keep on looping. A for loop is more concise: it has 3 parts: a starting value, a conditional statement, and a(n) action for what the loop should do after every iteration (in this case increase i by 1).
Here's a simple Java program that uses a for loop to generate the first 10 multiples of the first 10 natural numbers: public class Multiples { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.print("Multiples of " + i + ": "); for (int j = 1; j <= 10; j++) { System.out.print(i * j + " "); } System.out.println(); } } } This program iterates through the first 10 natural numbers and prints their multiples from 1 to 10.
final int limit = 1000000; // Print evens... for(int i = 0; i <= limit; i += 2) { System.out.print(i + " "); } // Print odds... for(int i = 1; i <= limit; i += 2) { System.out.print(i + " "); }
Sure. (You could have tried it yourself.)
For loop is utilize to do any specific work for specific number of times For example to print hello on computer screen 10 time we use For (start = 1; end = 10; start++) count<<"hello"; next
The do loop is similar to the forloop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.
#include<stdio.h> int main () { int odd=1; int count=0; while (count++<10) { printf (%d\n", odd); odd+=2; } return 0; }
//Both loops will print out the alphabet {A-Z} int i = 65; while(i<65+26) { System.out.println((char)i); i++; } ... for(int i=65; i<65+26; i++) System.out.println((char)i); As you can see, both loops accomplish the same thing. A while loop simply has a conditional statement that tells the loop what needs to be true for it to keep on looping. A for loop is more concise: it has 3 parts: a starting value, a conditional statement, and a(n) action for what the loop should do after every iteration (in this case increase i by 1).
Here's a simple Java program that uses a for loop to generate the first 10 multiples of the first 10 natural numbers: public class Multiples { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.print("Multiples of " + i + ": "); for (int j = 1; j <= 10; j++) { System.out.print(i * j + " "); } System.out.println(); } } } This program iterates through the first 10 natural numbers and prints their multiples from 1 to 10.
final int limit = 1000000; // Print evens... for(int i = 0; i <= limit; i += 2) { System.out.print(i + " "); } // Print odds... for(int i = 1; i <= limit; i += 2) { System.out.print(i + " "); }
String num = Integer.toString(12345); // Insert your Integer here. int sum = 0; for(int i = 0; i < num.length(); i++){ sum += Character.digit(num.charAt(i), 10); } System.out.println(sum); // Simply prints the sum of the digits to standard out. ///// ALTERNATE SOLUTION: int num = 12345; // Insert your Integer here. int sum = 0; while (num > 0){ sum += num % 10; num /= 10; } System.out.println(sum); // Simply prints the sum of the digits to standard out.