Académique Documents
Professionnel Documents
Culture Documents
Relational Operator
The Relational Operators determine the relationship that one operand has to the other. e.g. Equality and Ordering Operator Result
== != > < >= <= Equal to Not equal to Greater than Less than Greater than or equal to Less than or equal to
Flow of Control
Sequence Selection Iteration
Sequence
Key points: order of operations may or may not be important order of evaluation within a single statement can be critical example: a = b++; a = b = read(input);
Order of precedence
parentheses before operators then multiplication and division then addition and subtraction left to right for equal precedence ops RHS of an assignment before LHS if in doubt use brackets use brackets anyway (to clarify to other readers!)
Selection
if (condition) { statement1; } else { statement2; }
Selection
if (condition) { statement1; }
else { condition must be of type boolean
statement2;
}
Selection
if (condition) { statement1; } else { statement2; } else branch is optional
Selection
if (condition) { statement1; } else { statement2; }
Selection
if (condition)
statement;
Brackets around the Condition are essential
Selection
if (condition1) { if (condition2) { statement1; } else { statement2; } } else { statement3; }
if statements can be nested indentation ignored by the compiler but helps (or can mislead!) human readers
Example conditions if ((i < j) && (eof != true)) . if ((i < j) && !eof) . if ((i < j) && !eof()) . if ((n = input.read())== 0) Dont use = when you mean ==
Example Program
class Larger { public static void main(String[] args) { int x=20; int y=20; if(x >= y){ if (x==y){ System.out.println("x="+x+" and y="+y+ " are Equal"); } else { System.out.println("Both are unequal"); } System.out.println(x+" is the larger."); } else { System.out.println(y+" is the larger."); } } }
Example 2
class Largest { public static void main(String[] args){ // Read three integers and print which is the larger. SimpleInput keyboard = new SimpleInput(); System.out.println("Please type three integers."); int x = keyboard.nextInt(), y = keyboard.nextInt(), // A variable to hold the answer. int largest; if(x >= y){ // It must be either x or z. if(x >= z){ largest = x; } else{ largest = z; } } z = keyboard.nextInt();
else{
if(y >= z){
// It must be either y or z.
largest = y; } else{ largest = z; } } System.out.println("The largest is: "+largest); } // Program Code is courtesy of David J. Barnes.
Practice Exercise
int month=10; String season; If (month==12 || month ==1 || month ==2) season =Winter; else if(month==3 || month ==4 || month ==5) season=Spring; else if(month==6 || month ==7 || month ==8) season=Summer; else if(month==9 || month ==10 || month ==11) season=Autumn; else season= Bogus Month; System.out.println(October is in the +season +.);
Selection-switch
switch (Condition) { Case 1: Statement1;
Case 2: Statement 2; Break; .... .... Default: Statement n; Break;
Condition should be int, short, byte or a char type
Switch Selection is replacement of If-else for multiple selections and for multiple conditions.
Selection-switch
switch (Condition) { Case 1: Statement1;
Case 2: Statement 2; break; .... .... default: Statement n; Break;
This Should be an Integer or Constant value Remember this is COLON sign not SemiColon or Delimeter Break Used to break the condition If none of the Case Statement satisfies Default condition will work
Example-switch Statement
class switchT{ public static void main(String[] Args) { int x=1; switch(x){
case 1:
System.out.println("Out put is 1: "+x); case 2: System.out.println("Out put is 2"); break; case 3:
Iteration
while for simplest most common
do .. While uncommon
While Statement
while ( condition) { body statements; }
while the condition evaluates to true, repeatedly loop executing the body statements condition is tested before each pass through the body body statements should have some effect on the condition or you could loop indefinitely
While Statement
int a=10; while (a > 0) { System.out.println (a); a = a-1; } System.out.println (You Lost The Game");
do...while statement
int a=10; do{ System.out.println (a); a = a-1; } while (a > 0); System.out.println (You Lost The Game");
for statement
for Keyword Variable Initialisation Loop Condition
body statements;
}
Loop termination Condition
for statement
most convenient method for counter based iterations sequence of execution: initialisation is while condition true? if so, execute body statements execute end of loop operation is while condition true etc
for statement
Difference and common features of while and for loop
for (INITIALIZER; CONDITION; INCREMENTOR) { BODY }
for statement
for (int i=0; i<10; i++) { System.out.print(i + ); } output: 0123456789
for statement
break statement has importance in loop statements After reading break statement our compiler came out of loop and read the next statement after loop