Академический Документы
Профессиональный Документы
Культура Документы
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Control flow statements
While For If Switch
Decision making
while
while (expression) { statement }
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
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.