190 likes | 219 Views
Understand decision-making structures in JavaScript, including if statements, switch statements, and nested logic for efficient code execution. Master the use of conditional expressions and flow control techniques.
E N D
Tutorial 4Decision Making with Control Structures and StatementsSection A - Decision Making JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Tutorial 4A Topics • Section A - Decision Making • if statements • if…else statements • Nested if statements • switch statements JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • Decision Making • Process of determining the order in which statements execute in a program • AKA – flow control • Decision-Making Structures • Special type of JavaScript statements used for making decisions JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • if Statements • Used to execute specific programming code if the evaluation of a conditional expression returns a value of true • “Do this or don’t do this” • Syntax (3 primary parts) if (conditional_expression) { statement(s); } JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • if Statements • Operation • If the conditional expression is true, the statement(s) is/are executed • Control then continues to subsequent code • Command block • Multiple statements contained within a set of braces (require curly braces) JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • if Statements • Conditional Expression • Can consist of: • Comparison operators • Logical operators • Combination of the two • Must resolve to Boolean value JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • if…else Statements • Used to execute one set of code if the evaluation of a conditional expression returns a value of true, otherwise executes another set of code • “Do this or do something else” • Syntax if (conditional_expression) { statement(s); } else { statement(s); } JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • Nested if and if…else Statements • Nested statements • Statements contained within other statements • Syntax (variable) if (conditional_expression) { statement(s); if (conditional_expression) { statement(s); } } else { statement(s); } JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • switch Statements • Controls program flow by executing a specific set of statements, depending on the value of an expression • Syntax switch (expression) { case label1: statement(s); break; case label2: statement(s); break; default: statement(s); } JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
Decision Making • switch Statements • Case labels • Identify specific code segments • Can use a variety of data types as case labels • Break statement • Used to exit switch statements • Default label • Contains statements that execute when the condition expression doesn’t match any of the case labels JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements
JavaScript Tutorial 4 -Decision Making with Control Structures and Statements