COMP 110 Introduction to Programming. What did we discuss?

Similar documents
COMP Introduction to Programming If-Else Statement, Switch Statement and Loops

COMP 110 Introduction to Programming. What did we discuss?

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

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

COMP 110 Introduction to Programming. What did we discuss?

COMP Introduction to Programming Midterm Review

COMP 110 Introduction to Programming. What did we discuss?

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

Flow of Control. Chapter 3. Chapter 3 1

COMP 110 Introduction to Programming. What did we discuss?

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Darrell Bethea May 25, 2011

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

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

Repetition Structures

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Logic & program control part 3: Compound selection structures

Quiz 1: Functions and Procedures

Control Structures II. Repetition (Loops)

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Problem Solving With Loops

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS111: PROGRAMMING LANGUAGE II

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Control Structures in Java if-else and switch

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

06 - Building Lists Recursively. Spring 2018 COMP110

Loops. CSE 114, Computer Science 1 Stony Brook University

REPETITION CONTROL STRUCTURE LOGO

npm run pull npm start

Repetition CSC 121 Fall 2014 Howard Rosenthal

Quiz Determine the output of the following program:

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Repetition, Looping. While Loop

Java Coding 3. Over & over again!

Conditionals, Loops, and Style

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSC 1051 Data Structures and Algorithms I

Introduction. C provides two styles of flow control:

ECE 122. Engineering Problem Solving with Java

Chapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Claremont McKenna College Computer Science

Information Science 1

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

CT 229 Java Syntax Continued

Comp 151. Control structures.

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Practice with if-then-else. COMP101 Lecture 7 Spring 2018

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

3chapter C ONTROL S TATEMENTS. Objectives

Top-Down Program Development

Flow Control. CSC215 Lecture

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

In this chapter, you will:

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Principles of Computer Science I

Chapter 4: Control structures. Repetition

CONDITIONAL EXECUTION: PART 2

conditional statements

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

DECISION CONTROL AND LOOPING STATEMENTS

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Chapter 3 Structured Program Development

Chapter 8 Statement-Level Control Structures

Computer Programming I - Unit 5 Lecture page 1 of 14

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Principles of Computer Science

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CMPT 125: Lecture 4 Conditionals and Loops

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

LOOPS. Repetition using the while statement

Conditionals and Recursion. Python Part 4

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

Chapter 4: Control structures

Flow of Control: Loops

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

LECTURE 04 MAKING DECISIONS

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

COMP 202 Java in one week

Information Science 1

Chapter 4: Making Decisions

CSC 1051 Data Structures and Algorithms I

Chapter 4: Conditionals and Recursion

Chapter 4: Making Decisions

Topics. Chapter 5. Equality Operators

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

1 Short Answer (10 Points Each)

Transcription:

COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1

Today Announcements Quiz today boolean logic and if else Assignment 1 : Due Tuesday, Sep 22 @ 11:55 PM http://cs.unc.edu/~aikat/courses/comp110/assignments/assignment1 HACKER110: goto http://comp110.com/hacker More If else COMP 110 Fall 2015 3 Multibranch If-Else Statement Example Write a program that takes as input your year in college (as an integer) and outputs your year as freshman, sophomore, junior, senior, or super senior COMP 110 Fall 2015 4 2

Multibranch If-Else Statement Flow chart: Prompt user for year 1 2 Which year? 3 4 5 freshman sophomore junior senior super senior COMP 110 Fall 2015 5 Multibranch If-Else Statement We can write a program like this if (year == 1) System.out.println("freshman"); else { if (year == 2) System.out.println("sophomore"); else { if (year == 3) System.out.println("junior"); else { if (year == 4) System.out.println("senior"); else { if (year == 5) System.out.println("super senior"); else System.out.println("huh?"); COMP 110 Fall 2015 6 3

Multibranch If-Else Statement Because the previous version is too tedious, we use the multibranch statement instead It is not a new syntax rule. We only ignore the brackets so that the logical structure is clear. if (year == 1) System.out.println("freshman"); else if (year == 2) System.out.println("sophomore"); else if (year == 3) System.out.println("junior"); else if (year == 4) System.out.println("senior"); else if (year == 5) System.out.println("super senior"); else System.out.println("huh?"); COMP 110 Fall 2015 7 Multibranch If-Else Statement Though all the branches look equal, there is a precedence order among them Only the first satisfied branch will be executed COMP 110 Fall 2015 8 4

Multibranch If-Else Statement What s wrong with this piece of code? if (num < 50) System.out.println( Number is less than 50 ); else if (num < 25) System.out.println( Number is less than 25 ); else System.out.println( Number is greater than 50 ); COMP 110 Fall 2015 9 Multibranch If-Else Statement What s wrong with this piece of code? if (num < 50) System.out.println( Number is less than 50 ); else if (time < 25) System.out.println( Number is less than 25 ); else System.out.println( Number is greater than 50 ); Will this branch get executed? COMP 110 Fall 2015 10 5

Nested If and Else if (time < 7){ if (time < 6){ go to the gym; else{ // note the corresponding if have brkfst and leave; else{ go to school; What s the logic flow? If the time is less than 6, we go to the gym; If the time is between 6 and 7, we eat breakfast and leave If the time is greater than 7, we simply sprint to school! COMP 110 Fall 2015 11 Nested If and Else if (time < 6){ go to the gym; else{ if (time < 7){ have brkfst and leave; else{ go to school; What s the logic flow? If the time is less than 6, we cook breakfast; If the time is between 6 and 7, we get something cold If the time is greater than 7, we go to school COMP 110 Fall 2015 12 6

Same Logic, Different Code if (time < 6){ go to the gym; else{ if (time < 7){ have brkfst and leave; else{ go to school; if (time < 7){ if (time < 6){ go to the gym; else{ have brkfst and leave; else{ go to school; COMP 110 Fall 2015 13 Nested If-Else Statement Without brackets, every else will automatically match the nearest if if ( num < 50 ) if ( num < 25 ) System.out.println( Number is less than 25 ); else System.out.println( Number is greater than 50 ); Is this piece of code correct? COMP 110 Fall 2015 14 7

Nested If-Else Statement Without brackets, every else will automatically match the nearest if if ( num < 50 ) { if ( num < 25 ) System.out.println( Number is less than 25 ); else System.out.println( Number is between 25 and 50 ); Use brackets and indentation to avoid such errors COMP 110 Fall 2015 15 Nested If-Else Statement Without brackets, every else will automatically match the nearest if if ( num < 50 ) { if ( num < 25 ) System.out.println( Number is less than 25 ); else System.out.println( Number is greater than 50 ); Use brackets and indentation to avoid such errors COMP 110 Fall 2015 16 8

Notes Note: To exit your program before end of main method System.exit (0); COMP 110 Spring 2015 17 LOOPS Loops are designed to repeat instructions Think about the requirement: Print number 1 to 10 It s easy System.out.println( 1 ); System.out.println( 2 ); Think about the requirement: Print number 1 to 100 We can still do this Let the user input a value n, then print 1 to n We are in trouble COMP 110 Spring 2015 18 9

Loop Statement What is the pseudo code to fulfill the requirement? Count to 1, if 1<=n, write it down, otherwise stop Count to 2, if 2<=n, write it down, otherwise stop Count to 3, if 3<=n, write it down, otherwise stop Count to i, if i<=n, write it down, otherwise stop Count to i+1, if i+1<=n, write it down, otherwise stop While a counter<=n, write it down, increase the counter. Otherwise stop COMP 110 Spring 2015 19 While Statement Flow of while statement Start from expression evaluation As long as it s true, repeat instructions in brackets while (count <= number) { count++; COMP 110 Spring 2015 20 10

While Statement You have to do some initialization before the statement The loop body typically contains an action that ultimately causes the controlling boolean expression to become false. number = keyboard.nextint(); count = 1; while (count <= number) { count++; COMP 110 Spring 2015 21 While Statement Usually there is a counter variable in the statement You can use it in different ways Requirement: print the odd numbers from 1 to 10000 int count = 1; while (count < 10000) { count += 2; int count = 1; while (count * 2 1 < 10000) { System.out.println(count * 2 1); count++; COMP 110 Spring 2015 22 11

Infinite Loops Always make sure that your loop will end Never forget to change the counter while (count <= number) { while (count <= number); { while (count <= number) { ; COMP 110 Spring 2015 23 Infinite Loops Always make sure that your loop will end Never forget to change the counter Use comparison rather than == or!= in the control expression Know whether your counter is increasing or decreasing while (count!= number) { count+=2; while (count < number) { count ; COMP 110 Spring 2015 24 12

Infinite Loops If you wrote an infinite loop and executed it Use the terminate button of eclipse If it is red, the program is running COMP 110 Spring 2015 25 Infinite Loops Infinite loop is not a syntax error. It s a logical error eclipse will not help you in this case Write pseudo code, think, and rethink before coding COMP 110 Spring 2015 26 13

Next class More loops Reading Assignment: Chapter 4 COMP 110 Fall 2015 27 14