14
How to create a program A simple guide to help you better understand the process

Creating a program from flowchart

Embed Size (px)

Citation preview

Page 1: Creating a program from flowchart

How to create a program

A simple guide to help you better understand the process

Page 2: Creating a program from flowchart

1.Example program

Create a program that produces the following-

1.Input a number

2.Find out if the number is a multiple of 3

Page 3: Creating a program from flowchart

2.Create a flowchart

So if the output has more than one answer, it would be conditional instructions so we must include a decision box within the flowchart.

Page 4: Creating a program from flowchart

3.Creating the flowchart

To help us with coding, we should create variable names for the data we will input.

So if the instruction is to input a number then we will call it var numb.

*variables are case-sensitive and cannot include spaces or punctuation marks.

Page 5: Creating a program from flowchart

4.The flowchart

Page 6: Creating a program from flowchart

The code<html>

<body>

<script type="text/javascript">

var numb = prompt("enter number to check if it is a multiple of 3","");

numb= parseInt(numb);

var remainder=numb%3;

if(remainder%3!=0)

{

document.write("no");

}

else

{

document.write("yes");

}

</script>

</body>

</html>

Page 7: Creating a program from flowchart

5.Using the flowchart to help create code in javascript

<html>

<body>

<script type="text/javascript">

Page 8: Creating a program from flowchart

5.Using the flowchart to help use code in javascript

Here we create the code so the program will ask the user to enter a number and we assign the variable to a function.

var numb = prompt("enter number to check if it is a multiple of 3","");

numb= parseInt(numb);

Page 9: Creating a program from flowchart

5.Using the flowchart to help use code in javascript

This % is a mathematical operator which will present a remainder if a number is divided. The if statement checks if a code divides by 3 with a remainder.

var remainder=numb%3;

if(remainder%3!=0)

Page 10: Creating a program from flowchart

5.Using the flowchart to help use code in javascript

The “!=0” states to check if the remainder is not equal to 0 since a multiple would divide and have no remainder.

var remainder=numb%3;

if(remainder%3!=0)

Page 11: Creating a program from flowchart

5.Using the flowchart to help use code in javascript

So if code divides by 3 with a remainder, the if statement will produce output that reads “no” meaning the number entered in the program is not a multiple of 3. If there is any other number that does divide by 3 with no remainder it will display yes.

{

document.write("no");

}

else

{

document.write("yes");

}

Page 12: Creating a program from flowchart

5.Using the flowchart to help use code in javascript

We then end the program with this-

</script>

</body>

</html>

Page 13: Creating a program from flowchart

5.Using the flowchart to help use code in javascript

We then end the program with this-

</script>

</body>

</html>

Page 14: Creating a program from flowchart

Summary

Create a flowchart to help you when coding a program.

Remember that variables usually begin with “var” and are case-sensitive.

Document.write is used to produce an output. Assign a variable using parseInt I.e var age =

parseInt (age.)

*If a code doesn't work, you can check online to see what could be wrong with the code.