BCA : 5TH SEM
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING
RAM GOPAL GUPTA
ASSOCIATE PROFESSOR
DISCLAIMER
Contents of the tutorial, shown image(s) (if shown here) and movie(s) (if shown here)
are just compilation of research work/creation of various authors/organizations.
Contents are collected from various research papers, reports, books, websites and other
sources.
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
UNIT-1
CONTROL STRUCTURES IN JAVA
2. Iterative statements (Loops)
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
Java For Loop vs While Loop vs Do While Loop
Comparison for loop while loop do while loop
Introduction The Java for loop is a control flow The Java while loop is a control The Java do while loop is a control flow
statement that iterates a part of flow statement that executes a part statement that executes a part of the
the programs multiple times. of the programs repeatedly on the programs at least once and the further
basis of given boolean condition. execution depends upon the given
boolean condition.
When to use If the number of iteration is fixed, it If the number of iteration is not If the number of iteration is not fixed
is recommended to use for loop. fixed, it is recommended to use and you must have to execute the loop
while loop. at least once, it is recommended to use
the do-while loop.
Syntax for(init;condition;incr/decr){ while(condition){ do{
// code to be executed //code to be executed //code to be executed
} } }while(condition);
Example //for loop //while loop //do-while loop
for(int i=1;i<=10;i++){ int i=1; int i=1;
[Link](i); while(i<=10){ do{
} [Link](i); [Link](i);
i++; i++;
} }while(i<=10);
Syntax for for(;;){ while(true){ do{
infinitive //code to be executed //code to be executed //code to be executed
loop } } }while(true);
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
Simple For Loop
A simple for loop is the same as C/C++. We can initialize the variable, check condition and
increment/decrement value. It consists of four parts:
• Initialization: It is the initial condition which is executed once when the loop starts. Here, we
can initialize the variable, or we can use an already initialized variable. It is an optional
condition.
• Condition: It is the second condition which is executed each time to test the condition of the
loop. It continues execution until the condition is false. It must return boolean value either
true or false. It is an optional condition.
• Statement: The statement of the loop is executed each time until the second condition is
false.
• Increment/Decrement: It increments or decrements the variable value. It is an optional
condition.
Syntax:
1. for(initialization;condition;incr/decr){
2. //statement or code to be executed
3. }
Example:
1. //Java Program to demonstrate the example of for loop
2. //which prints table of 1
3. public class ForExample {
4. public static void main(String[] args) {
5. //Code of Java for loop
6. for(int i=1;i<=10;i++){
7. [Link](i);
8. }
9. }
10. }
for-each Loop
The for-each loop is used to traverse array or collection in java. It is easier to use than simple for
loop because we don't need to increment value and use subscript notation. It works on elements basis
not index. It returns element one by one in the defined variable.
Syntax:
1. for(Type var:array){
2. //code to be executed
3. }
Example:
1. //Java For-each loop example which prints the
2. //elements of the array
3. public class ForEachExample {
4. public static void main(String[] args) {
5. //Declaring an array
6. int arr[]={12,23,44,56,78};
7. //Printing array using for-each loop
8. for(int i:arr){
9. [Link](i);
10. }
11. }
12. }
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
Labeled For Loop
We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful if
we have nested for loop so that we can break/continue specific for loop. Usually, break and continue
keyword breaks/continues the innermost for loop only.
Syntax:
1. labelname:
2. for(initialization;condition;incr/decr){
3. //code to be executed
4. }
Example:
1. //A Java program to demonstrate the use of labeled for loop
2. public class LabeledForExample {
3. public static void main(String[] args) {
4. //Using Label for outer and for loop
5. aa:
6. for(int i=1;i<=3;i++){
7. bb:
8. for(int j=1;j<=3;j++){
9. if(i==2&&j==2){
10. break aa;
11. }
12. [Link](i+" "+j);
13. }
14. }
15. }
16. }
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
While Loop
The Java while loop is used to iterate a part of the program several times. If the number of iteration is
not fixed, it is recommended to use while loop.
Syntax:
1. while(condition){
2. //code to be executed
3. }
Example:
1. public class WhileExample {
2. public static void main(String[] args) {
3. int i=1;
4. while(i<=10){
5. [Link](i);
6. i++;
7. }
8. }
9. }
Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
Syntax:
1. while(true){
2. //code to be executed
3. }
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
Example:
1. public class WhileExample2 {
2. public static void main(String[] args) {
3. while(true){
4. [Link]("infinitive while loop");
5. }
6. }
7. }
8.
do-while Loop
The Java do-while loop is used to iterate a part of the program several times. If the number of
iteration is not fixed and you must have to execute the loop at least once, it is recommended to use
do-while loop. The Java do-while loop is executed at least once because condition is checked after
loop body.
Syntax:
1. do{
2. //code to be executed
3. }while(condition);
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])
Example:
1. public class DoWhileExample {
2. public static void main(String[] args) {
3. int i=1;
4. do{
5. [Link](i);
6. i++;
7. }while(i<=10);
8. }
9. }
BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])