(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
22H51A6755.pptx
1. Presented by : saniya siddiqua
Subject : oops
Roll no : 22h51a6755
Class : CSD
2. FLOW CONTROL
STATEMENTS
In java, the default execution flow of a program is a sequential order. But the
sequential order of execution flow may not be suitable for all situations. Sometimes,
we may want to jump from line to another line, we may want to skip a part of the
program, or sometimes we may want to execute a part of the program again and
again. To solve this problem, java provides control statements.
Types of Control Statements
In java, the control statements are classified as follows.
Selection Control Statements ( Decision Making Statements )
Iterative Control Statements ( Looping Statements )
Jump Statements
Let’s look at each type of control statements in java.
3.
4. ITERATIVE STATEMENTS
For loop:
for loop helps execute a set of code repeatedly, and it is recommended when the
number of iterations is fixed. You have seen the syntax of for loop, now try to
understand it.
Code: syntax:
Class Main {
public static void main(String[] args) {
int n = 5;
for (int I = 1; I <= n; ++i) {
System.out.println(i); }. } }
Output:
1 2 3 4 5
5. While loop :
The Java while loop is used to iterate a part of the program repeatedly until the
specified Boolean condition is true. As soon as the Boolean condition becomes false, the
loop automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is
not fixed, it is recommended to use the while loop.
Code: Syntax:
Public class WhileExample {
public static void main(String[] args) {
int i=1;
while(i<=10){
System.out.println(i);
i++; } } }
Out put :
1 2 3 4 5 6 7 8 9 10
6. Do while loop :
Loops in Java come into use when we need to repeatedly execute a block of statements.
Java do-while loop is an Exit control loop. Therefore, unlike for or while loop, a do-while
check for the condition after executing the statements of the loop body.
Note: The test_expression for the do-while loop must return a boolean value , else we
would get compile-time error.
Code: syntax:
Public class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10); } }
Out put:
1 2 3 4 5 6 7 8 9 10
7. For each loop :
The for-each loop is used to iterate through elements of arrays and collections (like
ArrayList). It is also known as the enhanced for loop.
Code : syntax :
Class ForEach Example{
public static void main(String args[]){
int arr[]={12,13,14,44};
int total=0;
for(int i:arr){
total=total+I; }
System.out.println(“Total: “+total); } }
Out put :
total : 83