Execution Control in Java Simplified

0
203
Spread the love
  • 113
  • 145
  • 56
  • 149
  •  
  •  
  •  
  • 12
  •  
  •  
    475
    Shares

Execution Control in Java Simplified:

When a program breaks the sequential flow and jumps to another part of the code, the technique is called branching. If branching takes place on a condition, it is called conditional branching. If the branching takes place without a decision, it is called unconditional branching.Java coding complexity is determined by control structures. There are mainly two control structures in java:

  1. branches
  2. loops

There are several six sub-control structures of these two types. They are as follows:

  1. Block
  2. do-while
  3. while
  4. for and for each
  5. if
  6. Switch

Java supports the following execution controls:

  • if
  • if-else
  • While
  • do-while
  • for
  • switch

Java does not support a goto statement for the jump from one statement to another. It supports a finer approach for this goto.

Block:

The block is used to group a set of statements into a single statement. The statements are enclosed between a pair of braces({}).


{
statements
}
 

There could be a block with no statement. They are called empty blocks. It is just a pair of two braces{}. The block of code is usually used to group several statements into logical boundaries like a function. We can declare and define variables inside a block but they are local to block. Hence they are not visible or accessible outside of the block.Java allocates memory to the variables declared and defined inside a block. When the block is over, the memory is discarded by JVM to make room for other variables. Block statements do not affect the flow of control in a program.

True False:

All conditional statements use the truth or falsehood of a conditional expression to determine the execution path. However, Java does not support number as a boolean.

if statement:

It is the basic branching or decision technique in java. It is a two-way decision-making statement and it in conjunction with an expression.” if statement” checks the true path and executes the statements if the expression is evaluated as true.

When we use a Boolean expression in an if-else statement, the Boolean expression must be enclosed with a parameter.

if(expression){
statement
} 
if(testcondition)
statements 
if-else in java
if-else in java

In this case, if the expression is evaluated as true then the statement block inside the if block will be executed. Otherwise, it will skip the statement inside if and continue execution.

if-else statement:

“if-else statement” checks the true path and executes the statements if the expression is evaluated as true. if the expression is evaluated as false, it will follow the else part and execute the statements present in the else part;


if(expression){
statement1
}
else
{
statement2
}

In this case, if the expression is evaluated as true then the statement1 block inside the if block will be executed. Otherwise, it will go to else part and execute the statement2 part.

if(testcondition)
statements-1
else
statements-2 

if-else styles

There are two styles of if-else. They are as follows:

  • Allman style
  • Kernighan and Ritchie style
Allman Style:

This type of style is called the Allman style. They are easy to read and decode. It is named after Eric Allman.

if(myScore>yourScore)
{
System.out.println("I win");
}
else
{
System.out.println("you win");
}
Kernighan and Ritchie style

The second style is called Kernighan and Ritchie style(K&R style) after Demis Ritchie and Brian Kernighan. They are tough to read and need programming practice to decode.

if(myScore>yourScore){
System.out.println("I win");
}else{
System.out.println("you win");
}

it can also have nested if-else

if(testcondition1)
  if(testCondition2)
     statements-1
  else
   statements-2     
else
statements-3 

we can also have else if a ladder


if(testcondition1)
statement1
else if(testcondition2)
statement2
else if(testcondition3)
statement 3
else if
...
...
...
 

A statement block can be a single statement or a group of statements including a method call.

if(flag){
int age=30;
calculatePF();
} 

If can have more than one condition to evaluate.

if((x>y)&&(z>a)) 

Example of the nested if:

if (x>15)
   if(y>20)
     if(z>30)
       ....

 

An example of if-else:

public class oddEvenTest{
public static void main(String []args){
int number[]={50,65,56,71,81};
int even=0;
int odd=0;
for(int i=0;i<number.length;i++)
{
if((number[i]%2)==0)
even++;
else
odd++;
}
System.out.println("No of odd-"+odd);
System.out.println("No of even-"+even);
}
}

Iterations/Loops:

The process of repeatedly executing a block of statements is known as looping. The statement is the block may be executed any number of times from 0 to infinite number.
A loop consists of three stages:

  1. Starter
  2. body or block of code to be repeated
  3. Control statement

The control statement tests certain conditions and checks if the loop needs to be continued or terminated.

Depending on the position of a control statement a loop structure can be classified as :

  1. Entry controlled structure-conditions are tested before the start of the loop execution if the condition is not satisfied, the loop body will not be executed. like while and for
  2. Exit controlled structure-conditions are tested at the end of the body of the loop hence the body executed unconditionally for the first time. like do-while

A looping process consists of the below steps:

The steps are:

  1. Setting and initialization of a counter
  2. Execution of the statement of the loop
  3. Test for a condition for execution of the loop.
  4. Increment/decrement the counter

Few words to remember :

  • If the loop goes on and on and never ends, it is called an infinite loop.
  • A different value used in a loop to demark the real input value or to create a break condition of the loop is called Sentinel value.
  • Setting up different things so that the test in a while loop executed is called priming the loop.
  • A variable used to evaluate the truth or falsehood is called the loop control variable.

While:

while loop in java
while loop in java

A while loop is an entry controlled loop statement. A while loop is used to execute a statement multiple times. The boolean expression is evaluated once at the beginning of the loop and again before each further iteration of the statement.

The statements can be compound and mix of different expressions. But eventually, they will boil down to true or false. If it is true the body of the loop gets evaluated. Then it comes to evaluate the boolean expression.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 113
  • 145
  • 56
  • 149
  •  
  •  
  •  
  • 12
  •  
  •  
    475
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here