Conditional statements in Java 12.pptx

51 views 10 slides Apr 17, 2024
Slide 1
Slide 1 of 10
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

About This Presentation

NA


Slide Content

Condition al Statement The if Statement

The if Statement The Java i f Statement has the following syntax: If the Boolean condition is true, the statement is executed; if it is false, the statement is skipped This provides basic decision making capabilities

Attendance { public class Hello_world { public static void main(String[] args ) { boolean attendance = true: if (attendance == true) { System.out.println ("Attended class"); }

If statement flow diagram if Condition statement;

If .. Else Statement An else clause can be added to an 16 statement to make it an if-else statement. if (condition) statementl; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed

Example - Attendance public class Hello_world { public static void main(String[] args ) { boolean attendance = true: if (attendance == true) { System.out.println ("Attended class"); } else { System.out.println ("Not Attended class"); } } }

If/else flow diagram else statement2;

The while statement A while statement has the following syntax: If the condition is true, the statement is executed; then the condition is evaluated again The statement is executed over and over until the condition becomes false If the condition of a •x‹ze statement is false initially, the statement is never executed Therefore, we say that a wh1le statement executes zero or more times

While statement flow diagram while (condition)

E X - Attendance public class Hello_world { public static void main(String[] args ) { char attendance = '0’; switch (attendance) { case '0'; System.out.println ("On duty"); break; case 'P'; System.out.println ("Attended class"); break; case 'A'; System.out.println (" Not Attended class"); break; } default { System.out.println ("Invalid"); } } }
Tags