22AD201 – Java Programming -Decision Statements.pptx

comicpov 1 views 21 slides Jul 12, 2024
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

: 22AD201 – Java Programming
Decision Statements


Slide Content

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 : 22AD 201 – 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;
Tags