SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY
Kuniamuthur, Coimbatore, Tamilnadu, India
An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)
COURSE MATERIAL
Course : 22AD201 – Java Programming
Module : 1
Topic : Decision Statements
www.skcet.ac.in
www.skcet.ac.in
Module 1:
1.2. Decision Statements - if Statements, if-else Branching, switch Statements,
Conditional Operator
22AD201 Java Programming – Module 1
Text Book:
Herbert Schildt, “Java: The Complete Reference”, 9th edition, Tata McGraw Hill, 2014.
Web Resources:
https://www.ktbyte.com/java-tutorial/book/if-statements
Session Outcome
At the end of this session, you will be able to:
❖ Understand the various decision making statements in
java.
❖ Use the decision making statements in java to build logic
in programs.
❖ Choose appropriate decision statements as required by
particular problems.
❖ if statements (one-way)
❖ if-else statements (two-way)
❖ nested if statements
❖ if-else statements (multi-way)
❖ switch statements
❖ conditional expressions
Types of Selection / Decision Statements
if statement executes an action if and only if the condition is true.
Syntax:
Example:
if statement in Java
if(boolean - expression)
{
statements(s);
}
Control Flow:
if statement in Java
if-else statement decides/chooses one of the two execution paths
based on whether the condition is true or false.
Syntax:
if-else statement in Java
if(boolean - expression)
{
statements(s) for the true-case;
}
else
{
statements(s) for the false-case;
}
Example:
Knowledge check:
What will be the output of the following code if number = 34?
if-else statement in Java
Control Flow:
if-else statement in Java
if statement may be included inside another if statement to create a
nested if.
Example:
❖ There is no limit to the depth of nesting of if statements.
Nested if statement in Java
Knowledge check:
With which if statement (first or second), is the else statement
associated?
if-else statement in Java
if (i > k)
{
if (j > k)
System.out.println("i and j are
greater than k");
else
System.out.println("i is less than or
equal to k");
}
Syntax:
Multi way if-else statements in Java
if(boolean - expression 1)
{
statements(s) to be executed if expression 1 is true;
}
else if (boolean - expression 2)
{
statements(s) to be executed if expression 1 is false & expression
2 is true ;
}
..
.
.
else
{
statements(s) to be executed if all above expressions are false;
}
Example:
Multi way if-else statements in Java
Control Flow:
Multi way if-else statements in Java
switch statement executes statements based on the value of a
variable or expression.
→ switch expression is evaluated
→ output of the switch expression is compared with all the case values.
→ Statements belonging to the case, for which, the switch expression is
equal to the case value, is executed.
→ If the switch expression does not match with any of the case values,
the statements under default are executed.
switch statements in Java
Syntax:
switch statements in Java
switch(expression)
{
case value1:
Statement(s) to be executed if (expression=value1);
break;
case value2:
Statement(s) to be executed if (expression=value2);
break;
case value3:
Statement(s) to be executed if (expression=value3);
break;
……..
default:
Statement(s) to be executed if expression did not match any of the
case values;
}
Example:
switch statements in Java
int day;
……
switch(day)
{
case 0:
System.out.println(“Sunday”);
break;
case 1:
System.out.println(“Monday”);
break;
……
default:
System.out.println(“Entered value is greater than 6”);
}
Knowledge check:
What will be the output of the following code?
i) if day=5 ii) if day=2 iii)if
day=0
if-else statement in Java
Control Flow:
switch statements in Java
❖ The switch-expression must yield a value of char, byte, short, int,
or String type and must always be enclosed in parentheses.
❖ The case values, value1, . . ., upto value N must have the same data
type as the value of the switch- expression.
❖ value1, . . ., and value N are constant expressions, meaning that they
cannot contain variables, such as 1 + x.
❖ When the value in a case statement matches the value of the switch-
expression, the statements starting from this case are executed until
either a break statement or the end of the switch statement is
reached.
❖ The default case, which is optional, can be used to perform actions
when none of the specified cases matches the switch-expression.
❖ The keyword break is optional. The break statement immediately
ends the switch statement.
switch statements in Java - Rules
Also called as ternary operator. It is similar to the working of if-else.
Syntax:
Example:
Conditional expressions in Java
boolean - expression ? expression 1 : expression 2;

22AD201 – Java Programming -Decision Statements.pptx

  • 1.
    SRI KRISHNA COLLEGEOF ENGINEERING AND TECHNOLOGY Kuniamuthur, Coimbatore, Tamilnadu, India An Autonomous Institution, Affiliated to Anna University, Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT) COURSE MATERIAL Course : 22AD201 – Java Programming Module : 1 Topic : Decision Statements www.skcet.ac.in
  • 2.
    www.skcet.ac.in Module 1: 1.2. DecisionStatements - if Statements, if-else Branching, switch Statements, Conditional Operator 22AD201 Java Programming – Module 1 Text Book: Herbert Schildt, “Java: The Complete Reference”, 9th edition, Tata McGraw Hill, 2014. Web Resources: https://www.ktbyte.com/java-tutorial/book/if-statements
  • 3.
    Session Outcome At theend of this session, you will be able to: ❖ Understand the various decision making statements in java. ❖ Use the decision making statements in java to build logic in programs. ❖ Choose appropriate decision statements as required by particular problems.
  • 4.
    ❖ if statements(one-way) ❖ if-else statements (two-way) ❖ nested if statements ❖ if-else statements (multi-way) ❖ switch statements ❖ conditional expressions Types of Selection / Decision Statements
  • 5.
    if statement executesan action if and only if the condition is true. Syntax: Example: if statement in Java if(boolean - expression) { statements(s); }
  • 6.
  • 7.
    if-else statement decides/choosesone of the two execution paths based on whether the condition is true or false. Syntax: if-else statement in Java if(boolean - expression) { statements(s) for the true-case; } else { statements(s) for the false-case; }
  • 8.
    Example: Knowledge check: What willbe the output of the following code if number = 34? if-else statement in Java
  • 9.
  • 10.
    if statement maybe included inside another if statement to create a nested if. Example: ❖ There is no limit to the depth of nesting of if statements. Nested if statement in Java
  • 11.
    Knowledge check: With whichif statement (first or second), is the else statement associated? if-else statement in Java if (i > k) { if (j > k) System.out.println("i and j are greater than k"); else System.out.println("i is less than or equal to k"); }
  • 12.
    Syntax: Multi way if-elsestatements in Java if(boolean - expression 1) { statements(s) to be executed if expression 1 is true; } else if (boolean - expression 2) { statements(s) to be executed if expression 1 is false & expression 2 is true ; } .. . . else { statements(s) to be executed if all above expressions are false; }
  • 13.
    Example: Multi way if-elsestatements in Java
  • 14.
    Control Flow: Multi wayif-else statements in Java
  • 15.
    switch statement executesstatements based on the value of a variable or expression. → switch expression is evaluated → output of the switch expression is compared with all the case values. → Statements belonging to the case, for which, the switch expression is equal to the case value, is executed. → If the switch expression does not match with any of the case values, the statements under default are executed. switch statements in Java
  • 16.
    Syntax: switch statements inJava switch(expression) { case value1: Statement(s) to be executed if (expression=value1); break; case value2: Statement(s) to be executed if (expression=value2); break; case value3: Statement(s) to be executed if (expression=value3); break; …….. default: Statement(s) to be executed if expression did not match any of the case values; }
  • 17.
    Example: switch statements inJava int day; …… switch(day) { case 0: System.out.println(“Sunday”); break; case 1: System.out.println(“Monday”); break; …… default: System.out.println(“Entered value is greater than 6”); }
  • 18.
    Knowledge check: What willbe the output of the following code? i) if day=5 ii) if day=2 iii)if day=0 if-else statement in Java
  • 19.
  • 20.
    ❖ The switch-expressionmust yield a value of char, byte, short, int, or String type and must always be enclosed in parentheses. ❖ The case values, value1, . . ., upto value N must have the same data type as the value of the switch- expression. ❖ value1, . . ., and value N are constant expressions, meaning that they cannot contain variables, such as 1 + x. ❖ When the value in a case statement matches the value of the switch- expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. ❖ The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. ❖ The keyword break is optional. The break statement immediately ends the switch statement. switch statements in Java - Rules
  • 21.
    Also called asternary operator. It is similar to the working of if-else. Syntax: Example: Conditional expressions in Java boolean - expression ? expression 1 : expression 2;