Loop Android Studio in Java

 Loop

In Java, a loop is a control structure that allows a certain block of code to be executed repeatedly as long as a specified condition is true. There are three main types of loops in Java: `for`, `while`, and `do-while`. Here's a brief overview of each, along with a loop specification table and examples:

1. For Loop:

 

for (initialization; condition; update) {// Code to be
//repeated code here }


  •     Initialization: Executed once at the beginning.
  •     Condition: Evaluated before each iteration. If false, the loop exits.
  •     Update: Executed after each iteration.


Example:

for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}

 


2. While Loop:

while (condition) {
// Code to be repeated
}

  • Condition: Evaluated before each iteration. If false, the loop exits.


   Example:

int count = 1;
while (count <= 5) {
System.out.println("Iteration " + count);
count++;
}


3. Do-While Loop:

do {
// Code to be repeated
} while (condition);


   - Code is executed at least once, even if the condition is false.

   - Condition: Evaluated after each iteration. If false, the loop exits.


   Example:

int count = 1;
do {
System.out.println("Iteration " + count);
count++;
} while (count <= 5);

Loop Specification Table:



In Java, a loop is a control structure that allows a certain block of code to be executed repeatedly as long as a specified condition is true. There are three main types of loops in Java: for, while, and do-while. Here's a brief overview of each, along with a loop specification table and examples:

  1. For Loop:

    java
    for (initialization; condition; update) { // Code to be repeated }
    • Initialization: Executed once at the beginning.
    • Condition: Evaluated before each iteration. If false, the loop exits.
    • Update: Executed after each iteration.

    Example:

    java
    for (int i = 1; i <= 5; i++) { System.out.println("Iteration " + i); }
  2. While Loop:

    java
    while (condition) { // Code to be repeated }
    • Condition: Evaluated before each iteration. If false, the loop exits.

    Example:

    java
    int count = 1; while (count <= 5) { System.out.println("Iteration " + count); count++; }
  3. Do-While Loop:

    java
    do { // Code to be repeated } while (condition);
    • Code is executed at least once, even if the condition is false.
    • Condition: Evaluated after each iteration. If false, the loop exits.

    Example:

    java
    int count = 1; do { System.out.println("Iteration " + count); count++; } while (count <= 5);


Loop Specification Table:
Loop TypeInitializationConditionUpdate (After each iteration)Execution Flow
for loopExecuted onceEvaluated beforeExecuted after each iterationInitialization → Condition check → Code execution → Update → Repeat
while loopN/AEvaluated beforeN/ACondition check → Code execution → Repeat until condition is false
do-while loopN/AEvaluated afterN/ACode execution → Condition check → Repeat until condition is false

These loops provide different ways to control the flow of your program and execute code repeatedly based on specified conditions.



For Loop Example

XML code

<EditText
android:id="@+id/inptOne"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="text one"
android:padding="20dp"
android:textSize="25sp"
android:textColor="@color/white"/>

<EditText
android:id="@+id/inptTwo"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="text one"
android:padding="20dp"
android:textSize="25sp"
android:textColor="@color/white"/>

<Button
android:id="@+id/btnClick"
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Click"
android:textSize="25dp"
android:backgroundTint="#878080"
/>

<TextView
android:id="@+id/tvDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/white"
/>



Java Code

nptOne = findViewById(R.id.inptOne);
nptTwo = findViewById(R.id.inptTwo);
btnClick = findViewById(R.id.btnClick);
tvDisplay = findViewById(R.id.tvDisplay);

btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sOneInp = nptOne.getText().toString();
String sTwoInp = nptTwo.getText().toString();

if (nptOne.length()>0 && nptTwo.length()>0){

int Input1 = Integer.parseInt(sOneInp);
int Input2 = Integer.parseInt(sTwoInp);

for (int x = Input1; x<=Input2; x++){
if (x%2==0) tvDisplay.append(""+x);
}

}
}
});

Now This Output 



Post a Comment

Post a Comment (0)

Previous Post Next Post