Conditional Statements and Control Structure

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

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.
DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
Loops (Part 1) Computer Science Erwin High School Fall 2014.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Angela Des Jardins.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Lecture 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
ACSE th Conference The Iconic Programmer Stephen Chen.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
Building Java Programs
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Introduction to Java Java Translation Program Structure
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Maria Kazachenko
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Sensor Information: while loops and Boolean Logic.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Java Programming Fifth Edition
CSC111 Quick Revision.
Data Types and Expressions
Building Java Programs
Chapter 3 Edited by JJ Shepherd
Chapter 6 More Conditionals and Loops
SELECTION STATEMENTS (1)
Topic 11 Scanner object, conditional execution
Building Java Programs
Microsoft Visual Basic 2005 BASICS
Building Java Programs
Building Java Programs
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Building Java Programs
C# Basics These slides are designed for Game Design Class
Building Java Programs
Selection Statements.
Computer Science Core Concepts
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 4
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Conditional Statements and Control Structure 5.1-

Control Structures Code which alters the flow of a program Conditionals- run code based on decisions Loops- repeats code until a condition is met

Conditional statements If statements – An if statement allows you to run specific blocks of code only if a given condition is true. Syntax: if (Boolean Condition) { code that you want to run }

If/else Allows you to run one block of code if a condition is true and another if a condition is false Syntax: if(Boolean Condition) { code to run if true } else code to run if false

Relational (Conditional) operators == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to **You can also use any Boolean variable or method

Avoid duplicate Code

This is better

What will the output be? String s1 = “Great Valley”; String s2 = “Great Valley”; if(s1 == s2) System.out.print(“same”); else System.out.print(“not the same”);

String Comparisons == operator compares what is stored in the variable. For strings or any other object that is simply a memory address. To compare strings use the method .equals(some string)

Alphabetical order

Be careful and pay attention to details

This will work correctly String s1 = “Great Valley”; String s2 = “Great Valley”; if(s1.equals(s2)) System.out.print(“same”); else System.out.print(“not the same”);

Multiple Alternatives Suppose you want to choose from several options. You can use an else if to check. For example if I want to withdraw money from a bank account, it might look like the following

Scanner in = new Scanner(System. in); System. out Scanner in = new Scanner(System.in); System.out.print(“withdrawal amount: “); double amt = in.nextDouble(); if (amt < 0) { System.out.println(“negative withdraw”); } else if (amt > balance) System.out.println(“insufficient funds”); else withdrawal(amt);

Exercise I will supply a dice class with a roll method Review the class, then create a die. Create a Main class which will roll the die 10 times and report how many times each number is rolled. Print your main class and console and turn in tomorrow