CONTROL FLOW STATEMENTS IN JAVA .

Guided by Bipin Ajay , Founder of Tech Jitendra .

Generally the statements inside your java code are executed from top to bottom, in the order that they appear. Control flow statements, change or break the flow of execution by implementing decision making, looping, and branching your program to execute particular blocks of code based on the conditions.

There are 3 types of control flow statements supported by the Java programming language.

  • Decision-making statements : if-then, if-then-else, switch
  • Looping statements : for, while, do-while
  • Branching statements : break, continue, return

Java if statements

The if-then Statement

The if-then statement is the most basic of all the control flow statements. It enables your program to execute a certain section of code depending on the state of variables, or values returned from methods.if(isCar) { System.out.println("I am a Car"); }

If isCar test evaluates to false, control jumps to the end of the if-then statement. In Java, the opening and closing braces are optional.But this is applicable only if the block of code to be executed is just a single statement:if(isCar) System.out.println("I am a Car");

But as a good practice, it is advisable to put the brackets around the statements, even if there is only one statement to execute.This is because, in the begining you may start with one statement and later during the development phase you may add more statements. During this a common mistake would be forgetting to add the newly required braces which compiler cannot catch.

The if-then-else Statement

The if-then-else statement provides a alternate path of execution when an “if” clause evaluates to false.if(isCar) { System.out.println("I am a Car"); } else { System.out.println("I am a Truck"); }

Chaining if Statements

You can chain if-else statements, and create a decision tree sort of thing.if(vehicle="Car") { System.out.println("I am a Car"); } else if(vehicle="Truck") { System.out.println("I am a Truck"); } else { System.out.println("I am a Bike"); }

In some other example, there could be a chance that it can satisfy more than one expression in the statements. But in Java, once a condition is satisfied and appropriate statements are executed then the remaining conditions are skipped.

Switch statement

Another way to control the flow of your programs with decision-making statements is by a switch statement. A switch statement gives you the option to test for a range of values for your variables. If you think your if-else-if statements is long and complex, you can use switch statement instead.class SwitchDemo { public static void main(String[] args) { int age = 3; String yourAge; switch (age) { case 1: System.out.println("You are one yr old"); break; case 2: System.out.println("You are two yr old"); break; case 3: System.out.println("You are three yr old"); break; default: System.out.println("You are more than three yr old"); break; } } }

The body of a switch statement is known as a switch block. The switch statement evaluates its expression within the brackets, then executes all statements that follow the matching case label. Again there might be more than one cases being matched but switch will choose the first immidiate matching case ignoring the others.

break statement is necessary.Because without it, statements in switch blocks fall through. Lets take a look at below program:class SwitchDemo { public static void main(String[] args) { int age = 2; String yourAge; switch (age) { case 1: System.out.println("You are one yr old"); case 2: System.out.println("You are two yr old"); case 3: System.out.println("You are three yr old"); default: System.out.println("You are more than three yr old"); break; } } }

Output will be:You are two yr old You are three yr old You are more than three yr old

Thanking you

Tech Jitendra .

Published by Tech Jitendra Direct Investing

Tech Jitendra Direct Investing (TJDI) Empoworing Your Wealth

Leave a comment

Design a site like this with WordPress.com
Get started