MOD1 PPT - Chap 2
MOD1 PPT - Chap 2
Control Statements
Java’s program control statements can be put into the
following categories:
selection, iteration, and jump.
• Selection statements allow your program to choose
different paths of execution based upon the outcome
of an expression or the state of a variable(if,if-else,if,-
else-if ladder.
• Iteration statements enable program execution to
repeat one or more statements (that is, iteration
statements form loops)while,do-while,for,nested loop.
• Jump statements allow your program to execute in a
non-linear fashion(break,continue,return).
Java’s Selection Statements
• Java supports two selection statements: if and
switch.
If: Switch
• Simple if Nested switch
• If else
• Nested if
• If else if laddder
The if statement
• The if statement executes a block of code only
if the specified expression is true.
• If the value is false, then the if block is skipped
and execution continues with the rest of the
program.
• You can either have a single statement or a
block of code within an if statement.
Note that the conditional expression must be a
Boolean expression.
Example: public class Example
{
public static void main(String[] args)
{
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)
System.out.println("b > a");
}
}
The if else statement
• The if statement is Java’s conditional branch statement.
• It can be used to route program execution through two
different paths.
• Here is the general form of the if statement
• 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 if works like this: If the condition is true,
then statement1 is executed. Otherwise,
statement2 (if it exists) is executed.
Example: public class Example
{
public static void main(String[] args)
{
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
Else
System.out.println("b > a");
}
}
Nested if
• A nested if is an if statement that is the target
of another if or else.
• When you nest ifs, the main thing to
remember is that an else statement always
refers to the nearest if statement that is
within the same block as the else and that is
not already associated with an else.
• One if statement worked within another if
statement
Syntax:
Here is an example:
class example
{
public static void main(String args[])
{
int a=10;
int b=20;
if(a==10){
if(b==20){
System.out.println(“nested if statement");
}
}
}
}
The if-else-if Ladder
• A common programming construct that is based upon a
sequence of nested if’s is the if-else-if ladder.
Syntax:
• 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.
// Java program to illustrate if-else-if
ladder
// condition 3
import java.io.*;
else if (i == 20)
class sample
{
System.out.println("i is 20\n");
public static void main(String[] args) else
{ System.out.println("i is not
// initializing expression present\n");
int i = 20; System.out.println("Outside if-
else-if");
// condition 1 }
if (i == 10) }
System.out.println("i is 10\n");
// condition 2
else if (i == 15)
System.out.println("i is 15\n");
The switch statement
• The switch case statement is a multi-way branch with several
choices. A switch is easier to implement than a series of if/else
statements.
• Switch statement executes one statement from multiple
conditions
Structure of Switch:
• The switch statement begins with a keyword, followed by an
expression.
• Following the controlling expression is a code block that
contains zero or more labeled cases.
• Each label must equate to an integer constant and each must
be unique.
syntax
ex
• Truth table:
Conditional OR
• The operator is applied between two Boolean
expressions. It is denoted by the two OR
operator (||).
• It returns true if any of the expression is true,
else returns false.
• Truth table:
example
public class ConditionalOperatorExample
{
public static void main(String args[])
{
int x=5, y=4, z=7;
System.out.println(x>y && x>z || y<z);
System.out.println((x<z || y>z) && x<y);
}
}
output
• true
• false
Ternary Operator
• The meaning of ternary is composed of three
parts.
• The ternary operator (? :) consists of three
operands. It is used to evaluate Boolean
expressions.
• Java includes a special ternary (three way)
operator that can replace certain types of if-
then-else statements.
• The ? has this general form:
Output:?