Lesson 7 Part 2 Flags

Similar documents
Oct Decision Structures cont d

Program Control Flow

Program Control Flow

Following is the general form of a typical decision making structure found in most of the programming languages:

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Conditional Programming

Chapter 3. Selections

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Handout 4 Conditionals. Boolean Expressions.

What methods does the String class provide for ignoring case sensitive situations?

Chapter 5 Lab Methods

Chapter 3 Lab Decision Structures

2.2 - Making Decisions

Selections. CSE 114, Computer Science 1 Stony Brook University

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Task #1 The if Statement, Comparing Strings, and Flags

In this chapter, you will:

Midterm Examination (MTA)

The Java language has a wide variety of modifiers, including the following:

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Decisions: Logic Java Programming 2 Lesson 7

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CONDITIONAL EXECUTION: PART 2

Java I/O and Control Structures

Repetition. Chapter 6

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Repetition. Chapter 6

AP COMPUTER SCIENCE A

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Chapter 5 Lab Methods

Flow of Control. Chapter 3

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Fundamentals of Programming Data Types & Methods

Flow of Control. Chapter 3

Programming with Java

Lecture 8 " INPUT " Instructor: Craig Duckett

Java I/O and Control Structures Algorithms in everyday life

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Chapter 4: Control Structures I

Loops. GEEN163 Introduction to Computer Programming

What two elements are usually present for calculating a total of a series of numbers?

Loops. CSE 114, Computer Science 1 Stony Brook University

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Flow of Control: Loops. Chapter 4

Flow of Control. Chapter

1 Short Answer (10 Points Each)

JAVA OPERATORS GENERAL

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Introduction to Computer Programming

Control Structures: if and while A C S L E C T U R E 4

Evaluating the Style of your programs

Java is an objet-oriented programming language providing features that support

Anatomy of a Java Program: Comments

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CSIS 10A Assignment 3 Due: 2/21 at midnight

Conditional Execution

STUDENT LESSON A12 Iterations

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Tutorial 03. Exercise 1: CSC111 Computer Programming I

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Object Oriented Programming. Java-Lecture 6 - Arrays

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Question: Total Points: Score:

Topics. Chapter 5. Equality Operators

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Over and Over Again GEEN163

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal

Repetition, Looping. While Loop

Question: Total Points: Score:

Selection Statements and operators

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Chapter 3 Selection Statements

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Algorithms and Conditionals

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Chapter 3: Decision Structures

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

A+ Computer Science -

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Chapter 3: Decision Structures

Full file at

Chapter 1 Lab Algorithms, Errors, and Testing

CSIS 10A Assignment 4 SOLUTIONS

Repetition CSC 121 Fall 2014 Howard Rosenthal

Transcription:

Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means some condition does not exist. if(score > 95) highscore = true; Here, highscore is a flag indicating that the score is above 95. Right now, we don t have any situations where these are terribly useful, but for now, just know we can and will use them. The if- if Statement Sometimes you need to be able to test a series of conditions You can do this with the if- if statement General form: if (BooleanExpression1) statement or block 1 if(booleanexpression2) statement or block 2 statement or block 3 If BooleanExpression1 is true, then statement or block 1 is executed. If BooleanExpression1 is false, then BooleanExpression2 is tested. If BooleanExpression2 is true, then statement or block 2 is executed. If BooleanExpression2 is false, then statement or block 3 is executed.

Note: You can have as many if clauses as is needed.

if- if Example import java.util.scanner; public class IfElseIfStatement { public static void main(string[] args) { int testscore; Scanner keyboard = new Scanner(System.in); System.out.print("Enter your numeric test score: "); testscore = keyboard.nextint(); if (testscore < 60) System.out.println("Your grade is an F."); if (testscore < 70) System.out.println("Your grade is a D."); if (testscore < 80) System.out.println("Your grade is a C."); if (testscore < 90) System.out.println("Your grade is a B."); if (testscore < 101) System.out.println("Your grade is an A."); System.out.println("Invalid Grade"); //end main and class

Nested if Statements Nesting is enclosing one structure inside of another. A block in Java can contain any valid Java code, this includes other if statements: if(booleanexpression1) { if(booleanexpression2) { statement3; statement4; statement1; statement2; If BooleanExpression1 is true and BooleanExpression2 is true, what is executed? statement1, statement2, statement3, statement4 If BooleanExpression1 is true and BooleanExpression2 is false, what is executed? statement3, statement4 If BooleanExpression1 is false, what is executed? Nothing Nested if Statements Example import java.util.scanner; public class NestedIfStatements { public static void main(string[] args) { double salary, yearsonjob; Scanner keyboard = new Scanner(System.in); System.out.print("Enter your annual salary: "); salary = keyboard.nextdouble(); System.out.print("Enter the number of years at your current job: "); yearsonjob = keyboard.nextdouble();

Logical Operators if (salary >= 30000) { if (yearsonjob >= 2) System.out.println("You qualify for a loan."); System.out.println("You must have been at your current job " + "for at least 2 years to qualify."); { System.out.println("You must earn at least $30,000 per year " + "to qualify."); Java provides logical operators. The binary logical operators combine two boolean expressions into one. The unary logical operator switches the value of a boolean expression. Operator Meaning Kind && AND Binary OR Binary! NOT Unary Binary logical operators have lower precedence than relational operators (they will be evaluated after) NOT has the same precedence as negation. Logical Operator Truth Tables Truth Tables show the result of a logical expression based on the values of the operands.

Logical Operator Practice 2 > 3 && 4 < 5 2 < 3 && 4 < 5 2 > 3 4 < 5 2 > 3 4 > 5!(2 > 3) Logical AND Example import java.util.scanner; public class LogicalAndOperator { public static void main(string[] args) { char walks, quacks; Scanner keyboard = new Scanner(System.in); System.out.print("Does it walk like a duck (Y or N)? "); walks = keyboard.nextline().charat(0);

System.out.print("Does it quack like a duck (Y or N)? "); quacks = keyboard.nextline().charat(0); if(walks == 'Y' && quacks == 'Y') System.out.println("It's proabably a duck."); System.out.println("It's proabably NOT a duck.");