if-then-else Conditional Control Statement

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Selection (decision) control structure Learning objective
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
PHP Conditions MIS 3501, Fall 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University September 11, 2014.
Conditional Statements Introduction to Computing Science and Programming I.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Running JavaScript Chapter 18.
 We are going to learn about programming in general…How to think logically and problem solve. The programming language we will use is Python. This is.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
1 2. Program Construction in Java. 2.4 Selection (decisions)
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures, String Comparison, Nested Structures
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
JavaScript Controlling the flow of your programs with ‘if’ statements
Special Triangles Review
Introduction to Decision Structures and Boolean Variables
‘C’ Programming Khalid Jamal.
Decision Making in C.
More on the Selection Structure
Standard and Expanded Form
COMP 14 Introduction to Programming
Python: Control Structures
WELCOME to….. The If Statement.
Lecture 3- Decision Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
Numbering System TODAY AND TOMORROW 11th Edition
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
If, else, elif.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4 Control Statements: Part I
Loop Control Structure.
Lesson 8: Boolean Expressions and "if" Statements
Conditions and Ifs BIS1523 – Lecture 8.
Selection CSCE 121 J. Michael Moore.
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
Objectives After studying this chapter, you should be able to:
We’re moving on to more recap from other programming languages
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Design and Implementation
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Python Programming Language
Decision I (if Statement)
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
CSC215 Lecture Control Flow.
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
while Loops Looping Control Statement
Functions Overview © 2018 Kris Jordan.
Visual Studio Code Walkthrough
The main Function, introcs Library, and Prompting
The return Statement © 2018 Kris Jordan.
Parameters and Arguments
The for Loop © 2018 Kris Jordan.
Arrays: Iteration Working through an array algorithmically.
Programming Fundamental
Presentation transcript:

if-then-else Conditional Control Statement © 2018 Kris Jordan

Example Setup In VSCode: Start the Development Server import { print, promptNumber } from "introcs"; export let main = async () => { print("I'm thinking of a number..."); let guess = await promptNumber("Guess:"); print("Your guess is..."); if (guess === 42) { print("Correct!"); } print("Game Over"); }; main(); In VSCode: Start the Development Server View Terminal npm run pull npm start Open the File Explorer Pane Right click on the src folder Select "New folder" Name it: x-if-then-else Right click on the x-if-then-else folder Select "New file" Name it: if-then-else-app.ts In if-then-else-app.ts, add the code to the right. © Kris Jordan

if-then Statements General form of an if-then statement: if (<"test" - boolean expression>) { <then block – runs when test is true> } if-then is a control statement It can be written anywhere you can write any other statement It is like a conditional phrase at the beginning of a sentence (and does not end in a semi-colon) The test in the parenthesis must be a boolean expression Statements in the "then block" curly brace will run if the test evaluates to true. Else, the processor jumps over the then block. © Kris Jordan

if-then Statements if test false In a flow chart ("control flow") we draw an if-then statement as a diamond. It will have two arrows coming out. We label these arrows for the two cases: true will point to code in the then block false will point to code following the then block true then © Kris Jordan

print("Your guess is..."); if (guess === 42) { print("Correct! "); } print("Game Over"); true false © Kris Jordan

How do we follow a different path when the test condition is false?

Example - Add an else clause Add an else clause like the one to the right. Try playing your game again and entering a correct guess as well as an incorrect guess. if (guess === 42) { print("Correct!"); } else { print("Nope!"); }

if-then-else Statements General form of an if-then-else statement: if (<"test" - boolean expression>) { <then block – runs when test is true> } else { <else block – runs when test is false> } Works the same as an if-then statement, however, when the test expression evaluates to false the statements within the else block will run. Once either block completes, the processor resumes at the line following the else block.

if-then-else Statements test false Notice, like the if-then statement, the then block runs only when the test condition is true Unlike the if-then statement, the else block runs only when the test condition is false After either the then-block or else-block complete, they both continue to the same next step true then else

Nesting if-then-else statements within if-then-else statements The then and else blocks may contain one or more statements… …but isn't if-then a statement? Yes! You can write further if-then statements inside of then or else blocks and the same rules apply.

Example - Add a nested if-then-else statement Add the nested if-then-else statement to the right inside of the else block. Your game should now indicate if the guess was too high or too low! if (guess === 42) { print("Yep!"); } else { if (guess > 42) { print("Too high!"); print("Too low!"); }