Pages

Thursday 8 August 2019

Control flow

Agenda:
  • Selection statements
  • Iterative statements
  • Transfer statements
Selection statements:
  • if-else
  • switch
if-else:
Syntax:
  1.  if(b){// here b is boolean type
  2. // action if b is true.
  3. }else{
  4. action if b is false.
  5. }
  • The argument of the if statement must be boolean type otherwise you will get a compiler error.
  • {} curly braces are optional if you take only one statement under if, but it should not be a declarative statement.
  • e.g if(10>5)
  • int x=10;// Compiler error because it is a declarative statement.
  • if(true); is valid syntax but not useful.
switch:
If many options are available then it is not recommended to use if-else, you should go for the switch statement.
Syntax:
  1. switch(x){
  2. case 1: action1
  3. case 2: action2
  4. case 3: action3
  5. .
  6. .
  7. .
  8. default: default action
  9. }
  • {} curly braces are mandatory.
  • case and default are optional
  • Every statement inside the switch must be under some case or default. Independent statements are not allowed.
  • e.g switch(){
  • System.out.println("Hello");// It is an independent statement so, compiler error.
  • }
byte,short,int,char,Byte,Short,Integer,Character,enum,String are allowed argument for switch.
Every case label should be compile-time constant otherwise you will get a compiler error.
e.g int x=10;
switch(x){
case x: System.out.println("Hello");//compiler error because x is not compile time constant. to make it compile time constant use  final keyword.
}
final int x =10;//compile time constant
switch(x){
case x: System.out.println("Hello");
}
Switch argument and case label can be expression, but case must be constant expression.
Every case label should be within the range of switch argument types.
byte b=10;
switch(b){
case 10:System.out.println("anu");
case 100: System.out.println("ambe");
case 1000: System.out.println("amit");//compiler error, byte range is -128 to 127  but case label 1000 which is out of range
}
Duplicate case label is not allowed.

Fall through inside the switch:
  • Within the switch statement if any case is matched from that case onwards all statements will be executed until the end of the switch or break.
  • Within the switch, you can take the default anywhere.
Iterative statements:
while loop:
  • If you don't know the number of iterations in advance then you should go for a while loop.
  • The argument of the while loop must be boolean type, otherwise compiler error.
  • {} curly braces are optional and without curly braces, you can take only one statement which should not be a declarative statement(int a =10; or int a)
do-while loop:
  • If you want to execute while loop at least one then you should go for a do-while loop.
  • {} curly braces are optional and without curly braces, you can take only one statement between do and while which should not be a declarative statement(int a =10; or int a).
Syntax:
do{}while(b);// semicolon is mandatory.
Note: do while(b);//compiler error

For loop:
If you know the number of iteration in advance then you can use this.
syntax:

{} curly braces are optional and without curly braces, you can take only one statement which should not be a declarative statement(int a =10; or int a)
1.Initialization section
  • This section will be executed only once.
  • Here you can declare loop variable and you will perform initialization.
  • You can declare multiple variables but should be of the same type.
  • You can't declare the different types of variables.
  • In the initialization section, you can take any valid statement including System.out.println().
2. condition check
  • You can take any java expression but should be of the type boolean.
  • The condition expression is optional if you are not taking any expression compiler will place true.
3.Increment and decrement section:
Here you can take any java statement including System.out.println().

Enhance for loop:
  • It was introduced in java 1.5v.
  • Best suitable to retrieve the elements of arrays and collection.
  • Example:
  • int a[] ={10,20,30,40,50};
  • for(int val:a){
  • System.out.println(val);
  • }
Transfer statements
Break statement:
  • You can use break statement in the following cases.
  • Inside switch to stop fall-through.
  • Inside loops to break the loop based on some condition.
  • Inside label, blocks to break block execution based on some condition.
  • If you are using anywhere else you will get a compiler error.
Continue statement:
  • You can use continue statement to skip current iteration and continue for the next iteration.
  • You can use continue statement only inside loops otherwise you will get a compiler error.
Next Topic: ARRAYS