conditional statements and loops. today’s learning goals … learn about conditional statements...

62
Conditional Statements and Loops

Upload: julius-kenneth-morton

Post on 17-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Conditional Statements and Loops

Page 2: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Today’s Learning Goals …

• Learn about conditional statements and their structure

• Learn about loops and their structure

• Identify various types of conditional statements and loops

• Know how conditional statements and loops are used in scripts

Page 3: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Defining Conditional Statements

• What is a conditional statement?

• Why conditionals are useful?

Page 4: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

What is a Conditional Statement?

• A statement that is used to execute a bit of code based on a condition or to do something else when the condition is not met

• It’s a bit like cause and effect– If you study hard, you will pass the course.

Otherwise, you will fail.

Page 5: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

What is a Conditional Statement?

• Example:– If a variable named my money is greater than

1000, send an alert that says my finances are ok. Otherwise, send an alert saying I need more money!

Page 6: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Why are Conditionals Useful?

• Let’s us execute only certain parts of the script instead of executing every single line of code in the script

Page 7: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Types of Conditional Statements

• If/Else Statement Blocks

• Switch Statement Blocks

Page 8: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

If/Else Statement Blocks

• Structure

• Block Nesting

• Complex Comparisons

Page 9: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

If/Else Statement Block Structure

if (comparison here)

We want to see if a variable named ‘boats’ is equal to 3.

Page 10: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

If/Else Statement Block Structure

if (boat==3)

Page 11: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

If/Else Statement Block Structure

if (boat==3)

{

JavaScript Statements Here

}

Page 12: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

If/Else Statement Block Structure

if (boat==3){JavaScript Statements Here}else{JavaScript Statements Here}

Page 13: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Problem 1

Send an alert that says “You have the right number of boats” if the variable boats is equal to three. If it is not, we want to send an alert that says “You do not have the right number of boats” instead.

Page 14: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript">

<!--

if (boats == 3)

{

window.alert("You have the right number of boats.");

}

else

{

window.alert("You do not have the right number of boats.");

}

//-->

</script>

Page 15: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Let’s declare a variable and assign it a value …

Page 16: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript">

<!--

var boats = 3;

if (boats == 3)

{

window.alert("You have the right number of boats.");

}

else

{

window.alert("You do not have the right number of boats.");

}

//-->

</script>

Page 17: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 18: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Now change the value of the variable …

Page 19: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript">

<!--

var boats = 0;

if (boats == 3)

{

window.alert("You have the right number of boats.");

}

else

{

window.alert("You do not have the right number of boats.");

}

//-->

</script>

Page 20: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 21: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

If/Else Statement Block Nesting

• During nesting, we put one structure inside another structure of a similar nature

Page 22: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Problem 2

If variable named ‘car’ is equal to yes, and if a variable named ‘licence’ is equal to yes, send an

alert that says ‘You can drive’ to the browser.

If variable named ‘car’ is equal to yes, but if a variable named ‘licence’ is not equal to yes,

send an alert that says ‘You cannot drive’ to the browser; otherwise send an alert that says ‘You

need a car’.

Page 23: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript"><!--if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

else{window.alert("You need a car.");}//--></script>

Page 24: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Oops, I made a mistake …

Page 25: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript"><!--if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

}else{window.alert("You need a car.");}//--></script>

Page 26: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Let’s declare some variables and assign them values …

Page 27: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript"><!--var car = "yes";var licence = "yes";if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

}else{window.alert("You need a car.");}//--></script>

Page 28: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 29: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Now change the values of the variables …

Page 30: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript"><!--var car = "yes";var licence = “no";if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

}else{window.alert("You need a car.");}//--></script>

Page 31: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 32: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

and changing the values of the variables again …

Page 33: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

<script language="JavaScript"><!--var car = "no";var licence = "no";if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

}else{window.alert("You need a car.");}//--></script>

Page 34: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 35: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Another example …

Page 36: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

}else{

if (helicopter == "yes"){window.alert("Use the helicopter.");}else{window.alert("You need a car.");}

}

Page 37: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Let’s declare some variables and assign them values …

Page 38: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

var car = "no";var licence = "no";var helicopter = "yes";if (car == "yes"){

if (licence == "yes"){window.alert("You can drive.");}else{window.alert("You need a licence to drive.");}

}else{

if (helicopter == "yes"){window.alert("Use the helicopter.");}else{window.alert("You need a car.");}

}

Page 39: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 40: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Switch Statements

• Allows us to take a single variable value and execute a different line of code based on the value of the variable.

Page 41: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Examplevar thename = "Salman";switch (thename){

case “Naveed”:window.alert("Naveed is an OK name.");break;

case “Salman”:window.alert(“Salman is a great name!");window.alert("Hi Salman!");break;

default:window.alert("You have a unique name.");

}

Page 42: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

What is a Loop?

• A block of code that allows us to repeat a section of code a certain number of times, perhaps changing certain variable values each time the code is executed.

Page 43: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);document.write(“Hello. Welcome to the world.”);

Page 44: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

We can write this in a more efficient manner using loops …

Page 45: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Do this block 10 times{document.write(“Hello. Welcome to the world.”);}

Page 46: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Types of Loops

• For loops

• While loops

Page 47: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

For Loops

• Structure

• Block Nesting

Page 48: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Structure of a For Loop

for (statement)

{

JavaScript goes here

}

Page 49: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Structure of a For Loop

for (varname = 1; varname <11; varname +=1)

{

JavaScript code goes here

}

Page 50: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

for (count = 1; count < 11; count += 1){document.write(“Hello. Welcome to the world.”);}

Page 51: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Oops, I made a mistake …

Page 52: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

for (count = 1; count < 11; count += 1){document.write(“Hello. Welcome to the world.<br>”);}

Page 53: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure
Page 54: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Nesting For Loops

• We can have nested loops just like If/Else blocks

Page 55: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

for (count = 1; count < 5; count += 1){document.write(count+“Hello. Welcome to the world.<br>”);

for (nestcount=1, nestcount<3; nestcount+=1){document.write(“Stop!”);}

}

Page 56: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

for (count = 1; count < 11; count += 1){

if (count = 5){document.write(“I’m halfway through.”);}

else{document.write(“I’m part of a loop.”);}

}

Page 57: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

While Loops

• Looks at short comparison and repeats until comparison is no longer true

Page 58: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

var count = 1;while (count < 11){

document.write(“I’m part of a loop.<br>”);count +=1;

}

Page 59: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

Do While Loops

• Loop executed at least once , even if comparison used return false the first time.

Page 60: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

var count = 1;do{

document.write(“Hi!”);count +=1;

} while (count < 6);

Page 61: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

var count = 11;do{

document.write(“Hi!”);count +=1;

} while (count < 10);

Page 62: Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure

What We Learnt Today …

• Learnt about conditional statements and their structure

• Learnt about loops and their structure

• Identified various types of conditional statements and loops

• Found out how conditional statements and loops are used in scripts