8
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, 2006. All rights reserved

Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

Embed Size (px)

Citation preview

Page 1: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

Java Programming Constructs 3

MIS 3023

Business Programming Concepts II

The University of Tulsa

Professor: Akhilesh Bajaj

All slides in this presentation ©Akhilesh Bajaj, 2006. All rights reserved

Page 2: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

Objectives

• Continue to understand programming constructs in Java:-the while loop-the do-while loop-the for loop

Look at the break statementLook at the continue statement

Let’s get started!

Page 3: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

Nesting for Loops

To get the above output, we can use:

Page 4: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

Nesting for Loops

A program that will list all the letters contained in a string and will also count the number of different letters.

Page 5: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

The break Statement Syntax:break;

• Breaks out of a loop or switch statement• If used in an if statement, then the if statement HAS to be in a loop or a switch, and the program will break out of that container loop or switch statement.

Can we write the above with a for loop?

Page 6: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

The break Statement

We can also create a labeled break, to break out of nested loops.

label_name:loop1 loop 2

loop3break label_name;

end loop3end loop2

end loop1

Will break out of all the loops and proceed with the next statement below the 3 labeled loops.See example of ListLetters.java

Page 7: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

The continue Statement Syntax:continue;

• Continues with the next iteration of the loop.

• See program ContinueDemo.java

• The continue statement can also refer to a label, if we want to continue the next iteration of an outer loop.

Page 8: Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation

Fun In Class Example

Write a program called

MultiplicationTable.java

that prints the multiplication table for a particular integer, input by the user, times 1 till times 12 of that integer.