0% found this document useful (0 votes)
14 views18 pages

JAVA Statements

Java PPT in Simple Way
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

JAVA Statements

Java PPT in Simple Way
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

JAVA Statements

4. Java Statements
1. Input and Output Statements:
2. Conditional Statements: if,if-else,if-else-ladder,nested-if-else,switch
3. Iterative Statements: for,for-each, while, do-while,
4. Transfer Statements: break, continue
1.Input and Output Statements

Java Input Statement


Java provides different ways to get input from the user. However, you will learn
to get input from user using the object of Scanner class.
In order to use the object of Scanner, we need to import java.util.Scanner
package.
Import java.util.Scanner;
Then, we need to create an object of the Scanner class. We can use the object to
take input from the user.

// create an object of Scanner


Scanner input = new Scanner(System.in);
// take input from the user
int number = input.nextInt();
1.Input and Output Statements
Java Output Statements
• System.out.println(); or
• System.out.print(); or
• System.out.printf();
to send output to standard output (screen).
Difference between println(), print() and printf()
print() - It prints string inside the quotes.
println() - It prints string inside the quotes similar like print() method.
Then the cursor moves to the beginning of the next line.
printf() - It provides string formatting (similar to printf in C/C++
programming).
2. Conditional Statement: if

The syntax of an if-then statement is:

if (condition) {
// statements
}
Here, condition is a boolean expression such as age >= 18.

if condition evaluates to true, statements are executed


if condition evaluates to false, statements are skipped
if-else

The if statement executes a certain section of code if the test expression is evaluated to
true. However, if the test expression is evaluated to false, it does nothing.
The syntax of the if...else statement is:

if (condition) {
// codes in if block
}
else {
// codes in else block
}
if-else ladder
In Java, we have an if...else...if ladder, that can be used to execute one block of code among
multiple other blocks.
if (condition1) {
// codes
}
else if(condition2) {
// codes
}
else if (condition3) {
// codes
}
.
else {
// codes
}
Nested if-else
In Java, it is also possible to use if..else statements inside an if...else
statement. It's called the nested if...else statement.
Switch
The switch statement allows us to execute a block of code among many alternatives.
The syntax of the switch statement in Java is:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
...
...

default:
// default statements
}
3. Iterative Statements
In computer programming, loops are used to repeat a block of code. For
example, if you want to show a message 100 times, then rather than
typing the same code 100 times, you can use a loop.

In Java, there are three types of loops.

• for loop
• for each loop
• while loop
• do...while loop
Java for Loop

Java for loop is used to run a block of code for a certain number of
times. The syntax of for loop is:
for (initialExpression; testExpression; updateExpression) {
// body of the loop
}
Java for-each Loop
The Java for loop has an alternative syntax that makes it easy to iterate through
arrays and collections. For example,

The syntax of the Java for-each loop is:


for(dataType item : array) {
...
}
Here,

array - an array or a collection


item - each item of array/collection is assigned to this variable
dataType - the data type of the array/collection
Java for-each Loop
// print array elements
class Main
{
public static void main(String[] args)
{
// create an array
int[] numbers = {3, 7, 5, -5};
// iterating through the array
for (int number: numbers)
{
System.out.println(number);
}
}
}
Java while Loop
• Java while loop is used to run a specific code until a certain condition
is met. The syntax of the while loop is:
Initialisation
while (testExpression) {
// body of loop
Incrementation/decrementation
}
Java do...while loop

The do...while loop is similar to while loop. However, the body of


do...while loop is executed once before the test expression is checked.
For example,
initialisation
do {
// body of loop
Incrementation/decrementation;
} while(textExpression);
4.Transfer Statements: break
• While working with loops, it is sometimes desirable to skip some
statements inside the loop or terminate the loop immediately without
checking the test expression.
• In such cases, break and continue statements are used.
• The break statement in Java terminates the loop immediately, and the
control of the program moves to the next statement following the loop.
Here is the syntax of the break statement in Java:
break;
How break statement works?
continue
• The continue statement skips the current iteration of a loop (for, while,
do...while, etc).
• After the continue statement, the program moves to the end of the
loop. And, test expression is evaluated (update statement is evaluated
in case of the for loop).

Here's the syntax of the continue statement.


continue;
Note: The continue statement is almost always used in decision-making
statements (if...else Statement).

You might also like