0% found this document useful (0 votes)
11 views19 pages

Lecture-5 Control Statements

The document discusses various control statements in Java including if/else statements, switch statements, loops like while, for and do-while loops. It provides examples of using break and continue in loops. It also gives an example of averaging a list of numbers entered by the user until a negative number is entered.

Uploaded by

Istiak Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Lecture-5 Control Statements

The document discusses various control statements in Java including if/else statements, switch statements, loops like while, for and do-while loops. It provides examples of using break and continue in loops. It also gives an example of averaging a list of numbers entered by the user until a negative number is entered.

Uploaded by

Istiak Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture 5: Control

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 is to hold the minimum of x and y


if ( x < y )
z = x;
Notice no braces!
else
z = y;

• Another way to do this:

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!

• Can be difficult to read

System.out.println ((number != 0) ? ((number > 0) ? "positive“ : "negative") : "zero“);

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

Handles all of the other cases 9


Java looping
• Options
• while
• do-while
• for

• Allow programs to control how many times a statement list is


executed

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

for ( int count = 0; count < 10; count++ ) {


System.out.println (count);
}
count is NOT defined here
• Would (mostly) translate as:

int count = 0; count IS defined here


while (count < 10) {
System.out.println (count);
count++;
} 13
Nested loops
int m = 2;
int n = 3;
for (int i = 0; i < n; ++i) {
System.out.println("i is " + i);
for (int j = 0; j < m; ++j) {
System.out.println(" j is " + j);
}
}

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

for ( int a = 0; a <= 10; a++ ) {


if ( a % 2 == 0 ) {
continue;
}
System.out.println (a + " is odd");
}

• 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

for ( int a = 0; a <= 10; a++ ) {


if ( a == 5 ) {
break;
}
System.out.println (a + " is less than five");
}

• Output: 0 is less than five


1 is less than five
2 is less than five
3 is less than five
4 is less than five
17
Averaging
•Problem
• Extract a list of positive numbers from standard input and produce their average
• Numbers are one per line
• A negative number acts as a sentinel to indicate that there are no more numbers to
process

•Solve the Problem


Enter positive numbers one per line.
Indicate end of list with a negative number.
4.5
0.5
1.3
-1
Average 2.1

18
public class NumberAverage {
// main(): application entry point
public static void main(String[] args) {
// initialize variables

// set up the input


// prompt user for values

// get first value


// process values one-by-one
while (value >= 0) {
// add value to running total
// processed another value
// prepare next iteration - get next value
}

// 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;

System.out.println("Average: " + average);

System.out.println("No list to average");


20

You might also like