You are on page 1of 7

Control Structures

Control structure includes the looping control structure and conditional or branching control structure. In a looping control structure, series of statements are repeated. In a conditional or branching control structure, a portion of the program is executed once depending on what conditions occur in the code.

Loops
The purpose of loop statements is to repeat Java statements more than times. There are several kinds of loop statements in Java.

For loop:
For loop is used to execute a block of code continuously to accomplish a particular condition. For statement consists of tree parts i.e. initialization, condition, and increment/decrement.

Initialization:
It is an expression that sets the value of the loop control variable. It executes only once.

Condition:
This must be a boolean expression. It tests the loop control variable against a target value and hence works as a loop terminator.

Increment/decrement:
It is an expression that increments or decrements the loop control variable.

Example:
int a; for (a=0; a<=10; a++) { System.out.println("a = " +a); }

While loop:
In while loop the statement(s) will be executed as long as Expression evaluates to true. If there is only a single statement inside the braces, you may omit the braces.

Example:
class counting{ public static void main (String args[]){ int i = 0; while (i < =5){ System.out.println("count"); i = i + 1; } } }

Do-while loop:
The do-while statement is like the while statement, except that the associated block always gets executed at least once.

Syntax:
do { statement (s) } while (Expression);

Example:
public class MyClass { public static void main(String[] args) { int i = 0; do { System.out.println(i); i++; } while (i < 10); } }

if statement:

The if statement is the simple form of control flow statement. It directs the program to execute a certain section of code if and only if the test evaluates to true. That is the if statement in Java is a test of any boolean expression.

Syntax
if (Expression) { statement (s) } else { statement (s) }

Example:
public class MainClass { public static void main(String[] args) { int a = 3; if (a > 3) a++; else a = 3; } }

Example:
class IfElseexamp { public static void main(String[] args) {

int testscore = 66; char grade;

if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } } The output will be D

The Switch Statement:


The Switch Statement is an alternative to a series of else if is the switch statement. Unlike if-then and if-then-else, the switch statement allows for any number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types. The body of a switch statement is known as a switch block. Any statement immediately contained by the switch block may be labeled with one or more case or default labels. The switch statement evaluates its expression and executes the appropriate case.

Switch Statement Syntax:


switch (expression) { case value_1 : statement (s); break; case value_2 : statement (s); break; . . . case value_n : statement (s); break; default: statement (s); }

Example:
class Switchexamp { public static void main(String[] args) { int month = 5; switch (month) {

case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break;

case 12: System.out.println("December"); break; default: System.out.println("Wrong Input"); break; } } } The output will be May

You might also like