0% found this document useful (0 votes)
5 views4 pages

Java

The document provides an overview of the Scanner class in Java, which is used for user input, and explains its methods for reading various data types. It also covers control statements such as break, continue, and switch, along with if-else statements for decision-making in programs. Additionally, it describes different types of loops (for, while, and do-while) for executing code repeatedly based on conditions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java

The document provides an overview of the Scanner class in Java, which is used for user input, and explains its methods for reading various data types. It also covers control statements such as break, continue, and switch, along with if-else statements for decision-making in programs. Additionally, it describes different types of loops (for, while, and do-while) for executing code repeatedly based on conditions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Scanner Class in Java

The Scanner class in Java is used to take input from the user during program execution. It is a
part of the [Link] package and provides methods to read different types of data such as
integers, floating-point numbers, strings, and booleans from the keyboard or other input
sources.

To use the Scanner class in a Java program, the [Link] package must be imported.
import [Link];

After importing, we need to create an object of the Scanner class to read input.

Syntax:

Scanner sc = new Scanner([Link]);

● Scanner → class name

● sc → object name

● [Link] → standard input stream Purpose


(keyboard) The Scanner class provides
several built-in methods to read
different types of input Method

nextInt() Reads an integer value

nextFloat() Reads a float value

nextDouble() Reads a double value

next() Reads a single word

nextLine() Reads a complete line of text

nextBoolean() Reads a boolean value


Control Statements
Control statements are used to control the flow of execution of a program. They allow the
program to change the normal sequence of execution based on conditions or special commands.
Some common control statements are break, continue, goto (labelled statement), and loop
control like do-while.

Break Statement
The break statement is used to terminate a loop or switch statement immediately. When the
break statement is executed, the program exits the loop and continues with the next statement
after the loop. It is commonly used when a certain condition is met and further repetition is not
required.

Continue Statement
The continue statement is used to skip the current iteration of a loop and move to the next
iteration. When continue is encountered, the remaining statements in that loop cycle are
skipped, and the loop starts the next cycle.

Jump Statement
A jump statement is used to transfer control from one part of the program to another. In Java,
statements such as break and continue are considered jump statements because they change the
normal flow of program execution and move control to another point in the program.

Switch Statement
The switch statement in Java is a multi-way decision statement that executes code based on the
value of an expression. It is a cleaner alternative to an if-else-if ladder and supports types like
byte, short, char, int, long, enum, String, and wrapper classes.

If–Else Statement
The if–else statement in Java is used to make decisions in a program. It allows the program
to execute different blocks of code based on whether a condition is true or false. This is called
conditional or decision-making control.
The if statement checks a condition. If the condition is true, the statements inside the if block
are executed. If the condition is false, the statements inside the else block are executed.
The if–else statement helps programs behave differently depending on the input or situation.
It is commonly used for tasks such as checking whether a number is positive or negative,
determining whether a student has passed or failed, or comparing two values.

Java also supports multiple conditions using else if, which allows the program to check several
conditions one by one.

Types of If Statements
1. Simple if statement – Executes code only if the condition is true.

2. if–else statement – Executes one block if the condition is true and another if it is false.

3. else-if ladder – Used when there are multiple conditions to check.

Advantages
● Helps in decision making

● Makes programs more flexible and logical

● Allows execution of different actions based on conditions

For Loop:
The for loop in Java is a control flow statement used to execute a block of code repeatedly
based on a condition. It is especially useful when the number of iterations is known in advance,
such as iterating over a range of values, arrays, or collections.

While Loop
Java while loop is a control flow statement used to execute the block of statements repeatedly
until the given condition evaluates to false. Once the condition becomes false, the line
immediately after the loop in the program is executed.

Do-While Loop
The do-while loop is a control statement that repeats a block of code until a given condition
becomes false. In this loop, the statements are executed at least once, because the condition is
checked after the execution of the loop body.

You might also like