[go: up one dir, main page]

0% found this document useful (0 votes)
7 views28 pages

Unit1 Contro Statements

The document outlines Java control flow statements, which include decision-making statements (if, switch), loop statements (for, while, do-while, for-each), and jump statements (break, continue). It provides syntax and examples for each type of statement, demonstrating their usage in Java programming. The document serves as a comprehensive guide for understanding and implementing control flow in Java.

Uploaded by

anuja.it
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)
7 views28 pages

Unit1 Contro Statements

The document outlines Java control flow statements, which include decision-making statements (if, switch), loop statements (for, while, do-while, for-each), and jump statements (break, continue). It provides syntax and examples for each type of statement, demonstrating their usage in Java programming. The document serves as a comprehensive guide for understanding and implementing control flow in Java.

Uploaded by

anuja.it
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/ 28

Java Control Statements |

Control Flow in Java


Java Control Statements | Control Flow in Java

• Java provides three types of control flow statements.


• Decision Making statements
– if statements
– switch statement
• Loop statements
– do while loop
– while loop
– for loop
– for-each loop
• Jump statements
– break statement
– continue statement
Decision-Making statements:

• Decision-making statements decide which


statement to execute and when.
• 1) If Statement:
• Simple if statement
• if-else statement
• if-else-if ladder
• Nested if-statement
1) Simple if statement:

if(condition) {
statement 1; //executes when condition is true
}
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
2) if-else statement

• Syntax:
• if(condition) {
• statement 1; //executes when condition is true
• }
• else{
• statement 2; //executes when condition is false
• }
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
3) if-else-if ladder:

Syntax of if-else-if statement is given below.


if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
public class Student {
public static void main(String[] args) {
String city = "Delhi";
if(city == "Meerut") {
System.out.println("city is meerut");
}else if (city == "Noida") {
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}
Output:
Delhi
Nested if-statement

• Syntax of Nested if-statement is given below.


• if(condition 1) {
• statement 1; //executes when condition 1 is true
• if(condition 2) {
• statement 2; //executes when condition 2 is true
• }
• else{
• statement 2; //executes when condition 2 is false
• }
• }
Class oddoreven
{
Public static void main(String args[])
{
int a=5;
If(a>0)
{
If(a%2==0)
System.out.println(“a is even”);
else
System.out.println(“a is odd);
}
else
{
If(a%2==0)
System.out.println(“a is even”);
else
System.out.println(“a is odd);
}
}}
Switch Statement:
The switch statement contains multiple blocks of code called cases and a single case is
executed based on the variable which is being switched.
The syntax to use the switch statement is given below.
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
public class Student
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
case 2:
System.out.println("number is 2");
break;

default:
System.out.println(num);
}
}
}
Output:
number is 2
Loop Statements

• for loop
• while loop
• do-while loop
Java for loop

• for(initialization, condition, increment/


decrement) {
• //block of statements
• }
Calculation.java
public class Calculattion {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int j = 1; j<=10; j++) {
sum = sum + j;
}
System.out.println("The sum of first 10 natural numbers is " + sum);
}
}
Output:
The sum of first 10 natural numbers is 55
Java for-each loop

• Java provides an enhanced for loop to traverse


the data structures like array or collection.

• for(data_type var : array_name/


collection_name){
• //statements
• }
Calculation.java
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] names = {"Java","C","C++","Python","JavaScript"};
System.out.println("Printing the content of the array names:\n");
for(String name:names) {
System.out.println(name);
}
}
}
Output:
Printing the content of the array names: Java C C++ Python JavaScript
Java while loop

• The syntax of the while loop is given below.


• while(condition){
• //looping statements
• }
Calculation .java
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
while(i<=10) {
System.out.println(i);
i = i + 2;
}
}
}
Output:
Printing the list of first 10 even numbers 0 2 4 6 8 10
Java do-while loop

• do
• {
• //statements
• } while (condition);
Calculation.java
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
do {
System.out.println(i);
i = i + 2;
}while(i<=10);
}
}
Output:
Printing the list of first 10 even numbers 0 2 4 6 8 10
Jump Statements

• Jump statements are used to transfer the


control of the program to the specific
statements. In other words, jump statements
transfer the execution control to the other
part of the program. There are two types of
jump statements in Java,
• i.e., break and continue.
Java break statement

• the break statement


• is used to break the current flow of the program and transfer the control to the next statement
outside a loop or switch statement
public class BreakExample {

public static void main(String[] args) {


// TODO Auto-generated method stub
for(int i = 0; i<= 10; i++) {
System.out.println(i);
if(i==6) {
break;
}
}
}
}
Output:
0123456
Java continue statement

• Unlike break statement, the


continue statement
doesn't break the loop, whereas, it skips the
specific part of the loop and jumps to the next
iteration of the loop immediately.
public class ContinueExample {

public static void main(String[] args) {


// TODO Auto-generated method stub

for(int i = 0; i<= 2; i++) {

for (int j = i; j<=5; j++) {

if(j == 4) {
continue;
}
System.out.println(j);
}
}
}

}
Output:
012351235235

You might also like