فيما يلي امثلة على الحلقات باستخدام جافا
for loops
مثال: برنامج يطبع كلمة hello ل100 مرة :
//write java program to print hello 100 times -- use for loop
for(int i=1;i<=100;i++)
{
System.out.println("hello");
}
مثال: برنامج يطبع الارقام من 1 الى 100 :
//write java program to print the numbers from 1 to 100 -- use for loop
for(int i=1;i<=100;i++)
{
System.out.println(i);
}
برنامج بجافا يطبع الاعداد الفردية بين 0 و100 :
//write java program to print all odd numbers between 0 to 100
for(int i=0;i<=100;i++)
{
if(i%2!=0)
System.out.println(i);
}
نفس البرنامج السابق لكن يطبع الارقام تنازليا من 100 الى 0 :
//write java program that print all odd numbers between 0 to 100, but print it descently
for(int i=100;i>=0;i--)
{
if(i%2!=0)
System.out.println(i);
}
برنامج يطبع الاعداد الزوجية بين 0 الى 100
//write java program to print all even numbers between 0 to 100
for(int i=0;i<=100;i++)
{
if(i%2==0)
System.out.println(i);
}
/*write java program to do the following:
create 2 arrays, car, and prices, and then asks user to enter cars names and price of each car.
at last the program prints the highest price, and summation of prices, and
*/
Scanner input=new Scanner(System.in);
System.out.println("welcome, enter the number of cars:");
String []cars=new String[input.nextInt()];
int []prices=new int[cars.length];
int sum=0;
int max=0;
for(int i=0;iprices[max])
max=i;
}
for(int j=0;j
مثال على الحلقات اللانهائية infinite loops :
//this is an infinite loop
for (int i = 10; i >0; i++) {
System.out.println("hello"+i);
System.out.println("hello");
i++;
if(i%2==0)
System.out.println(i);
}
مثال على الwhile باستخدام جافا:
int i=1;
while(i<=10) {
System.out.println("hello");
i++;
}
مثال على ال do while باستخدام جافا:
int i=1;
do{
System.out.println("hello");
i++;
}
while(i<=10);
برنامج مثال على الحلقات باستخدام FOR وايضا استخدام العبارات الشرطية باستخدام switch..case
/*
(Financial application: compound value) Suppose you save $100 each month
into a savings account with the annual interest rate 5%. Thus, the monthly interest
rate is 0.05/12 = 0.00417. After the first month, the value in the account
becomes
100 * (1 + 0.00417) = 100.417
After the second month, the value in the account becomes
(100 + 100.417) * (1 + 0.00417) = 201.252
After the third month, the value in the account becomes
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on.
Write a program that prompts the user to enter a monthly saving amount and
displays the account value after the sixth month. (In Exercise 5.30, you will use a
loop to simplify the code and display the account value for any month.)
*/
public class Exercise_02_13 {
public static void main(String[] args) {
// Generate an integer between 1 and 12.
for(int i=1;i<=10;i++)
{
int month = (int)((Math.random() * 12) + 1);
// Display the English month name
switch (month)
{
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December");
}
}
}
}