Core Java - Control Flow StatementsThe execution of the program can be controlled by the programmer in a way that programmer can decide either which instruction has to be executed and ignored by using same statement are called as control flow statements.We are having two types of control flow statements such as 1. Decision making statements:We use decision making statement to decide which instruction need to be executed and ignored,We are having following decision making statementsa. if condition: We go for if statement, if we are having only one option.If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If not,the first set of code after the end of the if statement(after the closing curly brace) will be executed. Syntax: if(Boolean){ //Statements will execute if the Boolean expression is true }Eg: public class Test{ public static void main(String args[]){ int x =10; if( x < 20){ System.out.print("This is if statement condition..."); } } }Output: This is if statement condition... b. if-else Statement: An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax: if(Boolean_expression){ //Executes when the Boolean expression is true }else{ //Executes when the Boolean expression is false }Eg: public class Test{ public static void main(String args[]){ int x =30; if(x < 20 ){ System.out.print("This is if statement condition..."); }else{ System.out.print("This is else statement condition..."); } } }Output: This is else statement condition... c. else-if ladder: An condition we go for else-if ladder if we are having more than two option where execution of one option is mandatory. When using if, else if , else statements there are few points to keep in mind. if(Boolean_expression1){ //Executes when the Boolean expression 1 is true }elseif(Boolean_expression2){ //Executes when the Boolean expression 2 is true }elseif(Boolean_expression3){ //Executes when the Boolean expression 3 is true }else{ //Executes when the none of the above condition is true. }Eg: public class Test{ public static void main(String args[]){ int x =30; if( x ==10){ System.out.print("Value of X : 10"); }elseif( x ==20){ System.out.print("Value of X : 20"); }elseif( x ==30){ System.out.print("Value of X : 30"); }else{ System.out.print("This is else statement"); } } }Output: Value of X : 30 d. switch statement: A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax: switch(expression){ case value : //Statements break;//optional case value : //Statements break;//optional //You can have any number of case statements. default://Optional //Statements }The following rules apply to a switch statement: public class Test{ public static void main(String args[]){ char grade = args[0].charAt(0); switch(grade){ case'A': System.out.println("Excellent!"); break; case'B': System.out.println("Good"); break; case'C': System.out.println("Well done"); break; case'D': System.out.println("You passed"); case'F': System.out.println("Better try again"); break; default: System.out.println("Invalid grade"); } System.out.println("Your grade is "+ grade); } }Output: java Test a
Your grade is a A
Your grade is a C e. break statement: It is a control transfer statement as well as keyword.It has to be used only inside switch (or) loop statement. The job of break is to take control of the main thread outside the switch block. Syntax: break;Eg: public class Test{ public static void main(String args[]){ int[] numbers ={10,20,30,40,50}; for(int x : numbers){ if(x ==30){ break; } System.out.print( x ); System.out.print("\n"); } } }Output : 10 20 f. return statement In java, every method should return the data after performing the task.Returntype in a method will specify what type of data has to be returned back. In java, in place of returntype we can use primitive datatype, non-primitive datatype and void. 2. Looping statementsWe use looping statement to execute the java instruction repeatedly (or) again and again. We are having following looping statements available such asa.while loop : A while loop is a control structure that allows you to repeat a task a certain number of times. Syntax: while(Boolean_expression){ //Statements }When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true. Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed. Eg: public class Test{ public static void main(String args[]){ int x =10; while( x < 20){ System.out.print("value of x : "+ x ); x++; System.out.print("\n"); } } }Output: value of x :10 value of x :11 value of x :12 value of x :13 value of x :14 value of x :15 value of x :16 value of x :17 value of x :18 value of x :19 b. do while loop: A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. Syntax: do{ //Statements }while(Boolean_expression);Eg: public class Test{ public static void main(String args[]){ int x =10; do{ System.out.print("value of x : "+ x ); x++; System.out.print("\n"); }while( x < 20); } }Output: value of x :10 value of x :11 value of x :12 value of x :13 c. for loop statement: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.A for loop is useful when you know how many times a task is to be repeated. Syntax: for(initialization;Boolean_expression; update){ //Statements }Here is the flow of control in a for loop: Eg: public class Test{ public static void main(String args[]){ for(int x =10; x < 20; x = x+1){ System.out.print("value of x : "+ x ); System.out.print("\n"); } } }Output: value of x :10 value of x :11 value of x :12 value of x :13 value of x :14 value of x :15 value of x :16 value of x :17 value of x :18 value of x :19 d. for each loop statement(Enhanced for loop ): As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays. Syntax: for(declaration : expression){ //Statements }Declaration : Expression : public class Test{ public static void main(String args[]){ int[] numbers ={10,20,30,40,50}; for(int x : numbers ){ System.out.print(x); System.out.print(","); } System.out.print("\n"); String[] names ={"James","Larry","Tom","Lacy"}; for(String name : names ){ System.out.print( name ); System.out.print(","); } } }Output: 10,20,30,40,50, James,Larry,Tom,Lacy, ☛ Join to Learn from Experts: Java / J2EE Course in Chennai by TesDBAcademy
« Previous
Next Topic »
(Core Java - Method)
|