03 adding logic and control
Upcoming SlideShare
Loading in...5
×
 

03 adding logic and control

on

  • 289 views

 

Statistics

Views

Total Views
289
Views on SlideShare
289
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

03 adding logic and control 03 adding logic and control Presentation Transcript

  • ADDING LOGIC ANDCONTROL TO YOURPROGRAM
  • Logic is the stuff that makes softwaresoftware Comparison operators Looping Conditional testing Functions
  • Comparison Operators•  ==•  !=•  ===•  !==•  >•  <•  >=•  <=
  • Conditional statementsif ( boolean ) { ! !doStuff(); !}!else if (anotherBoolean) { ! !doOtherStuff(); !}!else { ! !doSomeOtherStuff(); !}!
  • switch (answer) {! case yes:!Switch // Do stuff! break;!statements are case no:!more abstract // Do stuff! break;!ways of doing case not sure:!multiple else/ // Do stuff!ifs break;! default:! // Do stuff! }!
  • Boolean operators && ! ||
  • LOOPS
  • Looping•  Continuous loopswhile (boolean) {! doStuff();!}!•  Discrete loopsfor (! initializer; ! continue criteria; ! incrementer) {! doStuff();!}!
  • JavaScript supports pre-test loops aswell as post-test loops Pre-test Post-testwhile (boolean) {! do {! doStuff();! doStuff();!}! } while (boolean); !
  • Looping through arraysvar i = 0;!while (i < arr.length) {! doSomething(arr[i]);! i++;!}!for (var i=0; i<arr.length; i++) {! doSomething(arr[i]);!}!
  • FUNCTIONS
  • Remember, JavaScript is weird•  Functions are weird
  • Functions allow us to reuse code• Declaring itfunction func(p1, p2) {! /* Do things with p1 and p2 here. */! return anythingYouWant;!}!orfunc = function(p1, p2) {! /* Do things with p1 and p2 here. */! return anythingYouWant;}!• Calling itvar x = func(5, "hello");!
  • Functions always return a valuefunction foo() {!}!•  orfunction foo() {! return;!}!var x = foo();!•  Now xs type is undefined.
  • Variables are local to functions
  • Conclusion•  Logical operators•  Conditional tests •  if – else if - else•  Loops •  while •  for •  do/while•  Functions create reusable code•  Variables in functions are local scope