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"); } } }