Module 2: Input and If Statements

CMPS 162: Introduction to Programming

Introduction

It would be hard to write useful programs that do not take any user input, and so we will look at one mechanism to obtain it: prompts. Prompts are a simple way to obtain user input by showing a prompt to the user, which is essentially a field in which text can be entered. The input from the user can than be used in the program. For example, the program could ask the user to enter yes or no, and the actual value that is entered could be checked by an if statement. If the entered value is yes we can go into the if branch, otherwise we go into the else branch. Note that if is a statement in JavaScript, meaning that it does not evaluate to an expression and so you cannot assign it to a variable (in some languages the if construct is an expression). It is also possible to write more complicated conditional if statements using else if and else.

Module Objectives

  • Give examples of concatenating text strings
  • Obtain user input through prompts
  • Express if statements
  • Explain comparison operators
  • Apply if … else and else if statements
  • Illustrate testing sets of conditions
  • Apply if statements nested

Learning Resources

  • Module 2 Readings: Chapters 8 thru 14 from Myers

Learning Activities

For Further Study

Leave A Reply

Your email address will not be published. Required fields are marked *