Flat Preloader Icon

Core Java Statements

Set of fundamental statements

These statements are used to control the flow of a program, define classes and methods, manipulate data, and perform various operations. Here are some of the core Java statements:

Core Java Statements

If Statement:

  • Use if to specify a block of code to be executed, if a specified condition is true
				
					•	Conditional Statement - if and else 

Keyword and Syntax for if and else Statement:

The syntax of if and else statement is either 
one of these two:

If(condition){
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
  }

Example1:

assign var a=15;

If(a > 10) {
  greeting = "Good day";
} else {
  greeting="Good evening";
}

Example2:

If (booleanExpression) {
statement(s)
}
Or
If (booleanExpression) {
 
}
Else
{

}

				
			

While Statement

  • The while loop loops through a block of code as long as a specified condition is true:
				
					•	The syntax of while statement is:

While (booleanExpression)
{
statement(s)
}

				
			

Do-while Statement

  • The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
				
					•	The syntax of do-while statement is:

Do {
statement(s)
}
While (booleanExpression);

				
			

For Statement

    • When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
				
					•	The for statement has following syntax:
For (init; booleanExpression; update)
{
statement (s)
}

				
			

Break Statement

    • You have already seen the break statement used in an earlier chapter of this tutorial. It was used to “jump out” of a switch statement.
				
					•	The Break Statement is used to break from 
an enclosing
for, while, do and switch statement.

•	Cannot used elsewhere as we get a 
compilation error.

•	E.g.
for (int i=0; i < 10; i++){ if(i==6)
{
break;
}
System.out.println(i);
}

				
			

Continue Statement

    • The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
				
					•The Continue Statement is like Break, but it 
stops onlythe execution of current statement
and causes control to return to next iteration.

•	E.g.
for (int i=0; i < 10; i++){ if(i==6){
continue;
}
System.out.println(i);
}

				
			

Switch Statement

				
					•	The syntax of the switch statement
is as follows: Switch (expression)
{
Case Value_1:
statement(s); break;
Case value_2:
statement(s); break;
.
.
.
Case value_n:
statement(s); break;
Default:
statement(s);
}