Presentation3 - Control Statements

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Control Statements in Java

P.Kavitha AP-CSE KCET


 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

P.Kavitha AP-CSE KCET


1) If Statement:
 is used to evaluate a condition.
 The condition of the If statement gives a Boolean
value, either true or false.
 In Java, there are four types of if-statements given
below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement

P.Kavitha AP-CSE KCET


 Syntax :
if(condition) {
statement 1;
}
 Example:
 public class Student {
◦ public static void main(String[] args) {
◦ int x = 55;
◦ if(x>49 ) {
 System.out.println("x is greater than or equal to 50");
 }
 }
 }

P.Kavitha AP-CSE KCET


 Syntax :
if(condition) {
statement 1;
}
else{
statement 2;
}
 Example:
 public class Student {
public static void main(String[] args) {
int x = 55;
if(x>49 ) {
 System.out.println("x is greater than or equal to 50 - Pass");
 }
else {
System.out.println("x is less than 50- Fail");
}
 }
 }

P.Kavitha AP-CSE KCET


 The if-else-if statement contains the if-statement followed by multiple else-
if statements.
 Syntax:

if(condition 1) {
statement 1;
}
else if(condition 2) {
statement 2;
}
else if(condition 3) {
statement 3;
}
else {
statement 4;
}

P.Kavitha AP-CSE KCET


class ifelseifDemo {
public static void main(String args[])
{
int i = 20;

if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}

P.Kavitha AP-CSE KCET


 The if statement can contain a if or if-else statement inside
another if or else-if statement.
 Syntax:
 if(condition 1) {
 statement 1;
 if(condition 2) {
 statement 2;
 }
 else{
 statement 2;
 }
 }

P.Kavitha AP-CSE KCET


 class NestedIfDemo {
 public static void main(String args[])
 {
 int a=10, b=20, c=15;

 if (a>b{
 if (a>c){
 System.out.println(“a is greater"); }
 else {
System.out.println(“c is greater");
 }
 else{
 if (b>c{
 System.out.println(“b is greater"); }
 else {
System.out.println(“c is greater");
 }
 }
 }
 }

P.Kavitha AP-CSE KCET


 Points to be noted about switch statement:
 The case variables can be int, short, byte, char, or enumeration. Cases cannot be duplicate
 Default statement is executed when any of the case doesn't match the value of expression. It is
optional.
 Break statement terminates the switch block when the condition is satisfied. It is optional, if
not used, next case is executed.
 The syntax to use the switch statement is given below.
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}

P.Kavitha AP-CSE KCET


public class VowelConsonant {
public static void main(String[] args) {
char ch = 'z';
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is vowel");
break;
default:
System.out.println(ch + " is consonant");
}
}
}

P.Kavitha AP-CSE KCET


 for loop
 while loop
 do-while loop
 for(initialization, condition, increment/decrement)
 {
 //block of statements
 }

P.Kavitha AP-CSE KCET


 while(condition)
 {
 //looping statements
 }

P.Kavitha AP-CSE KCET


 do
 {
 //statements
 } while (condition);

P.Kavitha AP-CSE KCET


 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.
 However, it breaks only the inner loop in the case of the nested loop.
 The break statement cannot be used independently in the Java program, i.e., it can
only be written inside the loop or switch statement.
 public class BreakExample {

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

 for(int i = 0; i<= 10; i++)
 {
 if(i==6)
 {
 break;
 }
 System.out.println(i);
 }
 }
 }

P.Kavitha AP-CSE KCET


public class Calculation {
public static void main(String[] args) {
a:
for(int i = 0; i<= 10; i++) {
b:
for(int j = 0; j<=15;j++) {
c:
for (int k = 0; k<=20; k++) {
if(k==5)
{ break a; }
System.out.println(k);

}
}
}
}

P.Kavitha AP-CSE KCET


 The continue statement is used in loop control structure when you need to
jump to the next iteration of the loop immediately. It can be used with for loop
or while loop.
 The Java continue statement is used to continue the loop. It continues the
current flow of the program and skips the remaining code at the specified
condition. In case of an inner loop, it continues the inner loop only.
 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);
 }
 }
 }

P.Kavitha AP-CSE KCET


The java.lang.System.exit() method exits current program by terminating running Java virtual machine
exit(0) : Generally used to indicate successful termination.
exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination.
 import java.util.*;
 import java.lang.*;
 class GfG
 {
 public static void main(String[] args) {
 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
 for (int i = 0; i < arr.length; i++) {
 if (arr[i] >= 5) {
 System.out.println("exit...");
 // Terminate JVM
 System.exit(0);
 }
 else
 System.out.println("arr["+i+"] = " +
 arr[i]);
 }
 System.out.println("End of Program");
 }
 }

P.Kavitha AP-CSE KCET

You might also like