Saturday, September 4, 2021

# 17. loops

whenever we want to execute the same statements continuously or repeatedly we will go for looping statements

3 types of looping statements

1. while

2. do-while

3. for loop


while:

when do we go for while?

whenever we want to execute the statements continuously or repeatedly whenever we don't know the no of iterations in advance then go for while loop

if we don't know how many times we need to execute

How to define the while

standard syntax:

initialization section;        //starting point

while (condition)        // while is a keyword to convey it as while, the condition should always be boolean(true/false) otherwise we will get a compile-time error

{        //beginnig of loop

;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;

inc/dec section;

}

;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;


case 1: in while once the condition is true the loop body is executed till the condition become false.

case 2: if the condition is false 


int i=1;

while (i<=5)

{

System.out.println("Hello:"+i);

i++;  // i=i+1

}

System.out.println("oow");


do while:

if we want to execute the statement atleast once irrespective of the condition then go for do while

Standard syntax:

initialization section;        //starting point

do     

{        //beginnig of loop

;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;

inc/dec section;

}while (condition)

;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;


No comments:

Post a Comment

# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...