COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

SELECTION II CSC 171 FALL 2004 LECTURE 9. Sequential statements start end Block{…}
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
COMP 110 Introduction to Programming Tabitha Peck M.S. January 9, 2008 MWF 3-3:50 pm Philips 367.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 COMP 110 Inheritance Tabitha Peck M.S. April 16, 2008 MWF 3-3:50 pm Philips 367.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 CS 177 Week 4 Recitation Slides Conditionals. 2 Announcements Project 1 is due on Feb. 7th 9pm (extended) Project 2 will be posted today (Feb. 5th)
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
COMP 110 More Loops and Strings Tabitha Peck M.S. February 18, 2008 MWF 3-3:50 pm Philips
1 COMP 110 Arrays Tabitha Peck M.S. March 31, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
1 COMP 110 Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Designing Programs Tabitha Peck M.S. January 16, 2008 MWF 3-3:50 pm Philips
COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
March /18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class The ExamThe Exam Homework NotesHomework Notes.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 05 (Part III) Control Statements: Part II.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Catie Welsh February 7,  Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 2.
1 CS161 Introduction to Computer Science Topic #8.
Char ch; ch ‘L’‘X’‘V’‘I’ As in Roman numerals Want to give each a value, n say switch (ch) { case ‘I’:n = 1; break; case ‘V’:n = 5; break; … default:cout.
CS 1430: Programming in C++.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
Week 3 Part 2 Kyle Dewey.
Haidong Xue Summer 2011, at GSU
الكلية الجامعية للعلوم التطبيقية
Selection (if-then-else)
The Java switch Statement
Fundamental Programming
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Announcements Program 1 due noon Lab 1 due noon
Presentation transcript:

COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips 367 1

Announcements New Homework Policy Incorrect submissions == lose a letter grade Three incorrect submissions == zero credit Check.jar files BEFORE you send them See me if you are still having problems Grades Lab 1, Lab 2, Project 1 Posting Grades online? Handing back grades? 2

Questions? 3 From Lab 2 No real problems Never got the same problem twice

Gotcha == var1 = var2 (assignment statement) Error!!!!!!! var1 == var2 (boolean expression) Do NOT use == to compare Strings string1 == string2 //BAD string1.equals(string2); //GOOD 4

Gotcha (Syntax) if (boolean expression); DO NOT DO THIS!!!!!!!

Today in COMP 110 More if / else statements Case statements 6

Tracing if / else code I give you code and input You give me output 7

Example input = 5? input = 6? int days = 0; if (input < 6) System.out.print(“I worked “ + input + “days this week”); else { days = 6 – input + 1; System.out.print(“I worked “ + days + “ days of overtime”); } 8

Another Example Write the if/else part of a program that determines if a person is old enough to vote based on their age. If the person is old enough to vote, ask if they are voting in the democrat or republican primary, and then if they are voting for Obama or Clinton, or McCain or Romney. 9

Switch Statements switch (numOfSiblings) { case 0: System.out.print(“An only child”); break; case 1: System.out.print(“Just one you say”); break; case 2: System.out.print(“Two siblings!”); break; case 3: System.out.print(“Big family!”); break; default: System.out.print(“I don’t believe you”); break; } 10 Controlling expression integer or char ONLY! Break statement Case label

Practice with Switch statements Write a switch statement that takes as controlling expression your year in college (as in int) and outputs your year in college as freshman, sophomore, junior, senior, or super senior

Shorthand for if / else 12 if (n1 > n2 ) max = n1; else max = n2; max = (n1 > n2) ? n1 : n2;

Wednesday Read 3.2 Loop Statements 13