1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Slide 1 of 64 Lecture C Lesson 3: Conditions and Loops Unit 1: The if Statement.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue 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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Unit 3 1 Flow of control H We will talk about: conditional statements  if-then-else  logical and conditional operators  switch repetition statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control statements Mostafa Abdallah
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Control Statements: Part1  if, if…else, switch 1.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Selections Java.
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
Operator Precedence Operators Precedence Parentheses () unary
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)
Chapter 4: Control Structures I
CS2011 Introduction to Programming I Selections (II)
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
Logic of an if statement
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Conditionals and Loops
Control Structure.
Presentation transcript:

1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking your input. H Logical Operators.

2 Reading Input from the user H We leave the discussion of Input for later chapters H For now we will supply you with classes for getting input in all exercises and examples.  The class called EasyInput allows you to read input from the user. H In order to read an integer/double we use EasyInput as follows: int numberOfItems = EasyInput.readInt(); double width = EasyInput.readDouble();

3 Reading Input (contd.) H Please note that EasyInput is not a core Java class. We have supplied it for simplicity. H The EasyInput api is supplied on the course site.

4 Example - Multiplying two numbers // Requests two integers and prints their // multiplication. class MultiplicationExample { public static void main(String[] args) { int a,b; int mul; System.out.print(“The first number: “); a = EasyInput.readInt(); System.out.print(“The second number: “); b = EasyInput.readInt(); mul = a * b; System.out.println(“The multiplication is” + mul); }

5 Example: Circle area and circumference // Reads the radius of a circle and prints // its circumference and area to an output window class CircleExample { static final double PI = ; public static void main(String[] args) { double r, circumference, area; System.out.print(“Enter radius: “); r = EasyInput.readDouble(); circumference = 2*PI*r; area = PI*r*r; System.out.print(“Circumference: “); System.out.println(circumference); System.out.print(“Area: “); System.out.println(area); }

6 The if Statement H The Java if statement has the following syntax: if (boolean-condition) statement; H If the Boolean condition is true, the statement is executed; if it is false, the statement is skipped H This provides basic decision making capabilities

7 Temperature class Temperature { static final int THRESHOLD = 65; public static void main(String[] args) { System.out.print(“Enter the temperature:”); int temperature = EasyInput.readInt(); System.out.println(“Current temperature “+ temperature); if (temperature < THRESHOLD) System.out.println(“It’s cold in here!”); }

8 Boolean Expressions  The condition of an if statement must evaluate to a true or false result H Java has several equality and relational operators: Operator == != < <= > >= Meaning equal to not equal to less than less than or equal to greater than greater than or equal to

9 Block Statements H Several statements can be grouped together into a block statement H Blocks are delimited by braces H A block statement can be used wherever a statement is called for in the Java syntax  See Temperature2.java

10 Example - Temperature2 class Temperature2 { static final int THRESHOLD = 65; public static void main(String[] args) { System.out.print(“Enter the temperature: ”); int temperature = EasyInput.readInt(); System.out.println(“Current temperature “+ temperature); if (temperature < THRESHOLD) { System.out.println(“It’s cold in here!”); System.out.println(“But we’ll survive.”); }

11 If.. Else Statement  An else clause can be added to an if statement to make it an if-else statement: if (condition) statement1; else statement2; H If the condition is true, statement1 is executed; if the condition is false, statement2 is executed  See Temperature3.java.

12 Example - Temperature3 class Temperature3 { static final int FREEZING_POINT = 32; public static void main(String[] args) { System.out.print(“Enter the temperature: ”); int temperature = EasyInput.readInt(); if (temperature <= FREEZING_POINT) System.out.println(“It’s freezing!”); else System.out.println(“Above freezing.”); }

13 Checking your Input H When requesting input from the user, you keep in mind that the input may be invalid. One way to handle this is to use if – else control statement: //number of items should be positive int numberOfItems = EasyInput.readInt(); if (numberOfItems < 0) { System.out.println( “Number of items must be positive!”); } else { double price = numberOfItems * ITEM_PRICE; System.out.print(“The total price is:“); System.out.println(price); }

14 Logical Operators H There are three logical operators in Java: H They all take boolean operands and produce boolean results H Logical NOT is unary (one operand), but logical AND and OR are binary (two operands) Operator ! && || Operation Logical NOT Logical AND Logical OR

15 Logical NOT H The logical NOT is also called logical negation or logical complement  If a is true, !a is false; if a is false, then !a is true H Logical expressions can be shown using truth tables a false true !a true false

16 Logical AND  The expression a && b is true if both a and b are true, and false otherwise H Truth tables show all possible combinations of all terms a false true b false true false true a && b false true

17 Logical OR  The expression a || b is true if a or b or both are true, and false otherwise a false true b false true false true a || b false true

18 Logical Operators H Logical operators are used to form more complex logical expressions if (a<1 || a%2!=0) { System.out.println( “The input should be a positive “+ “even number!”); return; } H Logical operators have precedence relationships between themselves and other operators

19 Logical Operators H Full expressions can be evaluated using truth tables a < 1 false true a%2!=0 false true false true a<1 || a%2!=0 false true

20 Nested If // Receives 2 integers and compares them class CompareExample { public static void main(String[] args) { System.out.print(“First number: ); int a = EasyInput.readInt(); System.out.print(“Second number: ); int b = EasyInput.readInt(); if (a != b) if (a > b) System.out.println(a+” is greater”); else System.out.println(b+” is greater”); else System.out.println(“the numbers are equal”); }

21 Switch Construct Switch( integral expression) { case integral-value1: statements; break; case integral-value2: statements; break; : case integral-valueN: statements; break; default: statements; break; }

22 Switch Construct (cont.) Switch( integral expression) { case integral-value1: case integral-value2: statements; break; : case integral-valueN: statements; break; default: statements; break; }

23 public class Schedule { public final int SUNDAY = 1; public final int MONDAY = 2; public final int TUESDAY = 3; public void printSchedule(int day) { System.out.println(“Schedule for today is:”); switch(day) { case SUNDAY: System.out.println(“10: :00 : Infi”); System.out.println(“12: :00 : Alg”); break; case MONDAY: case TUESDAY: System.out.println(“10: :00 discrete math”); break; default: System.out.println(“Error: No such day”); break; }