0% found this document useful (0 votes)
5 views

Topic 5_Control Statement in Java

The document provides an overview of Java control statements, including selection statements (if, if-else, nested if-else, and switch-case) and iterative statements (for, while, and do-while loops). It explains how these statements control the flow of execution in a program, with examples of their syntax and usage. Additionally, it covers jump statements like break and continue, which alter the normal sequence of execution.

Uploaded by

joyikehi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Topic 5_Control Statement in Java

The document provides an overview of Java control statements, including selection statements (if, if-else, nested if-else, and switch-case) and iterative statements (for, while, and do-while loops). It explains how these statements control the flow of execution in a program, with examples of their syntax and usage. Additionally, it covers jump statements like break and continue, which alter the normal sequence of execution.

Uploaded by

joyikehi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA CONTROL STATEMENT

JAVA CONTROL STATEMENT


Control statements enable us to
specify the order in which the various
instructions in the program are to be
executed.

They define how the control is


transferred to other parts of the
program
SELECTION STATEMENT Java if Statement

Selection statement is used to The syntax of the if statement is:


perform the task of the conditional
operations. if (test expression)
{
Java programming has four types of statements:
// code
}
1. if statement
2. if-else statement Java if...else Statement
3. Nested if-else statement
The if statement may have an optional else block.
4. Switch-case statement The syntax of the if..else statement is:

For example, assigning grades (A, B, C) based on the


if (test expression) {
percentage obtained by a student.
// run code if test expression is true
if the percentage is 70 and above, assign grade A }
if the percentage is between 60-69, assign grade B else {
if the percentage is between 50-59, assign grade C
// run code if test expression is false
if the percentage is between 45-49, assign grade D
if the percentage is below 45, assign grade F }
Java if...else Ladder
Nested if...else
The if...else ladder allows you to check between
It is possible to include an if...else statement multiple test expressions and execute different
inside the body of another if...else statement. statements.

Syntax of Nested if...else


Syntax of if...else Ladder
if (test expression1) {
if (test expression1) {
// statement(s)
if(test expression2) {
}
// statement(s)
else if(test expression2) {
}
// statement(s)
else {
}
// statement(s)
else if (test expression3) {
}
// statement(s)
else {
}
// statement(s)
.
}
.
else {
// statement(s)
}
Java switch statement
The switch statement allows us to execute one code block among many alternatives.

Syntax of switch...case
How does the switch statement work?
switch (expression)
{
The expression is evaluated once and compared
case constant1:
with the values of each case label.
// statements
break; • If there is a match, the corresponding
statements after the matching label are
case constant2: executed. For example, if the value of the
// statements expression is equal to constant2, statements
break; after case constant2: are executed until break
. is encountered.
. • If there is no match, the default statements are
default: executed.
// default statements
}
Java Program Loops and Iteration

A loop statement allows us to execute a How for loop works?


statement or group of statements multiple
• The initialization statement is executed only
times until the specified condition is met.
once.
For example, if you want to show a • Then, the test expression is evaluated. If the
message 100 times, then rather than test expression is evaluated to false, the for
typing the same code 100 times, you can loop is terminated.
use a loop. • However, if the test expression is evaluated to
true, statements inside the body of the for loop
Java programming has three types of are executed, and the increment is applied.
iterative statements: • Again the test expression is evaluated.
for Loop • This process goes on until the test expression is
false. When the test expression is false, the
The syntax of the for loop is:
loop terminates.
for (initializationStatement; testExpression;
Increment)
{
// statements inside the body of loop
}
// Program to print numbers from 1 to 5

class Main {
if false if true public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println(i);
}
}
// Program to print a text 5 times using for Loop }

class Main { // Infinite for Loop


public static void main(String[] args) {
class Infinite {
int n = 5; public static void main(String[] args) {
// for loop int sum = 0;
for (int i = 1; i <= n; ++i) {
System.out.println("Java is fun"); for (int i = 1; i <= 10; --i) {
} System.out.println("Hello");
} }
} }
}
while loop
The test expression is evaluated before How while loop works?
The body of the while loop is executed.
• The while loop evaluates the testExpression inside
The syntax of the while loop is:
the parentheses ().
while (testExpression) { • If testExpression is true, statements inside the
// the body of the loop
body of while loop are executed. Then,
}
testExpression is evaluated again.
• The process goes on until testExpression is
evaluated to false.
• If testExpression is false, the loop terminates
(ends).
do...while loop
The syntax of the do...while loop is:
The body of do...while loop is executed at do {
least once. Only then, the test expression // the body of the loop
is evaluated. }
while (testExpression);

How do...while loop works?

• The body of do...while loop is executed once.


Only then, the testExpression is evaluated.
• If testExpression is true, the body of the loop is
executed again and testExpression is evaluated
once more.
• This process goes on until testExpression
becomes false.
• If testExpression is false, the loop ends.
// Program to display numbers from 1 to 5 // Java Program to display numbers from 1 to 5

class Main { import java.util.Scanner;

public static void main(String[] args) { // Program to find the sum of natural numbers
from 1 to 100.
// declare variables
int i = 1, n = 5; class Main {
public static void main(String[] args) {
// while loop from 1 to 5
int i = 1, n = 5;
while(i <= n) {
System.out.println(i); // do...while loop from 1 to 5
do {
i++; System.out.println(i);
} i++;
} while(i <= n);
} }
} }
Java break
Jump statements
The break statement ends the loop immediately
Jump statements change execution from when it is encountered.
its normal sequence.
Its syntax is:
When execution leaves a scope, all
automatic objects that were created in
that scope are destroyed. break;

Java continue

The continue statement skips the current Java goto Statement


iteration of the loop and continues with the
next iteration.
The goto statement allows us to transfer
control of the program to the specified label.
Its syntax is:
continue; Syntax of goto Statement
goto label;
... .. ...
... .. ...
label:
statement;
// Program to show break statement // Java Program to show continue statement

class Test { class Main {

public static void main(String[] args) { public static void main(String[] args) {

// for loop
// for loop
for (int i = 1; i <= 10; ++i) {
for (int i = 1; i <= 10; ++i) {
// if value of i is between 4 and 9
// if the value of i is 5 the loop terminates
// continue is executed
if (i == 5) {
if (i > 4 && i < 9) {
break;
continue;
}
}
System.out.println(i);
System.out.println(i);
}
}
}
}
}
}

You might also like