Java- continue keyword / Chapter -8 / Flow Control


java continue keywordjava continue keyword :-



  • we can use continue keyword inside loops to skip current iteration and continue for the next iteration.


example:-
for(int i=0 ; i<10 ; i++)
{
if(i%2==0)
continue;
System.out.println(i);
}
Output is:

Java- continue keyword / Chapter -8 / Flow Control

javabychetan.blogspot.com
2. continue :-
  • we can use continue statement inside loops to skip current iteration and continue for the next iteration.
example:-
for(int i=0 ; i<10 ; i++)
{
            if(i%2==0)
            continue;
             System.out.println(i);
}
Output is:

1
3
5
7
9



  • We can use continue statement only inside a loop is we are using anywhere else, we will get compile time error saying "continue outside of loop".
example:-

Java- break keyword / Chapter -7 / Flow Control



Java break keyword


The break keyword is used to break the control flow from switch & loops.


We can use break keyword in following places.


  • Inside switch ( ) to stop fall through.


example:
int x=0

switch(x)

{

  case 0 :

              System.out.println(0);

case 1 :
System.out.println(1);
break;
case 2 :
System.out.println(2);

default : System.out.println(default);
}
Output is :   0
1

  • Inside loop to break loop execution based on some condition.


example:-

Java- break keyword / Chapter -7 / Flow Control

javabychetan.blogspot.com
3.Transfer Statement:-
1.break:-
break is used to break the control flow from switch & loops
We can  use break keyword in following places.


  • Inside switch ( ) to stop fall through.

example:
int x=0
switch(x)
{
  case 0 :
              System.out.println(0);
  case 1 :
              System.out.println(1);
                                  break;
  case 2 :
              System.out.println(2);

default : System.out.println(default);
}
Output is :   0
                   1


  • Inside loop to break loop execution based on some condition.
example:-

Java- for each loop / Chapter -6 / Flow Control (1.5v)


java for each loop

java for each loop came into 1.5 version.


It is the specially designed loop to retrieve elements of arrays and collections.


example 1 :-
Q. print elements of the one-dimensional array?
int [ ] a={1,2,3,4,5};
To prints the elements of the array, we have 2 approaches.

1.old approach (old for loop) :-

for(int i=0 ; i<a.lenght ;i++ )
{
System.out.ptintln(a[i]);
}                                                   Output is  :
1
2
3
4
5

2. new approach (for each loop):-

for(int a1 :a )
{
System.out.ptintln(a1);
}

Output is :

Java- for each loop / Chapter -6 / Flow Control (1.5v)

javabychetan.blogspot.com
for each loop came into 1.5 version.It is specially designed loop to retrieve elements of arrays and collections.
example 1 :-
Q. print elements of the one-dimensional array?
 int [ ] a={1,2,3,4,5};
 To prints the elements of the array, we have 2 approaches.

1.old approach (old for loop) :-

for(int i=0 ; i<a.lenght ;i++ )              
{                                                      
System.out.ptintln(a[i]);
  }                                                   Output is  :
                                                                1            
                                                                2
                                                                3
                                                                4
                                                                5

2. new approach (for each loop):-

for(int a1 :a )
 {
System.out.ptintln(a1);                              
}      

Output is :  

Java for loop / Chapter -5/Flow Control




java for loop:-



  • If we know the number of iterations in advance then we should use for ( ) loop. 

  • It is the most commonly used loop in java.

  • Curly braces{ } are optional and without Curly braces { } we can take one statement under while which should not be the declarative statement.


Syntax:-
java for loop

                                                
(a) initialization section;-


Java-for( ) loop / Chapter -5/Flow Control




c. for ( ) loop :- 

  • If we know the number of iterations in advance then we should use for ( ) loop. 
  • It is the most commonly used loop in java.
  • Curly braces{ } are optional and without Curly braces { } we can take one statement under while which should not be the declarative statement.


Syntax:-
javabychetan.blogspot.com
                                                
(a) initialization section;-


  • This part will be executed only once in the whole life cycle of the for() loop.

Java do while loop / Chapter -4/ Flow Control




Java do while loop

Java do while loop:-


If we want to execute loop body at least once then we should go for the do-while loop.

Syntax:-

do
{
         body
}  while (b);         

                         Where; the semicolon is mandatory and b always should be a boolean type.



1. The arguments should be a boolean type. If we are trying to provide any other type then we will get compile time error.

example:
int b=10;
do
{
         body

}  while (b); 

Compile Time Error: Incompatible Type
Found: int
Required: boolean

Java-do-while loop / Chapter -4/Flow Control




b). do -while loop:-
If we want to execute loop body at-least once then we should go for the do-while loop.

Syntax:-

do
{
         body
}  while (b);         

                         where ; the semicolon is mandatory and b always should be a boolean type.



1. The arguments should be a boolean type . If we are trying to provide any other type then we will get compile time error.

example:
int b=10;
do
{
         body

}  while (b); 

Compile Time Error: Incompatible Type
Found:int
Required:boolean

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com