Skip to main content

Flow control part 2 Iterative Method

                                                         Iterative Method

  • Iterative statement
    • while()
    • do-while()
    • for()
    • for-each()    1.5v
                                                            while()
If we don't know the number of iterations in advance then we should go for a while loop.
Example-:
/*
while(rs.next()){
}

while(e.hasmoreElements()){
}

while(itr.hasNext()){
}

*/

the argument should be the boolean type if we are trying to provide any other type then we will get compile time error.
syntax-:
  while(b){
        statements;
}

this syntax is applicable to c and C++  but this terminology is not allowed in Java.

while(1){
   System.out.print("hello");
}
compile time error-:
incompatible type error
found int type
required boolean.

curly-braces are optional and without curly braces, we can take only one statement under while which should not be a declarative statement.

Example-:
/*
while(true)                                                                
     statement;                                                                                                           
valid                                

while(true);
valid                                

 while(true)  
       int x=10;  
 invalid                                

while(true){
    int x=10;  
 }  valid

*/

/*
while(true){                                                                             
    System.out.pirnt("hello");                             
}                                                                                                                  
System.out.print("hi");                                                          
                                                                                                                          
compile time error                                                           
unreachable statement               
System.out.print("hi")               

while(false){ 
        System.out.print("hello");  
 } 
System.out.print("hi");      

compile time error 
 unreachable statement
 {

 int a=10,b=20;
 while(a<b){
   System.out.print("hello");
 }    
System.out.print("hi");

output "hello" infinite time




int a=10,b=20;                                                 
while(a>b){                                                                        
   System.out.print("Hello");                     
}                                                                                                         
System.out.print("Hi");                                

output-"Hi"                                                              
                                                      

  final int a=10,b=20; 
while(a<b){ 
       System.out.print("Hello"); 
 }         
System.out.print("Hi");  
compile time error   
unreachable statement         
                                                                                                                  
final int a=10,b=20;
while(a>b){
    System.out.print("Hello");
 }
System.out.print("Hi"); 
compile time error
unreachable statement
  {

*/

Note-: Every final variable will be replaced by a value at compile time only. 
                

Note -: If every argument is the final variable (compile time constant) then that operation should be performed at compile time only.


                                                do-while()
if we want executed loop body at least once then we should go for do while.
syntax-:
do{
    body;
}while(condition);

curly braces are optional and without curly braces, we can take only one statement b/w do and while which should not be a declarative statement.

/*
do{                                                                                                  
   statement;                                                  
}while(true);                                                 

valid                      

do;
   while(true); 
valid                        

do                
    int x=10;
while(true);
  
 NotValid  


do{                      
     int x=10;  
     }while(true);
 valid    


 do
    while(true);
NotValid


*/

spacial case

do while(true)
    System.out.print("hello");
while(false);
we can write this 
do
    while(true)
       System.out.print("Hello");
while(false);
is valid








Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...