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
- Initialization: Executed once at the beginning.
- Condition: Evaluated before each iteration. If false, the loop exits.
- Update: Executed after each iteration.
Example:
System.out.println("Iteration " + i);
}
2. While Loop:
// Code to be repeated
}
- Condition: Evaluated before each iteration. If false, the loop exits.
Example:
System.out.println("Iteration " + count);
count++;
}
3. Do-While Loop:
} 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:
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:
For Loop:
javafor (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:
javafor (int i = 1; i <= 5; i++) { System.out.println("Iteration " + i); }
While Loop:
javawhile (condition) { // Code to be repeated }
- Condition: Evaluated before each iteration. If false, the loop exits.
Example:
javaint count = 1; while (count <= 5) { System.out.println("Iteration " + count); count++; }
Do-While Loop:
javado { // 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:
javaint count = 1; do { System.out.println("Iteration " + count); count++; } while (count <= 5);
Loop Type | Initialization | Condition | Update (After each iteration) | Execution Flow |
---|---|---|---|---|
for loop | Executed once | Evaluated before | Executed after each iteration | Initialization → Condition check → Code execution → Update → Repeat |
while loop | N/A | Evaluated before | N/A | Condition check → Code execution → Repeat until condition is false |
do-while loop | N/A | Evaluated after | N/A | Code 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
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
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);
}
}
}
});
Post a Comment