1 / 32

ITBP 119 Algorithms and Problem Solving

ITBP 119 Algorithms and Problem Solving. Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables. outline. Installation of the software Writing you first programs Doing VS Asking Function Literals Assignment Statements Variables Declaration and Initialization

keala
Download Presentation

ITBP 119 Algorithms and Problem Solving

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITBP 119Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables

  2. outline • Installation of the software • Writing you first programs • Doing VS Asking Function • Literals • Assignment Statements • Variables Declaration and Initialization • Expressions • Variable Naming • Function Parameter Evaluation

  3. Installing Arachnophilia 4.0 • Make a folder APS under My Documents and use it to store all files for this course. • Download the zip file Arachnophilia from the Blackboard • Unzip the file • Double click on the file arach_full.exe • Follow the instructions

  4. Installing Arachnophilia 4.0 • Set the preview browser to the internal browser

  5. Creating, editing, previewing HTML files • Create new HTML page • edit the title • Add new header • Add an image • Change the size of the image

  6. First Program • The use of alert and prompt • Let’s write a program that prompt the user with her name and the computer will greet that name by saying: hello ….. • Remember to be in the frames mode so that you can reduce the amount of typing.

  7. Important concepts • Script • Comment • Alert • Prompt • Function parameters • Function return values • Java script is case sensitive

  8. Doing Vs. Asking Functions • alert: alert( “hello World !!!”); • doing function in which you are asking the function to do some task and return nothing • Void function • prompt: var age = prompt (“enter your age”, “20”); • Asking function: in which you are asking the function to do some task and return a value. • Return values are normally assigned to a variable otherwise it will be discarded.

  9. Doing Vs. Asking Functions void function alert ( “ hello World “ ) ; parameters Function name Return value var age = prompt ( “ enter age: “ , “20” ) ; prompt ( “ enter age: “ , “20” ) ; Is this wrong?

  10. Literals • By literal we mean any number, text, boolean or other information that represents a value. • Example: • “ It’s true that ‘ is a literal” • 2008 • “ today is 2 / 9 /2008 “ • true • “ this#literal*contains%special@characters “ • -12.5 • “ here I include double quote \” as part of the literal” • String Literals are specified in programs with double quotes “” • Number and boolean literals are NOT specified by double quotes “”. • Boolean means either true or false value.

  11. Assignment Statements • var Variable = expression • Variables are named memory cells. • A variable contains a value thus you write person Hind to indicate that the variable person contains the value "Hind". var person = “Hind” ; Value stored in The memory location Hind Person Variable Memory location contains the value Hind

  12. Assignment Statements • var Variable = expression • Expression: any literal value, or a mathematical/logical expression. • Example of expressions: • var X = 10; • var Y = 100; • X + Y • var L = true • var name = “your Name “; • var L2 = X < Y && Y != X ; • var X2 = X * X + Y * Y * Y / 12.3 + 7.4;

  13. Assignment Statements/Evaluation response = “Samira” ; message = “hello “ + response ; message = “Hello “ + response ; Samira Hello Samira RHS LHS Samira Hello Samira response message

  14. Assignment Evaluation • The Right Hand side of the assignment is evaluated/calculated first. • The right hand side value is assigned to the left hand side of the assignment. • The left hand side of the assignment must be always a variable (or none). • var “xyz” = 12;  wrong declaration • var x + 10 = y;  wrong declaration

  15. Exercise • Given the following variables var month = “September” ; var year = 2008 ; var day = 2 ; • Calculate the value of each variable in the following assignment statements. • tomorrow = day + 1; • lastYear = year – 1; • var date1 = day + month + year ; • var date2 = day + “ month “ + year; • date3 = day + month + “ year “; • date4 = “ day ” + month + year;

  16. Exercise** • Given the following variables var x = “ hello ” ; var y = 2008 ; var z = 2 ; • Calculate the value of each variable in the following assignment statements. • var w = z + 1; • y = y / 4 + 7 ; • y = y – 1; • var d = x + y ; • var d2 = x + y + x + “ again ”; • var d3 = x + “ d2 “; • z + 1 ; • var d4 = z * 5 ;

  17. Assignment Statements • Which of the following is correct assignment and why? • var “salim” = 10; • var salim ; 3. var age = 22; var newAge = 11; age + 10 = newAge ;

  18. More about Variables / Variable Names • Variable Names: the rules of java script state the following about variable names: • The name of the variable cannot be the same as any of the language keywords. • Variable name can be any sequence of numeric, alphabet, or the special characters _, $ • Variable name should only start with alphabet ,under score _, or $. • Variables are case sensitive

  19. Exercise • Example: identify the correctness of the following variables names. • var my_age = 25; • var _myAge = 22; • var TODAY = “4/9/2008” ; • var $amir_ = “ SAMIR” ; • var alert = 10.43 ; • var Alert = 10.2 ; • var prompt = “ prompt me please “; • var salim%ali = true ; • var 1foo = false;

  20. Function Parameter Evaluation • The parameter of a function can be an expression. • If the parameter is an expression then it is evaluated/calculated first and then it is passed to the function.

  21. Function Parameter Evaluation • Example: var x =10; alert( x + 10 ); • The parameter of the function alert is the expression x + 1. • Before the alert is run, the computer computes the value of the expression which is x+1 = 10 + 1 = 11 • The value 11 is passed to the alert.

  22. Exercise • Which of the following are literals and which are variables? • “Fatima” • _salim • dateOfBirth • 12343 • “12343” • “3.14” • 12.423222 • Pi

  23. Exercise • Given the following two variables, what is the difference between the following: • var name = “zena” • var friend = “hoda” name = friend ; name = “ friend “;

  24. Initialization and Declaration • Declaration of a variable: is a way to give a name to some memory location. • Use the keyword var to declare a variable • There will be No initial value in the memory • Example: variable declarations var age ; var name ; var firstName, lastName ;

  25. Initialization and Declaration • Initialization of variable: Storing a value in the variable. • Example: var age; var name ; var firstName, lastName; age = 10; name = “Mahir”; firstName = “Salwa”; lastName = name; declarations initialization

  26. Initialization and Declaration • We can initialize a variable while declaring it • Example: var age = 10; var name = “Mahir” ; var firstName = “Salwa” , lastName= name ;

  27. Exercises • What will be the output of the alert function in the following (explain your answers) … <script> var name = “alia”; alert(name); name =prompt(“ enter name:” , “salma”); alert(name); </script> …

  28. Exercises • What will be the output of the alert function in the following (explain your answers) … <script> var name = “alia”; prompt(“ enter name:” , “salma”); alert(name); </script> …

  29. Exercises • What will be the output of the alert function in the following (explain your answers) … <script> var name ; var fName=prompt(“ enter name:” , “salma”); alert(name); alert(fName); </script> …

  30. Exercises • What will be the output of the alert function in the following (explain your answers) … <script> var fName=prompt(“ enter name:” , “salma”); var msg1 = “hello” + fName; alert( msg1); var msg2 = “welcome “ + “fName” ; alert( msg2); </script> …

  31. Exercises • What will be the output of the alert function in the following (explain your answers) … <script> var x = “salam” ; var msg1 = “hello ” + x ; alert( msg1); x = x + “ alaykum “; alert( x); </script> …

  32. Exercises • What will be the output of the alert function in the following (explain your answers) … <script> var x = 10 ; alert( x); x = x * 10 ; alert( x); x = x / 20; alert(x); </script> …

More Related