Вы находитесь на странице: 1из 13

BITS Pilani

BITS Pilani
Hyderabad Campus

Dr.Aruna Malapati Asst Professor Department of CSIS

BITS Pilani
Hyderabad Campus

OBJECT ORIENTED PROGRAMMING Loop Control

Todays Agenda
Control flow statements
While For If Switch

Decision making

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

while
while (expression) { statement }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

The do...while Loop:


do { //Statements }while(Boolean_expression);

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

The for Loop:


for(initialization; Boolean_expression; update) { //Statements }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Enhanced for loop in Java:


As of java 5 the enhanced for loop was introduced. This is mainly used for Arrays. for(declaration : expression) { //Statements }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

The break Keyword:


The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement. The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

The continue Keyword:


The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

The if statement
if(boolean condition){ //the boolean was true so do this } else { //do something else }

One idiosyncrasy of the Java if statement is that it must take a boolean expression not a numeric value. You cannot use the C/C++ convention of any non zero value to represent true and 0 for false.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

The if statement
In C/C++ you can test with syntax such as int i=1; if(i) { //do something } In Java an if clause must be a boolean test. Thus you can restate that example as if(i==1) Also note that the test is performed with the == operator and not the assignment operator { =. //do something } CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Switch statements
The argument to a switch statement must be a byte, char, short or int. The default clause does not need to come at the end of a case statement

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Summary
The Basic program constructs like loops and decision statements work same as in C/C++ with a few exceptions in if, Switch and for loops. In Java an if clause must be a boolean test only. The argument to a switch statement must be a byte, char, short or int. A variant of for loop exist for dealing with arrays.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Вам также может понравиться