BLACKMASK

OOP WITH JAVA CHAPTER 3 (NEW 2018)

OBJECT ORIENTED PROGRAMMING WITH JAVA

CHAPTER 3

CONTROL STATEMENT


Loop(Repeatition,Iterate):.
 A loop statement allows us to execute statement or set of statements repeatedly until given condition remain true.
Control variable
 Increment/decrement
 Condition/expression
Types of loop
1. Do while loop
2. While loop
3. For each loop
4. For loop
1) While Loop:.
 In while loop firstly condition is evaluated and then body of the loop is executed repeatedly until the condition remain true. 
Syntax
while(Expression)
{ Statement
}
 Int x=5
 while(x>=0)
 {
 System.out.println(“DAE”)
 }
 2) Do While Loop
 Do loop in much like while loop execute except that the loop expression is tested at the bottom of the loop.
 Syntax:.
 Do{
 ---
 ---
 ---
 }
 While (Expression);
 3 For Loop
 For statement provide the more convinent way of looping than while and do-while .for-loop is useful when you know how many time the task is to be repeated.
 Syntax
For(initialize;test;update)
{
 Statement
 }
 Int x=4;
 For(int i=1;10<10;i++){}
 System.out.println(x+”*”+i+”=”+i*x); 
 4 For each loop
 For each loop is used for handling collection of objects that need to be looped over. The for each loop uses ‘for’ keyword followed by an opening parenthesis, a variable declaration, a colon, an expression a closing parenthesis and finally the body of loop. 
Syntax 
 For(declaration;expression) 
 { //body of loop } 
 Int[]primes={3,5,7,11,13,17,19,23}; 
 For(int i:prime) System.out.println(i); 
 The loop is executed once for each element of array or collection and does not use a counter,because number of interaction are known. 
 Foreach loop cannot
 Iterate backward.
 Use a single variable for two arrays of some length.
 Int[]primes={2,3,5,7,11,13,19}; 
 Int[]even={2,4,6,8,10,12,14}; 
 For(i=0;i><=7;i++){}
 System.out.println(“primes=”+prim(i)); System.out.println(“even=”+even(i));
We can not iterate through element of list using calls to its gets ()method.
Break
For(int i=1;i<10;i++){
If(i==5)
{break}
System.out.println(i);
}
Continue
For(int i=1;i=<10;i++){
If i==5
{
continue
}
System.out.println(i);
}
Note(you can take notes of OOP WITH JAVA from "swedish89.wordpress.com")