logo

جافا أثناء الحلقة

ال جافا حائط اللوب يتم استخدامه لتكرار جزء من البرنامج بشكل متكرر حتى يصبح الشرط المنطقي المحدد صحيحًا. بمجرد أن يصبح الشرط المنطقي خاطئًا، تتوقف الحلقة تلقائيًا.

تعتبر حلقة while عبارة if متكررة. إذا لم يكن عدد التكرار ثابتًا، فمن المستحسن استخدام while حلقة .

بناء الجملة:

كيفية إرسال السلسلة إلى int في Java
 while (condition){ //code to be executed I ncrement / decrement statement } 

الأجزاء المختلفة من حلقة do-while:

1. الحالة: عبارة مجرب. إذا كان الشرط صحيحًا، فسيتم تنفيذ نص الحلقة وينتقل التحكم إلى تحديث التعبير. عندما يصبح الشرط خاطئا، نخرج من الحلقة while.

مثال :

أنا<=100< p>

2. تحديث التعبير: في كل مرة يتم فيها تنفيذ نص الحلقة، يزيد هذا التعبير أو ينقص متغير الحلقة.

مثال:

أنا++;

مخطط انسيابي لجافا أثناء الحلقة

جوفيندا الممثل

هنا، الشيء المهم في حلقة while هو أنه في بعض الأحيان قد لا يتم تنفيذها. إذا كانت نتيجة الشرط الذي سيتم اختباره خطأ، فسيتم تخطي نص الحلقة وسيتم تنفيذ العبارة الأولى بعد الحلقة.

مخطط انسيابي لجافا أثناء الحلقة

مثال:

في المثال أدناه، نقوم بطباعة قيم عددية من 1 إلى 10. وعلى عكس حلقة for، نحتاج بشكل منفصل إلى تهيئة المتغير المستخدم في الشرط وزيادته (هنا، i). وإلا، سيتم تنفيذ الحلقة إلى ما لا نهاية.

whileExample.java

 public class WhileExample { public static void main(String[] args) { int i=1; while(i<=10){ system.out.println(i); i++; } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h2>Java Infinitive While Loop</h2> <p>If you pass <strong>true</strong> in the while loop, it will be infinitive while loop.</p> <p> <strong>Syntax:</strong> </p> <pre> while(true){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>WhileExample2.java</strong> </p> <pre> public class WhileExample2 { public static void main(String[] args) { // setting the infinite while loop by passing true to the condition while(true){ System.out.println(&apos;infinitive while loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+c </pre> <p>In the above code, we need to enter Ctrl + C command to terminate the infinite loop.</p> <hr></=10){>

جافا المصدرية أثناء الحلقة

إذا نجحت حقيقي في حلقة while، ستكون صيغة المصدر while.

كل الحروف الكبيرة تتفوق على الأوامر

بناء الجملة:

 while(true){ //code to be executed } 

مثال:

whileExample2.java

 public class WhileExample2 { public static void main(String[] args) { // setting the infinite while loop by passing true to the condition while(true){ System.out.println(&apos;infinitive while loop&apos;); } } } 

انتاج:

 infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+c 

في الكود أعلاه، نحتاج إلى إدخال الأمر Ctrl + C لإنهاء الحلقة اللانهائية.