1 5 2 P 603c5d883c701 File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CO_IF_22412_CO1

Ms. Yogita Jore, Head of Information Technology, Vidyalankar Polytechnic

Date: 13th February 2021

Learning at your doorstep


Unit 1:
Basic Syntactical constructs in Java

Written by

Yogita Jore
Head, Department of Information Technology (NBA Accredited),
Vidyalankar Polytechnic, Mumbai
Unit Outcome 5:
Develop Programs using relevant
control structure to solve the given
problem.
Learning Outcome 5b:
Students should understand the use of
looping statements.
What we will learn today-Looping Statements

Key takeaways
Where to use looping statements in given problem?

Yogita Jore
Head, Department of Information Technology (NBA Accredited), Vidyalankar Polytechnic,
Mumbai

Page 5 Maharashtra State Board of Technical Education


Concept Map

Page 6 Maharashtra State Board of Technical Education


Learning Objective/ Key learning

► while
► do while
► for
► For each

Page 7 Maharashtra State Board of Technical Education


while loop

 The Java while loop is used to iterate a part of the program several times. Syntax:
 If the number of iteration is not fixed, it is recommended to use while loop. while(condition)
{
 Example: //code to be executed
public class WhileExample }
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
}
}
Page 8 Maharashtra State Board of Technical Education
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.
 Example: Syntax:
public class DoWhileExample
{ do
public static void main(String[] args) {
{ //code to be executed
int i=1;
do }
{ while(condition);
System.out.println(i);
i++;
}
while(i<=10);
}
}
}
}
Page 9 Maharashtra State Board of Technical Education
for loop

 The Java for loop is used to iterate a part of the program several times.
 If the number of iteration is fixed, it is recommended to use for loop.
 Example:
public class ForExample Syntax:
{ for(initialization;condition;incr/decr)
public static void main(String[] args) {
{ //statement or code to be executed
for(int i=1;i<=10;i++) }
{
System.out.println(i);
}
}
}

Page 10 Maharashtra State Board of Technical Education


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.
 Example:
public class ForEachExample Syntax:
{ for(Type var:array)
public static void main(String[] args) {
{
//code to be executed
//Declaring an array
int arr[]={1,2,3,7,8,14,15}; }
//Printing array using for-each loop
for(int i:arr){
System.out.println(i);
}
}
}

Page 11 Maharashtra State Board of Technical Education

You might also like