Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops CGS3416 Spring 2019 Lecture 7.

Similar presentations


Presentation on theme: "Loops CGS3416 Spring 2019 Lecture 7."— Presentation transcript:

1 Loops CGS3416 Spring 2019 Lecture 7

2 The if-else statement public class UnderstandingIfElseStatements {
public static void main(String[] args) { // TODO Auto-generated method stub boolean isLightGreen = false; if(isLightGreen) { //traffic light is green System.out.println("Drive!");} else{ System.out.println("stop!");} } if ( Boolean expression) { // codes Statements; When; True; } else { // some other code False; Boolean expression is tested. If this expression is true. A series of codes is executed. These codes underneath the if statements. If the Boolean expression is false. The statements under if are skipped. And the statements in the else block are executed. In this particular statements, we will do one of two things based on the Boolean expression is true or false. And never both.

3 Types of Control Flow Sequential: Default mode. Statements are executed line by line. Selection: Used for decisions, branching – choosing between 2 or more alternative paths. if if - else switch Repetition: Used for looping – repeating a piece of code multiple times in a row. while do - while for There are 3 types of loop in java, the first type of loop is while, the second type is do while

4 Control Flow

5 Incrementing and Decrementing
Some short-cut assignment operators (numbers) v += e; means v = v + e; // v -= e; means v = v - e; v *= e; means v = v * e; v /= e; means v = v / e; v %= e; means v = v % e; Increment and Decrement Operators ++x; // pre-increment (returns reference to new x) x++; // post-increment (returns value of old x) // shortcuts for x = x + 1 --x; // pre-decrement x--; // post-decrement // shortcuts for x = x - 1 This example is simple but it gives you a very important concept what we will use in our loop structure. Increment by 1 let’s trace through the program and see what is going on.

6 While Loop Declare a loop control variable
public class DifferenceBetweenSelectionandLoop { public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; while (i < 10) { System.out.println("While Loop, iteration " + i); i++; } System.out.println("The While Loop Ended"); do while vs while loop The only time you should use do while loop is when you want to execute the statements inside loop at least once, even though condition expression returns false. Otherwise it’s always better to use while loop. Java while loop is used to execute a block of statements for a specific number of times while the if statement is a conditional statement which only execute once. Increment or decrement

7 Do while Loop public class UnderstandingDoWhileLoop {
public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; do { System.out.println("Do-While Loop, iteration " + i); i++; }while(i<10); System.out.println("The Do-While Loop Ended"); } Increment or decrement

8 for Loop public class UnderstandingForLoop {
public static void main(String[] args) { // TODO Auto-generated method stub /* for(loop control variable initialization; termination/exit condition; increment or decrement statement) */ for(int i = 0; i <10; i++) { System.out.println("For Loop, iteration " + i); } System.out.println("The For Loop Ended"); Let’s me create a simple example to show the working of for loop. Semi colons

9 Nested Loops Demo

10 Break and continue These statements can be used to alter the flow of control in loops, although they are not specifically needed. (Any loop can be made to exit by writing an appropriate test expression). break: This causes immediate exit from any loop (as well as from switch blocks). continue: When used in a loop, this statement causes the current loop iteration to end, but the loop then moves on to the next step. In a while or do-while loop, the rest of the loop body is skipped, and execution moves on to the test condition. In a for loop, the rest of the loop body is skipped, and execution moves on to the iterative statement.

11 For Loops with Strings For loops run a fixed number of times strings have a fixed length. Printing each of the characters in a string using a for loop

12 The file is located at c:\myFiles\myDoc.doc
Task Use the java standard print out function to print the following sequences on screen: He said "Hello, World!“ The file is located at c:\myFiles\myDoc.doc Bachslash (\) vs forward slash:: (/) An escape sequence will always start with a backslash

13 Escape Sequences: \” \\ \n \t \r
A way to use symbols that are already being used in Java (“ and \) Stop code and perform a task (newline, tab, and carriage return) Must be contained in quotes Will always start with a backslash \ Backslash escape sequences are very important in java, they are especially important when you are trying to print out a quote or backslash. If you are not aware of them , you will get unexpected errors. And not get the result that you desire.


Download ppt "Loops CGS3416 Spring 2019 Lecture 7."

Similar presentations


Ads by Google