Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.

Slides:



Advertisements
Similar presentations
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Introduction to Computers and Programming Lecture 5 New York University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Selection Statements.
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Week 3 Part 2 Kyle Dewey.
Selections Java.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Announcements Quiz 1 Posted on blackboard
Introduction to programming in java
Multiple variables can be created in one declaration
Boolean Expressions and If
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,
SELECTION STATEMENTS (1)
Selection (if-then-else)
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Chapter 4: Control Structures I (Selection)
Lecture Notes – Week 2 Lecture-2
The Java switch Statement
Structured Program Development in C++
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Announcements Exercise Sets 2 and 3 Due Thursday.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Quiz 1 Exam 1 Next Monday

Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You have a B!” ); else System.out.println(“We’ll give you a C.” );

if / else (Cascaded) Sequence of if / else Statements Example: if (myGrade > 90) System.out.println(“You have an A!” ); else if (myGrade > 80) System.out.println(“You have a B!” ); else if (myGrade > 70) System.out.println(“You have a C!” ); else System.out.println(“Oh-oh!” );

Boolean Type A Boolean Value Is One of Either “True” or “False” In JAVA, Type boolean Example: boolean done = false;... if (currentLetter == ‘Z’) done = true; else done = false; … if (done) System.exit(0); if / else Conditionals Must Evaluate to True or False, and Are Therefore Called Boolean Expressions

Logical Operators A Logical Operator Is One Used to Further Specify True or False in an Expression Connects Two or More Expressions Together && Is Logical “AND” || Is Logical ‘OR” &&: Both Operands Must Be True for Entire Expression to Be True ||: Only One (or Both) of the Operands Must Be True for Entire Expression to Be True

Logical Operators if (numStudents > MIN && numStudents < MAX) classRun = true; if (numStudents > MAX || numInstructors == 0) classRun = false;

Operator Precedence () ! (not) *, /, % +, -, >= (Relational Operators) ==, != (Equality Operators) && (Logical AND) || (Logical OR) = (ASSIGNMENT)

Order of Operations Precedence: Level of Importance of Operations Multiplicative Operators Have Higher Precedence than Additive Operators: *, /, % Higher +, - Lower Associativity: Order of Operation for Equal Level Precedence Most Operators Have Left-to-Right Associativity Use Parentheses to Force Differing Precedence of Operations

Multiple Logical Operators if ( num1 > MAX || num2 == 0 && num3 == 0) System.out.println(“ num1 is MAX or something is 0”); System.out.println(“ I think…..” );

Switch Statements Also Called Switch/Case Statement Just Case in Other Languages Selects Among Several Different Actions Can Only Select from Integer or Character If an Integer Value Is Matched, Statements under Control of that Case Block Are Executed

Switch/Case Example final double BASERATE = ; double rate; int numPassengers; System.out.print(“ Enter Passengers: “); numPassengers = scan.nextInt(); switch(numPassengers) { case 2: rate = BASERATE * 0.80; break; case 4: rate = BASERATE * 0.75; break; case 5: rate = BASERATE * 0.55; break; default: rate = BASERATE; break; }

Switch Case Example char menuItem; String inputS; System.out.print("Enter Menu Selection: “); inputS = scan.next(); menuItem = inputS.charAt(0); switch(menuItem) { case 'O': case ‘o’: //Code to Order Something break; case 'C': case ‘c’: //Code to Checkout break; default: //Error Code break; }

Announcements Exam 1 Next Week in Monday Lecture Everything through Lab 4 50 minutes 10 % of Total Grade Covers Everything through Lecture this Week –Quiz 1 Topics (terminology, variables, constants, basics) –if-then-else –Compound statements –Relational operators –Logical operators –Switch/Case Statements