Control Statements in java
- if Statements
- switch Statement
- while Loop
- do..while Loop
- for Loop
if Statements
Three Forms of the if Statement
- “if”—use when you want to do one thing or nothing.
- “if, else”—use when you want to do one thing or another thing.
- “if, else if”—use when there are three or more possibilities.
The usual logical conditions from mathematics in java:
- Less than: a < b
- Greater than: a > b
- Less than or equal to: a <= b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
Syntax:
if
if (condition) {
// block of code to be executed if the
condition is true
}
|
Example:
|
b is grater then a
|
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
}
|
public class Pairosoft{
public static void main(String[] args){
int a=10;
if(a<1){
System.out.println("if.");}
else{
System.out.println("else.");}
}
}
|
else.
|
if (condition1) {
// block of code to be executed if
condition1 is true
} else
if (condition2) {
// block of code to be executed if the
condition1 is false and condition2 is true
} else
{
// block of code to be executed if the
condition1 is false and condition2 is false
}
|
public class Pairosoft{
public static void main(String[] args) {
int a=10;
if(a<1){
System.out.println("C++");}
else if(a<2){
System.out.println("PYTHON");}
else{
System.out.println("JAVA");}
}
}
|
JAVA
|
switch Statement
syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
|
Example:
public class Pairosoft{
public static void main(String[] args) {
int a=11,b=20;
char ch='+';
switch (ch) {
case '+':
System.out.println(a+b);
break;
case '-':
System.out.println(a-b);
break;
}
}
}
|
31
|
while Loop
Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop.
Example: i <= 10
Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value.
Example: i++;
Syntax:
Example:
Output:
Syntax:
Example:
Output:
Output:
while (test_expression)
{
// statements
update_expression;
}
|
public class Pairosoft{
public static void main(String[] args) {
int i = 1;
while (i <= 5) // test expression
{
System.out.println("java");
i++; // update expression }
}
}
}
|
java
java
java
java
java
|
do..while Loop
do
{
// loop body
update_expression
}
while
(test_expression);
|
Example:
public class Pairosoft{
public static void main(String[] args){
int i = 1;
do { // Print the statement
System.out.println("java");
i++; // update expression
}
while (i <= 5); // test expression
}
}
|
java
java
java
java
java
|
for Loop
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
|
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example:
public class Pairosoft{
public static void main(String[] args){
for(int i=1;i<=10;i++){
System.out.println(i);}
}
}
|
1
2
3
4
5
6
7
8
9
10
|
Common escape sequences
Escape Sequence
|
Description
|
\t
|
Move
the cursor to the next tab stop
|
\n
|
Newline-go to first column in next line
|
\r
|
Return
to first column in current line
|
\”
|
Print a literal double quote
|
\’
|
Print
a literal single quote
|
\\
|
Print a literal backslash
|
0 Comments