Menu
Topics Index
...
`

Control Statements In Java | switch case & for Loop

In general, every program execution can be done from top to bottom in sequential order. But, if we use control statements, then we can change program execution order depending on logic & values. That means, we can skip some lines of code and we can iterate same lines of code based on our requirement. Simply, the control statements in Java are useful to control the program flow. There are different types of control statements in Java such as Selection statements, Iteration statements (Loops) and Jump statements.

Control Statements In Java, Control Statements In Java | switch case & for Loop

There is no limitation for the usage of these control statements in Java. The control statements in Java can be used together or they can be used as nested together with each other. The three classifications of these control statements in Java can used in different situations depending on our requirement. Selection statements are used to execute a block of code only if the condition is true. Iteration statements are used to iterate a block of code repeatedly until the condition is false. And the Jump statements are used to skip the statements and come-out from those.

Java switch case statement is comes under Selection statements. In this Java switch case, it allows a variable to be checked for equality with different types of values. Every variable value is treated as case and the variable can be checked with those values in each case. The following are the important rules of Java switch case statement.

  • Java switch case variables can only take values as integers, enums and strings.
  • Java switch case statement can contain any number of cases. Each case must have a value to be checked and executed.
  • In Java switch case, the value of each case must be match with the variable data type in switch & it must be a literal or a constant.
  • In Java switch case, if a variable is equals to a case, then the statements under that case will be executed until it reaches the break statement.
  • If a break statement is executed then the Java switch case terminates the flow and the control come out from that switch statement.
  • In Java switch case, there is no rule that every case must contain a break statement. If the break doesn't exist then the flow continuesly go through its subsequent cases until the break statement exists.
  • Java switch case statement may contain a default case which is optional. It must be the last case of the switch. The default case can be executed if all the cases are failed to fulfil the condition. The default case doesn't contain any break statement.
Java Switch Case, Control Statements In Java | switch case & for Loop

Apart from switch case statements if, nested if, etc are also comes under selection statements which explained below of the page.

A Java for loop is comes under Iteration Statements. In this Java for loop, it allows to iterate a specific block of code multiple times according to our requirement. The syntax of Java for loop is,

  • for(initialization; condition; increment/decrement)
    {
        //statements
    }
Java For Loop, Control Statements In Java | switch case & for Loop

The flow of control in Java for loop is as follows;
  • At first the control comes to initialization section in Java for loop. This section is executed only once when the for loop starts its execution. The loop control variables can be declared and initialized in this step. This section ends up with a semi-colon (;) symbol.
  • Now, the control comes to the condition section. It checks whether the condition is true or not. If the condition is true, then the statements in the Java for loop body can be executed. If the condition is false, then the statements in the Java for loop won't be executed and the control comes out from the for loop; the next line after Java for loop will be executed.
  • If the condition is true, the list of statements in the for loop block will be executed. After executing all the statements, now the control comes at updating (increment/decrement) section. In this section, we can able to update the loop control variables by incrementing or decrementing them. Semi-colon is not placed at the end of this section because it is the last statement inside the for loop.
  • Now, again the control comes back to condition section and it checks the condition with modified values. If the condition is true, the Java for loop executes its block of statements and the same process repeats again as explained above. This process continues until the condition is false. After the condition is false the Java for loop terminates and the next statement after for loop will be executed.

Apart from for loop; while loop, do while loop, etc are also comes under Iteration statements which explained below.

There are mainly three jump statements in Java. break, continue and return are the jump statements which explained below.


In this section, we'll go into detail about:
5.1 Control Statements In Java
  • The control statements in Java, as the name suggests controls the flow of the program. Depending upon the state ( values of variables, user input etc ) the appropriate code is ...Read More

5.2 Selection Statements
5.2.1 Selection Statements In Java
  • Java supports two selection statements - if and switch. These statements allow us to control the flow of the program. Depending upon the expressions or values, the corresponding...Read More

5.2.2 if Condition In Java
  • if condition in Java is a conditional branch statement, which can be used to route program execution through different paths. if condition in Java can be used to ...Read More

5.2.3 Nested if Statements In Java
  • When a if condition is included in the if block of some other if condition then those are called nested ifs in Java. We can have as many nested ifs and it can go into ...Read More

5.2.4 if else if ladder In Java
  • It is very common in programming to have more than two ways (or branches). e.g., printing the grade of a student like "Distinction", "First Class", "Second Class" and ...Read More

5.2.5 switch Statement In Java
  • switch provides a better multi-way branch than if-else-if ladder which executes multiple branches depending on the values of an expression. The expression must be of ...Read More

5.2.6 if else Vs switch Performance In Java
  • Although if-else and switch are multi-way branch statements, they are not completely same. switch can be used only for a specific value and can not be used for ...Read More

5.2.7 Nested switch Statements In Java
  • Similar to if statement, switch statements can be nested as well. In nested switch statements, switch statements can be presented with in the switch statements. In order to...Read More

5.2.8 Fall Through Switch Case Statements In Java
  • It is not necessary for every case in the switch Statement In Java to have a break. Some times we might have a situation where we have the same or similar handling ...Read More

5.3 Blocks of code
5.3.1 Block Of Code In Java
  • Block of code in Java allows grouping of two or more statements. We use curly braces { } to denote a block of code in Java. Block of code in Java can be used where ever we...Read More

5.3.2 Scope Of Variables In Nested/Multiple Blocks
  • Scope Of Variables in Nested/Multiple Blocks in Java States that variables declared in outer blocks will be accessible in inner blocks, where as variables declared in ...Read More

5.3.3 Lifetime Of Variable In Java
  • Lifetime of a variable in Java defines how long a variable and its value are valid. The life time of a variable can be depend on the declaration of that variable. If it is declared at...Read More

5.3.4 Expressions, Statement, Line & Block In Java
  • Although the terms expression, statement, line, block, condition, and body represent code (or program), there are small differences between them. It is required to ...Read More

5.4 Iteration statements (Loops)
5.4.1 Iteration Statements Or Loops In Java
  • Java's Iteration statements are for, while and do-while. These statements are commonly called as loops. A loop repeatedly executes the same set of instructions until a ...Read More

5.4.2 while Loop In Java
  • The while loop is Java’s most fundamental looping statement. It repeats statement or block while its controlling expression (termination condition) is true. The while ...Read More

5.4.3 for Loop In Java
  • The for statement in Java provides a compact way to iterate over a range of values. We often call it as the "for loop" because of the way in which it repeatedly loops as long ...Read More

5.4.4 for Vs while Loop In Java
  • Although for and while are both loops, there are few differences in the syntax and the usage. for loop is used when we know the number of iterations we have to perform i.e....Read More

5.4.5 do while Loop In Java
  • do-while loop is similar to while loop statement but do-while loop executes all the statements in the block, at least once and then checks the condition at the end. If the condition...Read More

5.4.6 Nested Loops in Java
  • Nested loops are very common in programming, when we have to call a loop multiple times. For e.g., printing 1 2 3 4 5, can be done using a loop, but for printing it four ...Read More

5.4.7 Nested While Loop In Java
  • Placing one while loop with in the body of another while is called Nested while loop in java programm. Similar to nested loop. Nesting while, do-while will work similar to ...Read More

5.4.8 Nested for Loop In Java
  • Placing one for loop with in the body of another for is called nested for loop. This kind of nesting is commonly used in programming. Having loops inside loops, which are ...Read More

5.4.9 for Loop Example Program In Java - Sum Of Numbers
  • This example finds the sum of all numbers till a given input number using for Loop In Java. e.g., if the input is 6, then the sum is 1 + 2 + 3 + 4 + 5 + 6 = 21; So, it sequentially add ...Read More

5.4.10 Factorial Program In Java Using for Loop
  • This program calculates the factorial of the given number using for Loop In Java. The program finds the factorial of the given number 6, by multiplying the variable factorial with...Read More

5.4.11 Factorial Program In Java Using While Loop
  • This is Java Program " Factorial Using While Loop". The Java Program shows how to Calculate The factorial of a given number using while Loop. It finds the factorial of the ...Read More

5.5 Jump Statements
5.5.1 Jump Statements In Java
  • Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program. These statements can be used inside...Read More

5.5.2 Using Break In for Loop To Exit
  • By using break, you can force immediate termination of a loop even though there are iterations to be completed. e.g., In the following program which prints numbers from 1 to...Read More

5.5.3 Using break in switch case Statement
  • break can be used to come out of the switch statement. It is compulsory to put a break statement after each case block, then only we can stop the execution. If there is no break it...Read More

5.5.4 Using Java Break Statements as Java Goto
  • Java does not support goto to avoid complex code. When we use lots of goto's it will be very difficult to understand that code. break when used with label's can be a substitute for...Read More

5.5.5 Using break In Nested Loop Java Program
  • We can also use breaks in nested loop programs. Here is the example of using break statement in nested loops. The loops are for loops. If we put break in inner loop then it will...Read More

5.5.6 Java continue Statement
  • continue stops a particular iteration in a loop. e.g., If a loop has 15 iterations and having 5 lines of code, if in iteration 10 the continue statement is encountered at line 3, then...Read More

5.5.7 Java return Statement
  • return statement is used to explicitly return from a method. It causes the program control to transfer back to the caller of the method. return can be used to return from...Read More

5.6 Java for loops vs Java while loops vs Java do while loops
  • Loops repeatedly executes the same set of instructions until a termination condition is met. We have three loops in Java. for loop, while loop & do-while loop. Working : When the loop...Read More

Dependent Topics : 4. Operators 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App