OOP Chapter 2
OOP Chapter 2
Programming Language
Basics in Java
Programming
// Read a string
System.out.print("Enter your name: ");
String name = scanner.nextLine();
// Read an integer
System.out.print("Enter your age: ");
int age = scanner.nextInt();
do {
statement
} while(boolean);
IS_Dept@AU_Waliso Object Oriented Programming – INSY2042 38
while...
• while and do..while loops are used if you want to
repeat a certain statement or block of statements until
a certain expression becomes false.
• Here is an example for both of the loop statements.
int n = 0;
while(n <= 5){
System.out.println(n);
n++;
}
int n = 0;
do{
System.out.println(n);
n++;
}while(n <=5);
IS_Dept@AU_Waliso Object Oriented Programming – INSY2042 39
switch statement
• The switch control flow structure is useful
when you have a single expression with
many possible options.
• The same thing can be done using recursed
if..else statements.
• The swtich statement is executed by
comparing the value of an initial expression
or variable with other variables or
expressions.