200 likes | 336 Views
More Java Syntax. if , then, else for loop Strings more if, else…. If, then, else. Syntax: if ( condition ) {… } else {… } In Java, we do not use the word then , it is implied in the statement The condition must be true or false
E N D
More Java Syntax if , then, else for loop Strings more if, else…
If, then, else • Syntax: if (condition) {… } else {… } • In Java, we do not use the word then, it is implied in the statement • The condition must be true or false • Use the comparators ==, !=, <, <=, >, >= or a Boolean variable
Example, if - then public class IfThenElse { public static void main(String[] args) { int i, j, k; i = 0; j = 20; k = 3; while (i <= j) { if ((i % k) == 0) { System.out.println(i + " is divisible by " + k); } else { System.out.println("skipping " + i); } i = i + 1; } } }
For loop • Syntax: for (initialization; condition; increment) { … } • You need to declare a counter before, ex: int i; • Increment: i = i +1; can be written as i++; • The loop stops when the condition is false
For loop, example public class ForLoop { public static void main(String[] args) { int i; i = 0; for (i = 1; i <= 10; i++) { System.out.println("the counter is at " + i); } } }
Another initialization here, which makes the previous one useless Initialization is done here, very important! Now because is starts at 0, the loop will get done 11 times… Increment happens here, no risk of infinite loop Increment happens here, do not forget or else all hell breaks loose!!! Differences between the while loop and the for loop public class WhileLoop { public static void main(String[] args) { int i; i = 0; while ( i <= 10) { … i++; } } } public class ForLoop { public static void main(String[] args) { int i; i = 0; for (i = 1; i <= 10; i++) { … } } }
Why use a while loop? • Sometimes our condition is Boolean • Sometimes our condition involves a String or a char or a double, etc. The for loop cannot handle that! • While loops are dangerous only if they go into an infinite loop…
Strings • Strings are not primitives, they are objects • To create a full string, we need to do this: • Declare it String name; • Create it name = new String(); This gets an address in the memory for us… more when we talk about objects and classes later… • Initialize it name = ""; or name = "Garfield"; • We can do all this in one line! • String name = new String("Garfield");
Example public class SomeString { public static void main(String[] args) { String catName; catName = new String(); catName = "Garfield"; System.out.println("the name of the cat is " + catName); String dogName; dogName = new String("Odie"); System.out.println("the name of the dog is " + dogName); String manName = new String("Jon"); System.out.println("the name of the man is " + manName); } }
Other example public class AnotherString { public static void main(String[] args) { String message = new String("Hello"); String name1 = new String("Crystal"); String name2 = new String("Danny"); String name3 = new String("Wetzey"); String name4 = new String("Reyes"); message = message + " " + name1 + ", " + name2 + ", " + name3 + " and " + name4; System.out.println(message); } }
Nested if • Let us suppose we need to make a decision but there are many possible outcomes… Ex: if an effort rating is ‘A’, then put the student on the honour roll, if the effort is ‘B’, then put the student on the effort list, if the effort is ‘C’, then nothing happens but if the effort is a ‘D’, then the student is given a Saturday night detention…
effort yes result = “effort list” result = “honours” result = “nothing” result = “detention” no yes effort = ‘A’? no yes effort = ‘B’? no effort = ‘C’? result Flow chart
Method 1 public class AnotherIf { public static void main(String[] args) { char effort; String result; effort = 'A'; if (effort == 'A') { result = "honours"; } else { if (effort == 'B') { result = "effort list"; } else { if (effort == 'C') { result = "nothing"; } else { result = "detention"; } } } System.out.println("Your effort is " + effort + " so you deserve " + result); } } Not so good, very confusing !!!
Method 2 public class OneMoreIf { public static void main(String[] args) { char effort; String result; effort = 'D'; if (effort == 'A') { result = "honours"; } else if (effort == 'B') { result = "effort list"; } else if (effort == 'C') { result = "nothing"; } else { result = "detention"; } System.out.println("Your effort is " + effort + " so you deserve " + result); } } Now we are talking !!!
Assignment 2 Write a java program that will loop from 1 to 7 and output “Sunday” if the loop counter is 1, “Monday” if the loop counter is 2 and so on…