KEMBAR78
Control statements in java | PPTX
JAVA
P.Prathibha
Topics for Today’s Session
What is JAVA
Control Statements in Java
Selection Statements
Looping Statements
Jumping Statements
Control Statements
 The Java control statements inside a program are usually
executed sequentially.
 Sometimes a programmer wants to break the normal flow and
jump to another statement or execute a set of statements
repeatedly.
 Control statements in java enables decision making, looping and
branching.
 A control statement in java is a statement that determines
whether the other statements will be executed or not.
 It controls the flow of a program.
 Control Statements can be divided into three categories,
namely
 Selection statements
 Iteration statements
 Jump statements
Decision-Making Statements
 Conditional Control Statements allows the program to
select between the alternatives during the program
execution.
 They are also called as decision-making statements or
selection statements.
 Statements that determine which statement to execute
and when are known as decision-making statements.
 These statements decides the flow of the execution based
on some conditions.
 Java’s Selection statements:
 if
 if-else
 nested-if
 if-else-if
 switch-case
Simple if statement
 if statement is the most simple decision making statement
 The if statement determines whether a code should be executed based on
the specified condition.
 if statement’s condition should evaluate only to the boolean values ‘true’
or ‘false’.
 We can add more than one condition in the same if statement by using
&& or || operators.
if(condition)
{
// statements (if Block)
}
//other statements
class IfDemo1 {
public static void main(String[] args)
{
int marks=70;
if(marks > 65)
{
System.out.print("Hello Java! Am in If");
}
}
}
if-then -else Statement
 The if-else statement is used for testing conditions.
 It is used for true as well as for false condition.
 If then Else statement provides two paths.
 The if block is executed when the condition holds true.
When the condition evaluates to false, the statements inside
the else block are executed.
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
contd..
// Java program to illustrate if-else statement
class IfElseDemo {
public static void main(String args[])
{
int i = 20;
if (i < 45)
System.out.println("i is smaller than 45");
else
System.out.println("i is greater than 55");
}
}
Nested if statement
An if present inside an if block is known as a nested if block.
It is similar to an if..else statement, except they are defined inside another
if..else statement.
contd..
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
class NestedIfDemo {
public static void main(String args[]) {
int s = 15;
if (s > 30)
{ if (s%2==0)
System.out.println("s is an even number and greater than 30!");
else
System.out.println("s is a odd number and greater than 30!"); }
else {
System.out.println("s is less than 15"); }
System.out.println("Hello World!");
} }
If Else-If ladder:
 If the condition is true, then it will execute the If block.
 Otherwise, it will execute the Else-If block.
 Again, if the condition is not met, then it will move to the else block.
 if-else-if ladder is used to decide among multiple options.
 The if statements are executed from the top down.
 As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed.
 If none of the conditions is true, then the final else statement will be
executed.
if(condition1)
{
//code for if condition1 is true
}
else if(condition2)
{
//code for if condition2 is true
}
else if(condition3)
{
//code for if condition3 is true
}
...
else
{
//code for all the false conditions
}
Switch statement
 The switch statement is a multiway branch statement.
 A switch statement is used to execute a single statement from multiple conditions.
 The switch statement can be used with short, byte, int, long, enum types, etc.
 One or N number of case values can be specified for a switch expression.
 Dulplicate case values are not allowed. A compile-time error is generated by the
compiler if unique values are not used.
 The case value must be literal or constant. Variables are not permissible.
 Usage of break statement is made to terminate the statement sequence. It is optional..
If this statement is not specified, the next case is executed.
 Based on the argument in the switch statement suitable case value will be selected and
executed.
 If no matching case found, then the default will be executed.
Switch(variable/value/expression){
Case :
// statements [];
Case :
// statements [];
…
default:
// statements [];
}
Looping Statements / Iter
ative Statements
 These are used to execute a block of statements multiple
times.
 It means it executes the same code multiple times until a
specified condition is met,
 These are also called Iteration statements.
 There are three types of looping control statements:
1. For loop
2. While loop
3. Do-while loop
While loop
 While loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition.
 The while loop can be thought of as a repeating if statement.
 It is used for iterating a part of the program several times.
 When the number of iteration is not fixed then while loop is
used.
 While loop executes till the condition becomes false.
while (test_expression)
{
// statements
update_expression;
}
class WhileDemo {
public static void main(String[] args) {
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
} }
// Example for infinite while loop
do-while loop
 In Java, the do-while loop is used to execute a part of the program
again and again.
 do-while loop is an Exit control loop.
 If the number of iteration is not fixed then the do-while loop is
used.
 This loop executes at least once because the loop is executed before
the condition is checked.
 Therefore, unlike for or while loop, a do-while check for the
condition after executing the statements or the loop body.
do
{
//code for execution
}
while(condition);
// Program to display numbers 1 to 15 using do-while loop
class DoWhileDemo {
public static void main(String[] args) {
int i=1;
do {
System.out.println(i);
i++;
}while(i<=10);
} }
// Program for infinite do-while loop
class DoWhileDemoIn {
public static void main(String[] args)
{
do {
System.out.println("infinitive do while loop");
}while(true);
}
}
For loop:
 The for loop in java is used to iterate and evaluate a code multiple
times.
 When the number of iterations is known by the user, it is
recommended to use the for loop.
 In java there are 3 types of for loops, they are as follows:
1. Simple for loop
2. For-each loop
3. labelled for loop
for (initialization; condition; increment/decrement)
{
statement;
}
// Java program to illustrate for loop
class forLoopDemo {
public static void main(String args[]) {
// Writing a for loop
// to print Hello World 5 times
for (int i = 1; i <= 5; i++)
System.out.println("Hello World");
} }
// Program to display multiplication table
class ForDemo1 {
public static void main(String[] args) {
int n, i;
n=2;
for(i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
} }
Output:
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
For-Each loop:
 The traversal of elements in an array can be done by the for-
each loop.
 The elements present in the array are returned one by one.
 It must be noted that the user does not have to increment the
value in the for-each loop.
for(Type var:array)
{
//code for execution
}
public class ForEachDemo1
{
public static void main(String[] args) {
inta[]={100,101,102,103,104};
for(int i:a)
{ System.out.println(i);
}
} }
Output:
100
101
102
103
104
Labelled For Loop
 In Java, Labelled For Loop is used to give label before any
for loop. It is very useful for nesting for loop.
labelname:
for(initialization;condition;incr/decr)
{
//code for execution
}
public class LabeledForDemo {
public static void main(String[] args) {
num:
for(inti=1;i<=5;i++)
{
num1:
for(int j=1;j<=5;j++)
{ if(i==2&&j==2)
{
break num;
}
System.out.println(i+"
"+j);
} }
}
}
Output:
1 1
1 2
1 3
1 4
1 5
2 1
 Branching statements are used to jump from the current
executing loop.
 Branching statements in java are used to jump from a statement
to another statement, thereby the transferring the flow of
execution
break Statement
 break is a keyword.
 It is used within any control statements.
 break – Terminates the loop or switch statement and transfers
execution to the statement immediately following the loop or
switch.
 The break statement in java is used to terminate a loop and
break the current flow of the program.
 Syntax:
break; or
break <label>;
Unconditional Control Statements /
Jump Statements / Branching statements
// Using break statement in for loop
Output:
0
1
2
3
4
5
After loop
continue Statement
 continue is a keyword.
 In Java, the Continue statement is used in loops.
 Continue statement is used to jump to the next iteration of the loop
immediately.
 It is used with for loop, while loop and do-while loop.
Syntax:
continue; or
continue<label>;
class continueTest {
public static void main(String args[]) {
for (int j = 0; j &lt; 10; j++)
{
// If the number is odd then bypass and continue with next value
if (j%2 != 0)
continue;
// only even numbers will be printed
System.out.print(j + " ");
}
} }
Output: 0 2 4 6 8
//continue with label is used to continue the number of loops below
the label
class contlabelDemo{
public static void main(String args[]){
Termi:
for(int i=1;i<=4;i++)
{
for(int k=1;i<=4;k++)
{
System.out.print(“ ”+i+” ”);
if(i==3)
continue Termi;
System.out.println(“Hello”);
}
}
} }
Summary
 In this lesson you learnt about
 Control Statements
 Decision making statements
 Iterative statements
 Branching statements
Control statements in java

Control statements in java

  • 1.
  • 2.
    Topics for Today’sSession What is JAVA Control Statements in Java Selection Statements Looping Statements Jumping Statements
  • 3.
    Control Statements  TheJava control statements inside a program are usually executed sequentially.  Sometimes a programmer wants to break the normal flow and jump to another statement or execute a set of statements repeatedly.  Control statements in java enables decision making, looping and branching.  A control statement in java is a statement that determines whether the other statements will be executed or not.  It controls the flow of a program.  Control Statements can be divided into three categories, namely  Selection statements  Iteration statements  Jump statements
  • 5.
    Decision-Making Statements  ConditionalControl Statements allows the program to select between the alternatives during the program execution.  They are also called as decision-making statements or selection statements.  Statements that determine which statement to execute and when are known as decision-making statements.  These statements decides the flow of the execution based on some conditions.  Java’s Selection statements:  if  if-else  nested-if  if-else-if  switch-case
  • 6.
    Simple if statement if statement is the most simple decision making statement  The if statement determines whether a code should be executed based on the specified condition.  if statement’s condition should evaluate only to the boolean values ‘true’ or ‘false’.  We can add more than one condition in the same if statement by using && or || operators. if(condition) { // statements (if Block) } //other statements
  • 7.
    class IfDemo1 { publicstatic void main(String[] args) { int marks=70; if(marks > 65) { System.out.print("Hello Java! Am in If"); } } } if-then -else Statement  The if-else statement is used for testing conditions.  It is used for true as well as for false condition.  If then Else statement provides two paths.  The if block is executed when the condition holds true. When the condition evaluates to false, the statements inside the else block are executed.
  • 8.
    if (condition) { // Executesthis block if // condition is true } else { // Executes this block if // condition is false } contd.. // Java program to illustrate if-else statement class IfElseDemo { public static void main(String args[]) { int i = 20; if (i < 45) System.out.println("i is smaller than 45"); else System.out.println("i is greater than 55"); } }
  • 9.
    Nested if statement Anif present inside an if block is known as a nested if block. It is similar to an if..else statement, except they are defined inside another if..else statement. contd.. if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } } class NestedIfDemo { public static void main(String args[]) { int s = 15; if (s > 30) { if (s%2==0) System.out.println("s is an even number and greater than 30!"); else System.out.println("s is a odd number and greater than 30!"); } else { System.out.println("s is less than 15"); } System.out.println("Hello World!"); } }
  • 10.
    If Else-If ladder: If the condition is true, then it will execute the If block.  Otherwise, it will execute the Else-If block.  Again, if the condition is not met, then it will move to the else block.  if-else-if ladder is used to decide among multiple options.  The if statements are executed from the top down.  As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.  If none of the conditions is true, then the final else statement will be executed.
  • 11.
    if(condition1) { //code for ifcondition1 is true } else if(condition2) { //code for if condition2 is true } else if(condition3) { //code for if condition3 is true } ... else { //code for all the false conditions }
  • 12.
    Switch statement  Theswitch statement is a multiway branch statement.  A switch statement is used to execute a single statement from multiple conditions.  The switch statement can be used with short, byte, int, long, enum types, etc.  One or N number of case values can be specified for a switch expression.  Dulplicate case values are not allowed. A compile-time error is generated by the compiler if unique values are not used.  The case value must be literal or constant. Variables are not permissible.  Usage of break statement is made to terminate the statement sequence. It is optional.. If this statement is not specified, the next case is executed.  Based on the argument in the switch statement suitable case value will be selected and executed.  If no matching case found, then the default will be executed. Switch(variable/value/expression){ Case : // statements []; Case : // statements []; … default: // statements []; }
  • 14.
    Looping Statements /Iter ative Statements  These are used to execute a block of statements multiple times.  It means it executes the same code multiple times until a specified condition is met,  These are also called Iteration statements.  There are three types of looping control statements: 1. For loop 2. While loop 3. Do-while loop
  • 15.
    While loop  Whileloop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.  The while loop can be thought of as a repeating if statement.  It is used for iterating a part of the program several times.  When the number of iteration is not fixed then while loop is used.  While loop executes till the condition becomes false. while (test_expression) { // statements update_expression; }
  • 16.
    class WhileDemo { publicstatic void main(String[] args) { int i=1; while(i<=10) { System.out.println(i); i++; } } } // Example for infinite while loop
  • 17.
    do-while loop  InJava, the do-while loop is used to execute a part of the program again and again.  do-while loop is an Exit control loop.  If the number of iteration is not fixed then the do-while loop is used.  This loop executes at least once because the loop is executed before the condition is checked.  Therefore, unlike for or while loop, a do-while check for the condition after executing the statements or the loop body. do { //code for execution } while(condition);
  • 18.
    // Program todisplay numbers 1 to 15 using do-while loop class DoWhileDemo { public static void main(String[] args) { int i=1; do { System.out.println(i); i++; }while(i<=10); } } // Program for infinite do-while loop class DoWhileDemoIn { public static void main(String[] args) { do { System.out.println("infinitive do while loop"); }while(true); } }
  • 19.
    For loop:  Thefor loop in java is used to iterate and evaluate a code multiple times.  When the number of iterations is known by the user, it is recommended to use the for loop.  In java there are 3 types of for loops, they are as follows: 1. Simple for loop 2. For-each loop 3. labelled for loop for (initialization; condition; increment/decrement) { statement; }
  • 20.
    // Java programto illustrate for loop class forLoopDemo { public static void main(String args[]) { // Writing a for loop // to print Hello World 5 times for (int i = 1; i <= 5; i++) System.out.println("Hello World"); } } // Program to display multiplication table class ForDemo1 { public static void main(String[] args) { int n, i; n=2; for(i=1;i<=10;i++) { System.out.println(n+"*"+i+"="+n*i); } } } Output: 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20 Output: Hello World Hello World Hello World Hello World Hello World
  • 21.
    For-Each loop:  Thetraversal of elements in an array can be done by the for- each loop.  The elements present in the array are returned one by one.  It must be noted that the user does not have to increment the value in the for-each loop. for(Type var:array) { //code for execution } public class ForEachDemo1 { public static void main(String[] args) { inta[]={100,101,102,103,104}; for(int i:a) { System.out.println(i); } } } Output: 100 101 102 103 104
  • 22.
    Labelled For Loop In Java, Labelled For Loop is used to give label before any for loop. It is very useful for nesting for loop. labelname: for(initialization;condition;incr/decr) { //code for execution } public class LabeledForDemo { public static void main(String[] args) { num: for(inti=1;i<=5;i++) { num1: for(int j=1;j<=5;j++) { if(i==2&&j==2) { break num; } System.out.println(i+" "+j); } } } } Output: 1 1 1 2 1 3 1 4 1 5 2 1
  • 23.
     Branching statementsare used to jump from the current executing loop.  Branching statements in java are used to jump from a statement to another statement, thereby the transferring the flow of execution break Statement  break is a keyword.  It is used within any control statements.  break – Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.  The break statement in java is used to terminate a loop and break the current flow of the program.  Syntax: break; or break <label>; Unconditional Control Statements / Jump Statements / Branching statements
  • 24.
    // Using breakstatement in for loop Output: 0 1 2 3 4 5 After loop
  • 25.
    continue Statement  continueis a keyword.  In Java, the Continue statement is used in loops.  Continue statement is used to jump to the next iteration of the loop immediately.  It is used with for loop, while loop and do-while loop. Syntax: continue; or continue<label>; class continueTest { public static void main(String args[]) { for (int j = 0; j &lt; 10; j++) { // If the number is odd then bypass and continue with next value if (j%2 != 0) continue; // only even numbers will be printed System.out.print(j + " "); } } } Output: 0 2 4 6 8
  • 26.
    //continue with labelis used to continue the number of loops below the label class contlabelDemo{ public static void main(String args[]){ Termi: for(int i=1;i<=4;i++) { for(int k=1;i<=4;k++) { System.out.print(“ ”+i+” ”); if(i==3) continue Termi; System.out.println(“Hello”); } } } }
  • 27.
    Summary  In thislesson you learnt about  Control Statements  Decision making statements  Iterative statements  Branching statements