In this lab, you will learn more about selection statements. You will get familiar to

Similar documents
LAB 5: SELECTION STATEMENTS

Chapter 3 Selection Statements

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Eng. Mohammed S. Abdualal

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

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Selection Statements. Pseudocode

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

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

Decision Making in C

Selections. CSE 114, Computer Science 1 Stony Brook University

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

3chapter C ONTROL S TATEMENTS. Objectives

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

5. Selection: If and Switch Controls

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Information Science 1

Review for Test 1 (Chapter 1-5)

Flow of Control. Chapter 3 Part 3 The Switch Statement

Lecture 5 Tao Wang 1

Chapter 2: Functions and Control Structures

CHAPTER 5 FLOW OF CONTROL

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

conditional statements

Information Science 1

Lecture 1 Java SE Programming

3 The L oop Control Structure

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Object-Oriented Programming in Java

Lecture 4. CS118 Term planner. The simple if statement. Control flow: selection. The if... else statement cont... The if... else statement.

5. Control Statements

Flow Control. CSC215 Lecture

INTRODUCTION TO COMPUTER SCIENCE - LAB

Lecture 3 Tao Wang 1

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Control Structures in Java if-else and switch

Repetition Structures

4. Java language basics: Function. Minhaeng Lee

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

SELECTION. (Chapter 2)

Condition Controlled Loops. Introduction to Programming - Python

APCS Semester #1 Final Exam Practice Problems

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

CONDITIONAL EXECUTION: PART 2

Chapter 4: Making Decisions

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

Chapter 4: Making Decisions

DECISION MAKING STATEMENTS

Introduction. C provides two styles of flow control:

All about flow control

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

LAB 4.1 Relational Operators and the if Statement

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

CHAPTER : 9 FLOW OF CONTROL

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

Conditional Statement

8. Decision-Making Statements. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Any Integer Can Be Written as a Fraction

Programming Lecture 4

Lab # 02. Basic Elements of C++ _ Part1

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Lecture 10: for, do, and switch

Important Java terminology

Week 4 Selection Structures. UniMAP Sem II-11/12 DKT121 Basic Computer Programming 1

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

LECTURE 04 MAKING DECISIONS

204111: Computer and Programming

SELECTION STATEMENTS:

Use of scanf. scanf("%d", &number);

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Module 2: Choice and Iteration

Control Structure: Selection

PROGRAMMING FUNDAMENTALS

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Control Structures in Java if-else and switch

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Topics. Chapter 5. Equality Operators

Chapter 7. Additional Control Structures

COMP 208 Computers in Engineering

Transcription:

Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements. Nested if Statements: When you use if or if...else statement, you can write any statements to be executed in true or false cases including another if / if else statement. The inner if is called nested if. Check the following code to see how it works. The output: 1

Note that the else clause matches the most recent unmatched if clause in the same block. You can use braces to make your own match. See next code. The output: Here nothing is printed because the second if statement has nothing to be executed in the false case. Switch Statement Switch is a shorthand for nested if else statements. It is used to replace a stack of if statements that all relate to the same quantity. Here is the full syntax for the switch statement: switch (switch-expression) { case value1: statement(s)1; switch statement94 Chapter 3 Selections case value2: statement(s)2; 2

... case valuen: statement(s)n; default: statement(s)-for-default; } When using switch statement, observes the following rules: The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. The value1, and valuen must have the same data type as the value of the switch-expression. Note that value1, and valuen are constant expressions, meaning that they cannot contain variables, such as 1 + x. When the value in a case statement matches the value of the switchexpression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. The keyword break is optional. The break statement immediately ends the switch statement. The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end. 3

Caution: Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as fallthrough behavior. For example, the following code prints character a three times if ch is 'a': switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Exercise: Write a program that reads a character from the user then prints a word beginning with this character. To simplify work, test for a, b, c characters. Otherwise print invalid character. Homework: Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days. 4

(Game: scissor, rock, paper) Write a program that plays the popular scissorrock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs: Hint: for generating a random number you can use Math.random() method. (int)(math.random() *10) returns a random single-digit integer (i.e., a number between 0 and 9). 5