1 / 20

More Java Syntax

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

merle
Download Presentation

More Java Syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Java Syntax if , then, else for loop Strings more if, else…

  2. 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

  3. 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; } } }

  4. Result

  5. 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

  6. 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); } } }

  7. Result

  8. 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++) { … } } }

  9. 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…

  10. 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");

  11. 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); } }

  12. Result

  13. 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); } }

  14. Result

  15. 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…

  16. 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

  17. 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 !!!

  18. 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 !!!

  19. Results

  20. 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…

More Related