0% found this document useful (0 votes)
1 views6 pages

Controlstatements Examples

The document provides an overview of control statements in Java, including selection statements like if and switch, iteration statements such as while, do-while, and for-each, as well as jump statements like break, continue, and return. It explains the syntax and usage of these statements with examples to illustrate their functionality. Overall, it serves as a guide for understanding how to control the flow of a Java program based on conditions and loops.

Uploaded by

jyothisb3389
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)
1 views6 pages

Controlstatements Examples

The document provides an overview of control statements in Java, including selection statements like if and switch, iteration statements such as while, do-while, and for-each, as well as jump statements like break, continue, and return. It explains the syntax and usage of these statements with examples to illustrate their functionality. Overall, it serves as a guide for understanding how to control the flow of a Java program based on conditions and loops.

Uploaded by

jyothisb3389
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
You are on page 1/ 6

Control Statements in JAVA

1.Selection Statements in JAVA:

Java supports two selection statements

if statement and
switch Statement

These statements allow you to control the flow of your program’s execution based upon conditions known
only during run time.

if statement : The if statement is Java’s conditional branch statement. It can be used to route program
execution through two different paths.

general form of the if statement:

if (condition) statement1;
else statement2;

Here, each statement may be a single statement or a compound statement enclosed in curly
braces (that is, a block). The condition is any expression that returns a boolean value. The else
clause is optional.

The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed. In no case will both statements be executed.

Example Program:

class IfTest1{
public static void main(String args[])
{
int x=2;
if(x==3) {
System.out.println(“ x must be 3”);
}
else {
System.out.println(“ x is not 3”);
}​
System.out.println(“ This runs no matter what”);
}
}

Nested ifs

public class NestedIfExample {


public static void main(String[] args) {
//Creating two variables for age and weight
int age=20;
int weight=80;
//applying condition on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
}
}
}
}

The if-else-if Ladder


A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder. It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;

Example
// Demonstrate if-else-if statements.

class IfElseExample {
public static void main(String args[]) {
int month = 4; // AprilString season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}

switch
The switch statement is Java’s multiway branch statement.
the general formof a switch statement:

switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}

Example :

// A simple example of the switch.


class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}

Iteration Statements
Java’s iteration statements are for, while, and do-while.
These statements create what we commonly call loops.

while
The while loop is Java’s most fundamental loop statement. It repeats a statement or block
while its controlling expression is true. Here is its general form:

while(condition) {
// body of loop
}

The condition can be any Boolean expression. The body of the loop will be executed as long
as the conditional expression is true. When condition becomes false, control passes to the
next line of code immediately following the loop. The curly braces are unnecessary if only
a single statement is being repeated

Example:
class WhileExample {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick " + n);
n--;
}
}
}

do while

Example

// Demonstrate the do-while loop.

class DoWhileExample {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}

for each

The for-each style for automates the preceding loop. Specifically, it eliminates the need
to establish a loop counter, specify a starting and ending value, and manually index the
array. Instead, it automatically cycles through the entire array, obtaining one element at
a time, in sequence, from beginning to end. For example, here is the preceding fragment
rewritten using a for-each version of the for:

the for-each style for automatically cycles through an array in sequence from the lowest index to the
highest.
Although the for-each for loop iterates until all elements in an array have been examined,
it is possible to terminate the loop early by using a break statement. For example, this program
sums only the first five elements of nums:

// Use a for-each style for loop.

class ForEachExample {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
// use for-each style for to display and sum the values
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
if(x==5) break; // if you wish to break the loop
}
System.out.println("Summation: " + sum);
}
}

Jump Statements
Java supports three jump statements: break, continue, and return. These statements transfer
control to another part of your program.

Example continue :

public class ContinueExample {


public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
}

// Demonstrate continue.
class ContinueExample1 {
public static void main
(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 == 0) continue;
System.out.println("");
}
}
}

return
The last control statement is return.
The return statement is used to explicitly return from a method. That is, it causes program control to
transfer back to the caller of the method.

At any time in a method the return statement can be used to cause execution to branch
back to the caller of the method. Thus, the return statement immediately terminates the
method in which it is executed.

// Demonstrate return.
class ReturnExample {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}

You might also like