Presentation is loading. Please wait.

Presentation is loading. Please wait.

UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping.

Similar presentations


Presentation on theme: "UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping."— Presentation transcript:

1 UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping

2 UFCFY5-30-1Multimedia Studio Agenda Additional control flow structures and loops The ‘switch’ branching structure The while do loop The for loop Boolean data type Formatting decimal output

3 UFCFY5-30-1Multimedia Studio The switch statement int month = 3; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); }

4 UFCFY5-30-1Multimedia Studio Looping: while // loop until some condition is met while(aCondition) { // do something here // update loop condition }

5 UFCFY5-30-1Multimedia Studio int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++;// shorthand for count = count + 1 } Looping: while d

6 UFCFY5-30-1Multimedia Studio ‘while’ verses ‘do while’ int count = 11; while (count < 11) { // loop will not be processed System.out.println("Count is: " + count); count++;// shorthand for count = count + 1 } program continues here ignoring the loop

7 UFCFY5-30-1Multimedia Studio ‘while do’ verses ‘do while’ int count = 11; do { // loop will be processed once System.out.println("Count is: " + count); count++;// shorthand for count = count + 1 } while (count < 11) program continues here after going through the loop

8 UFCFY5-30-1Multimedia Studio The while do loop is a pre-condition loop structure, if the initial condition is not met the loop will not be processed The do while loop is a post-condition loop structure, the loop will always be processed at least once ‘while do’ verses ‘do while’

9 UFCFY5-30-1Multimedia Studio The for loop for(start value; end value ; increment){ // do something here }

10 UFCFY5-30-1Multimedia Studio The for loop for(int aNumber =1; aNumber < 6; aNumber ++){ System.out.println("Count is: " + aNumber); }

11 UFCFY5-30-1Multimedia Studio The for loop The output of the program would be: Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5

12 UFCFY5-30-1Multimedia Studio The for loop // change the end value for the loop for(int aNumber =1; aNumber <= 6; aNumber ++){ System.out.println("Count is: " + aNumber); }

13 UFCFY5-30-1Multimedia Studio The for loop The output of the program now would be: Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6

14 UFCFY5-30-1Multimedia Studio The for loop The for loop is processed for a pre-determined number of times Typically used where a collection of data is being counted or totalled, or searching a collection of data for a given item or value

15 UFCFY5-30-1Multimedia Studio New Data Type: Boolean The boolean data type has only two possible values: true and false Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that is precisely defined.

16 UFCFY5-30-1Multimedia Studio Additional Boolean Condition boolean guessed = false; // declare Boolean variable – set to false if (userGuess == numberToGuess){ System.out.println("Well Done!" + userName + " You guesed the number in " + tries + " tries."); guessed = true;// set Boolean to true } while(tries < 10 && guessed == false);// && reads AND // or while(tries < 10 && !guessed);// reads AND NOT guessed, (! means NOT)

17 UFCFY5-30-1Multimedia Studio Formatting Decimal Output Numeric values output from variables declared from decimal types e.g. float types will usually require some formatting to make output more readable and user-friendly Java has several methods to format decimal output e.g. printf System.out.printf("%.2f", val)// prints to 2 decimal places // PI to nine decimal places System.out.printf("%1.2f", 3.141592654); //outputs 3.14 There are other methods available to format output of decimal values e.g. rounding a decimal value

18 UFCFY5-30-1Multimedia Studio Summary The flow of a program can be modified by using loops and branching statements The switch statement may branch to many outputs depending on the case of each condition The while do loop is a pre-condition loop will not be processed if the initial loop condition is not met The do while loop is a post-condition loop that will always be processed at least once The for loop is processed for a pre-determined number of times Its typically used for adding or counting collections of data and searching collections of data for values or named items Boolean data type boolean has values of true or false Use printf to format decimal output e.g. System.out.printf("%.2f", val)// prints to 2 decimal places

19 UFCFY5-30-1Multimedia Studio Code Design Activity Design a program to emulate the National Lottery Draw. The user will be prompted to enter the required number of values via the keyboard. The system will then choose a random selection of its own values. The system will compare the system values with the user values A score will be calculated based on the following matches together with a user message: 3 matches (You have won £20) 4 matches (You have won £100) 5 matches (You have won the Jackpot!)


Download ppt "UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping."

Similar presentations


Ads by Google