Lecture Notes – Week 2 Lecture-2

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
The switch Statement, DecimalFormat, and Introduction to Looping
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Programming Fundamentals I (COSC- 1336), Lecture 3 (prepared after Chapter 3 of Liang’s 2011 textbook) Stefan Andrei 10/9/20151 COSC-1336, Lecture 3.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 05 Title: Decision/Selection Control Structures Reference: MalikFarrell, chap 1, Liang Ch 3.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Control statements Mostafa Abdallah
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Introduction to Control Statements IT108 George Mason University.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
CS0007: Introduction to Computer Programming
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Lecture 3 Selection Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Control Structures I
Lecture 4b Repeating With Loops
CNG 140 C Programming (Lecture set 3)
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Elementary Programming
Chapter 2 Elementary Programming
Selections Java.
The switch Statement, and Introduction to Looping
Decisions Chapter 4.
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
Introduction to programming in java
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Data types, Expressions and assignment, Input from User
Chapter 5 Control Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
IF if (condition) { Process… }
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
In this class, we will cover:
Lecture Notes – Week 3 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Control Structures Part 3
Lecture Notes – Week 3 Lecture-1
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Lecture Notes – Week 4 Chapter 5 (Loops).
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Lecture Notes – Week 2 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
In this class, we will cover:
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

Lecture Notes – Week 2 Lecture-2 Chapters 3 & 4

Outline Introduction to selection statements Boolean data type and Boolean expressions One-way if statements Two-way if-else statements Nested if and multi-way if-else statements Switch statements To do list

Checking Input from Keyboard /* This program reads a radius from keyboard and check whether the input is valid, ...*/ import java.util.Scanner; public class ComputerAreaWithConsoleInput { public static void main(String[] args) { Scanner input = new Scanner(system.in); double radius, area; final double PI = 3.14159; System.out.print("Enter a number for radius: "); double radius = input.nextDouble(); //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } We want to have a condition on the input. We use a selection statement to check the condition.

One-way if Statements /* This a program fragment from the previous slide … //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } A one-way if statement starts with keyword if, followed by a Boolean expression for the condition, then followed by a statement body. 4

One-way if Statements } if (Boolean-expression) { statement(s); if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } if (Boolean-expression) { statement(s); } 5

Relational Operators The examples are Boolean expressions. A Boolean expression consists of a variable or value on each side of a relational operator. The expression is evaluated as true if the relation operator is satisfied; otherwise it is evaluated as false. 6 6

Logical Operators The examples are logical expressions. A unary logical expression consists of a logical operator, followed by a Boolean expression or value. A binary logical expression consists of a Boolean expression or value on each side of a logical operator. 7 7 7

Two-way if-else Statements /* This a program fragment from the previous slide … //If the radius is not a negative number if (radius >= 0) { area = radius * radius * PI; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } else { System.out.println("Negative input"); If there are two alternative actions to be taken, we use a two-way if-else statement: one action is taken when the Boolean expression is evaluated as true while another is taken otherwise. For each alternative action, we would need a block of statements. 8

Two-way if-else Statements if (boolean-expression) { statement(s); \\first block of statement } else { statement(s); \\second block 9 9

Multi-way if-else Statements /* This a program fragment for a multi-way if-else statement … if (score >= 90.0) System.out.print(”A”); \\ if score >= 90.0 else if (score >= 80.0) System.out.print(”B”); \\ if score < 90.0 and >= 80.0 else if (score >= 70.0) System.out.print(”C”); \\ if score < 80.0 and >=70.0 else if (score >= 60.0) System.out.print(”D”); \\ if score < 70.0 and >= 60.0 else System.out.println(”F");\\ score < 60.0 This statement has 4 nested if-else statements and 5 alternative actions (5 ways). 10 10

Multi-way if-else Statements else if else if else if else 11 11 11

switch Statements switch (switch-expression) { The switch-expression must yield a value of char, byte, short, or int data type. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. value1, ..., and valueN can be constants only. 12 12 12 12

switch Statements The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. 13 13 13 13 13

To Do List Before Next Lecture Read Week 2 lecture slides. Read and run Week 2 program examples. Selectively read those sections in Chapters 3 and 4 that cover the topics in this lecture.