Lecture-5 Control Statements
Lecture-5 Control Statements
Statement
Sub: Object Oriented Programming (OOP)
Code: CSE-1205
1
Control Statements
• Java’s control statements are nearly identical to those in C/C++.
• There are a few differences – especially in the break and continue
statements.
• The control statements discussed here:
• if
• if - else
• if - else if
• switch
•?
• while
• for
2
if statement
• What does the following do?
________________________________________________________
Scanner stdin = new Scanner(System.in);
System.out.print("Enter an integer number: ");
int value1 = stdin.nextInt();
System.out.print("Enter another integer number: ");
int value2 = stdin.nextInt();
if (value2 < value1) {
int rememberValue1 = value1;
value1 = value2;
value2 = rememberValue1;
}
System.out.println("The numbers in sorted order are "
+ value1 + " and then " + value2);
3
If-then-else precedence
if (number != 0)
if (number > 0)
System.out.println("positive");
Which if does this
else refer to? else
System.out.println("negative");
4
If-else-if
• Consider
if (number == 0) {
System.out.println("zero");
}
else if (number > 0) {
System.out.println("positive");
}
else {
System.out.println("negative");
} 5
Finding the minimum value using “?” notation
• Consider:
z = (x<y) ? x : y;
6
The ?: notation
• Only works when both “cases” return a value!
• Meaning when both “cases” are expressions
• Example: z = (x<y) ? x : y;
• Thus, you can’t put a print statement in there!
if (number != 0)
if (number > 0)
System.out.println("positive");
else
System.out.println("negative");
else
System.out.println("zero");
7
A switch statement example
if (a == ‘0’) switch (a) {
case ‘0’:
System.out.println (“zero”); System.out.println (“zero”);
break;
else if (a == ‘1’)
case ‘1’:
System.out.println (“one”); System.out.println (“one”);
break;
else if (a == ‘2’) case ‘2’:
System.out.println (“two”); System.out.println (“two”);
break;
else if (a == ‘3’) case ‘3’:
System.out.println (“three”); System.out.println (“three”);
break;
else if (a == ‘4’) case ‘4’:
System.out.println (“four”); System.out.println (“four”);
break;
else default:
System.out.println (“five+”); System.out.println (“five+”);
break;
} 8
Testing for vowel-ness
switch (ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
System.out.println("vowel“);
break; The break causes an exiting of the switch
default:
System.out.println("not a vowel“);
}
10
for vs. while
• An example when a for loop can be directly translated into a while loop:
int count;
for ( count = 0; count < 10; count++ ) {
System.out.println (count);
}
• Translates to:
int count;
count = 0;
while (count < 10) {
System.out.println (count);
count++;
}
12
for vs. while
• An example when a for loop CANNOT be directly translated into a
while loop: only difference
14
Loop controls
15
The continue keyword
• The continue keyword will immediately start the next iteration of the loop
• The rest of the current loop is not executed
• Output: 1 is odd
3 is odd
5 is odd
7 is odd
9 is odd
16
The break keyword
• The break keyword will immediately stop the execution of the loop
• Execution resumes after the end of the loop
18
public class NumberAverage {
// main(): application entry point
public static void main(String[] args) {
// initialize variables
// display result
if (valuesProcessed > 0){
// compute and display average
}
else{
// indicate no average to display
}
}
}
19
Solve? What Loop?
int valuesProcessed = 0;
double valueSum = 0;
Scanner stdin = new Scanner (System.in);
System.out.println("Enter positive numbers 1 per line.\n"
+ "Indicate end of the list with a negative number.");
double value = stdin.nextDouble();
valueSum += value;
++valuesProcessed;
value = stdin.nextDouble();
double average = valueSum / valuesProcessed;