The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.

Slides:



Advertisements
Similar presentations
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Advertisements

July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
COMP 14 Introduction to Programming Miguel A. Otaduy May 19, 2004.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
COMP 14 Introduction to Programming
Selection (also known as Branching) Jumail Bin Taliba by
Decisions Chapter 4.
CiS 260: App Dev I Chapter 4: Control Structures II.
Control Structures.
Selection (if-then-else)
Chapter 2 Programming Basics.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Quiz Grades

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Review Topics Comparisons with text Tokenizer objects Using the debugger Closing curly brace signals the end of an if statement.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Review Flow of Execution

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Two-Way Selection

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 The if-else Statement if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Nested if Statements The general syntax of a nested if statement is: if (condition1) { block1 } else if (condition2) { block2 } else { block3 }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Nested if Statements if (hasFourLegs) { if (playsFetch) System.out.println ("DOG"); else System.out.println ("CAT"); } else if (hasWings) System.out.println ("BIRD"); else System.out.println ("FISH"); has four legs does not have four legs plays fetch doesn't play fetch has wings doesn't have wings

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 The switch Statement

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 The switch Statement Provides another means to decide which statement to execute next Evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 The switch Statement The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value2, control jumps to here

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 The switch Statement Expression evaluated must be integral type ♦ integer or character, not boolean or floating point Case values must be constant (literal), not variable Can be implemented with nested if statements, but is much clearer with switch statements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 The switch Statement default ♦ no case value matches the expression ♦ if no default exists and no case value matches the expression, no statements in the switch statement are executed break ♦ processing jumps to statement following the switch statement ♦ usually at the end of each case

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 The switch Statement System.out.print ("Enter prize code: "); int prize = Integer.parseInt(keyboard.readLine()); switch (prize) { case 1: System.out.println (“A Brand New Car!”); break; case 2: System.out.println (“A Trip to Hawaii!”); break; default: System.out.println (“Sorry, Try Again”); break; }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie What is printed if movieRating is 1 and movieName is "Gigli"? 2. What is printed if movieRating is 5 ? Questions Assume movieRating is an int and movieName is a String switch (movieRating) { case 1: System.out.println ("Run away!"); if (movieName.equals("Gigli")) System.out.println ("Quickly!"); case 2: System.out.println ("Save your money"); break; case 3: System.out.println ("It's OK"); break; } Run away! Quickly! Save your money [Nothing]

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 What is printed with the following values of x and y : 1.x = 5, y = -4 2.x = -9, y = 5 3.x = 5, y = 5 Questions if ((x > 0) && (y < 0)) { z = x+y; System.out.println (“z = ” + z); } System.out.print (“The sum is “); if (x+y < 0) System.out.println(“negative.”) else System.out.println (“positive.”); z=1 The sum is positive. The sum is negative. The sum is positive.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Exercise Rock, Paper and Scissors Player 1 inputs (R)ock, (P)aper or (S)cissors Player 2 inputs (R)ock, (P)aper or (S)cissors Output message: ♦ Input of player A ♦ Input of player B ♦ Winner (or draw)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 To do Work on assignment 3. Read ch. 5.