DCS/100: Procedural Programming

Size: px
Start display at page:

Download "DCS/100: Procedural Programming"

Transcription

1 DCS/100: wk 3 p.1/50 DCS/100: Procedural Programming Week 3: Making Decisions Queen Mary, University of London

2 DCS/100: wk 3 p.2/50 Last Week From last week you should be able to explain and write programs that use the following: variables types initialization and assignement expressions

3 DCS/100: wk 3 p.3/50 This Week At the end of this week you should be able to: write programs that make decisions explain the use of boolean expressions explain what is meant by if and switch statements trace programs containing if and switch statements

4 DCS/100: wk 3 p.4/50 Decisions, Decisions Straightline programming isn t enough. You have to be able to do different things in different circumstances. Example: Q: Which courses should I take this semester? A: if you are a G400 student, then Programming, Logic, Computer Architecture and C&S, but if you are a G4N1 student, then Programming, Logic, Computer Architecture and..., and if you are a...

5 DCS/100: wk 3 p.5/50 Cases Or imagine a cash machine: Do you want cash or a statement?...or your mobile phone with all those menus of options.

6 DCS/100: wk 3 p.6/50 If statements Programming languages solve this by having if statements: char ans= n ; out.write("hi, would you like me "); out.write("to insult you? "); ans = in.read(); if (ans == y ) out.writeln("you big ugly baboon!"); else out.writeln("oh, all right then.");

7 DCS/100: wk 3 p.7/50 if statement Form: if (<test>) <statement> else <statement>

8 DCS/100: wk 3 p.8/50 Flow of control is ans== y? yes no print insult don t print insult

9 DCS/100: wk 3 p.9/50 Doing more than one thing But often you want to do more than one thing: if (ans == y ) { out.writeln("you big ugly baboon!"); } else { out.writeln("oh, all right then."); out.writeln("i won t."); } Instead of a single command, you can put a whole sequence inside braces: {... }

10 DCS/100: wk 3 p.10/50 Doing more than one test You can do a sequence of tests: if (ans == y ) out.writeln("you big..."); else if (ans == n ) out.writeln("ok, I won t."); else out.writeln("answer yes or no!"); You can easily build these up to the point where your code is very opaque. Don t.

11 DCS/100: wk 3 p.11/50 The Infamous Dangling Else What does this mean? if (test1) if (test2) A; else B;

12 DCS/100: wk 3 p.12/50 The Infamous Dangling Else Is it if (test1){ { if (test2) A; } else B;

13 DCS/100: wk 3 p.12/50 The Infamous Dangling Else Is it or is it if (test1){ if (test1) { if (test2) { if (test2) A; A; } else else B; B; }

14 DCS/100: wk 3 p.12/50 The Infamous Dangling Else Is it or is it if (test1){ if (test1) { if (test2) { if (test2) A; A; } else else B; B; } There isn t a right answer. Some languages pick one and some the other. It doesn t matter which Java picks, Your code will be confusing if you depend on it.

15 DCS/100: wk 3 p.12/50 The Infamous Dangling Else Is it or is it if (test1){ if (test1) { if (test2) { if (test2) A; A; } else else B; B; } There isn t a right answer. Some languages pick one and some the other. It doesn t matter which Java picks, Your code will be confusing if you depend on it.

16 DCS/100: wk 3 p.13/50 Booleans The test you put in an if statement has to be something like: ans == y X <= 34 X*Y > 0 mystring.equals("dog") It is a yes/no question. It has to evaluate to either true or false. It has to be a Boolean expression.

17 DCS/100: wk 3 p.14/50 The Type: boolean We have a type: boolean for true or false things! It has two elements: true and false It is just like the other types (int, char, double, String). There are functions that return boolean. You can have boolean variables.

18 DCS/100: wk 3 p.15/50 Example boolean t = x==2; if (t) A; else B; is equivalent to if (x==2) A; else B; (This trick can make code with complicated tests much simpler to understand).

19 DCS/100: wk 3 p.16/50 Constructing booleans standard comparison operators: equal: ==, not equal:!=, less than: <, greater than: >, less than or equal: <=, greater than or equal: >= (cf. Brinch Hansen p35)

20 DCS/100: wk 3 p.17/50 Beware: = and == USE == (comparison) DON T USE = (assignment) Read = as gets the value of NOT equals.

21 DCS/100: wk 3 p.18/50 Constructing booleans combinations through boolean operators (called connectives): and: & or && or: or not:! (cf. Brinch Hansen p37 and Logic and Proof course) Examples: ans== y ans == Y X>0 & Y>0

22 DCS/100: wk 3 p.19/50 Equals for strings Java has two types of objects, simple and complicated (they re not technically called that). == and its ilk only work for simple objects. For complicated objects they only tell you whether the objects are represented internally in Java by the same bit of memory. Strings are complicated. This means: USE DON T USE mystring.equals( Hello World! ) mystring == Hello World!

23 DCS/100: wk 3 p.20/50 Blocks Java (and many other languages) have two sorts of statements: Simple statements: have to be ended by a semi-colon x = x+1; ans = in.readline(); Compound statements: (or blocks) sequences of statements enclosed in braces. { A; B; C; D;} A block is a statement, and can be used anywhere a statement is expected. NOTE: variables can only be used inside the blocks in which they are declared.

24 DCS/100: wk 3 p.21/50 Tracing If statements Trace using a series of boxes showing the values of variables, the following code for values input of 3, 10 and 11. You may also wish to use extra question boxes for boolean expressions. int price = 0; int weight = in.readint(); if (weight <= 5) price = 2; else if (weight <= 10) price = 4; else price = 6; out.writeln("the cost is " + price);

25 DCS/100: wk 3 p.22/50 ParcelForce 9am ParcelForce use a table to determine the cost of parcels. Prices are computed (for 9am deliveries) as follows: 10kg or less: more than 10kg: plus 1.15 for each additional kg (or part kg) more than 30kg: not carried (The other times are all similar)

26 DCS/100: wk 3 p.23/50 ParcelForce 9am, cont Start by simplifying the problem forget about pounds and pence for the moment, just work in pence. The price structure is now: 10kg or less: 3035p more than 10kg: more than 30kg: 3035p plus 115p for each additional kg not carried

27 DCS/100: wk 3 p.24/50 Exercise Assume the weight of a new parcel in kg is in a variable weight. Write an if statement that puts the price into a variable price.

28 DCS/100: wk 3 p.25/50 ParcelForce 9am cont. So your problems are: how do I get the program to take a weight in grammes, and round it up to the nearest kilo? (week3 ex8) how do I get the program to print out a price in pence as a price in pounds and pence? (week3 ex7) By putting these three exercises together you get a program that works for the ParcelForce 9am service.

29 DCS/100: wk 3 p.26/50 Ex 8: if (cm%100 == 0) metres = cm/100; else metres = cm/ ; (You have to convert this so it deals with grammes and kilos) Ex 7: pounds = cost/100; pence = cost%100; if (pence < 10) out.write("0"+pence); else out.write(pence);

30 DCS/100: wk 3 p.27/50 Boolean variables For example: boolean leap_year = (year%4==0 & year%100!=0) year%400==0; if (leap_year)... else...

31 DCS/100: wk 3 p.28/50 Systematic testing Read Brinch Hansen ch3 pp You re writing programs whose behaviour is more complicated now. Systematic testing is very important. In industry more effort goes into testing code than writing it. It is impossible to test programs exhaustively (every possible input), so representative cases have to be picked.

32 DCS/100: wk 3 p.29/50 Systematic testing The idea is to test each possible program behaviour, not each possible input. For complex program behaviours there are still lots of cases, and the testing has to be done on an automated test-bed. Complex programs have to be broken down into bits, and each bit tested separately. Microsoft employs more people to test code, than to write it. You have to take testing seriously too.

33 DCS/100: wk 3 p.30/50 Programming style and layout The point of having a high-level language is that it can be read and understood by people. This point is lost if the code is written down in a way that is hard for people to understand. Most industrial coding is maintenance. This means you will have to read, understand and change code written by other people. Worse: This means you will have to read, understand and change code written by yourself! You will need all the help you can get!

34 DCS/100: wk 3 p.31/50 Programming style and layout, cont There are some simple rules you can follow to make your program more easily readable. Brinch Hansen pp58-61 Jon Rowson s guidelines available on the web from the Course materials page

35 DCS/100: wk 3 p.32/50 Programming style and layout, cont These boil down to: choose informative names for variables (and later other things ) use layout to make the grammatical structure of the program clear

36 DCS/100: wk 3 p.33/50 The switch statement If you have a lot of cases to check it can look ugly to have a lengthy if statement (and can be slightly inefficient): Example: numbers for months int month; String name ="";... if (month == 1) name ="January"; else if (month == 2) name ="February"; else if (month == 3) name ="March"; else if (month == 4) name ="April";... else if (month == 11) name ="November"; else if (month == 12) name ="December"; else name ="not a month";

37 DCS/100: wk 3 p.34/50 Switch Many languages provide switch or case statements for this kind of thing. In Java they look like: int month; String name ="";... switch (month){ case 1: name ="January"; break; case 2: name ="February"; break; case 3: name ="March"; break; case 4: name ="April"; break;... case 11: name ="November"; break; case 12: name ="December"; break; default: name ="not a month"; break; }

38 DCS/100: wk 3 p.35/50 Switch NOTE THE break; STATEMENTS!

39 DCS/100: wk 3 p.36/50 Switch statements The general form is: switch (<expression>) { case <value1>: <statements> break; case <value2>: <statements> break;... case <valuen>: <statements> break; default: <statements> break; }

40 DCS/100: wk 3 p.37/50 Switch statements, cont You can give the cases in any order... (at least if they all have break s) default covers anything not mentioned in the other cases. You don t have to have it, and it does not have to come last. That s just good style.

41 DCS/100: wk 3 p.38/50 switch and if You don t really need switch if you have if: switch (<expression>) { case <value1>: <statements> break; case <value2>: <statements> break; default: <statements> break; } is the same as if (<expression> == <value1>) {<statements>} else if (<expression> == <value2>) {<statements>} else {<statements>}

42 DCS/100: wk 3 p.39/50 switch and if You don t really need if if you have switch: if (test) A; else B; is the same as switch (test) { case true: A; case false: B; } or it would be, were it not for the fact that...

43 DCS/100: wk 3 p.40/50 In Java you can only switch on int and char As it says, you can t switch on booleans: switch (test) { case true: A; case false: B; } will not compile because test is a boolean, not an int or a char.

44 DCS/100: wk 3 p.41/50 In Java you can t... In other languages you can switch on booleans, and the equivalence holds. Similarly you cannot switch on Strings (cf wk3 ex5 planets).

45 DCS/100: wk 3 p.42/50 What does break do? If you don t put in the break s, you can sometimes get very strange behaviour.

46 DCS/100: wk 3 p.43/50 What does break do? break means stop doing the switch and go on with the rest of the program. Without break you could execute several cases. But only if you change the value of the expression you re switching on during the switch! (You aren t allowed duplicate cases) (Do some Experiments... )

47 DCS/100: wk 3 p.44/50 Switch statements (Java tutorial): Following is an example,..., which illustrates why it might be useful to have case statements fall through:

48 switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numdays = 31; break; case 4: case 6: case 9: case 11: numdays = 30; break; case 2: if ( ((year % 4 == 0) &&!(year % 100 == 0)) (year % 400 == 0) ) numdays = 29; else numdays = 28; break; } DCS/100: wk 3 p.45/50

49 DCS/100: wk 3 p.46/50 Switch statements Compare this with the alternative: if the language did not fall through, but allowed multiple case and ranges instead...

50 switch (month) { case 1,3,5,7,8,10,12: numdays = 31; case 4,6,9,11: numdays = 30; case 2: if ( ((year % 4 == 0) &&!(year % 100 == 0)) (year % 400 == 0) ) numdays = 29; else numdays = 28; } DCS/100: wk 3 p.47/50

51 DCS/100: wk 3 p.48/50 Random Numbers Brinch Hansen s random number generator random gen = new random(1,365); creates a random number generator, called gen, that generates a stream of random numbers between 1 and 365. gen.readint() gives you the next number in the stream.

52 DCS/100: wk 3 p.49/50 Random Numbers, cont. The numbers aren t really random of course (they have to be generated by some deterministic algorithm, so give enough information you should be able to predict the next one). They are what s called pseudo-random, and that will be good enough for us.

53 DCS/100: wk 3 p.50/50 By the end of the week Once you have done the reading and the exercises you should be able to: Reading write programs that make decisions explain the use of boolean expressions explain what is meant by if and switch statements trace programs containing if and switch statements Brinch Hansen ch3 Computing Without Computers ch5 Jon Rowson notes on programming style

DCS/100: Procedural Programming

DCS/100: Procedural Programming DCS/100: wk 5 p.1/33 DCS/100: Procedural Programming Week 5: While Loops Queen Mary, University of London DCS/100: wk 5 p.2/33 Last week You should now be able to: write programs that follow instructions

More information

DCS/100: Procedural Programming

DCS/100: Procedural Programming DCS/100: wk 12 p.1/23 DCS/100: Procedural Programming Week 12: Classes Queen Mary, University of London DCS/100: wk 12 p.2/23 Last Week: Learning Outcomes From last week you should be able to: explain

More information

DCS/100 Procedural Programming

DCS/100 Procedural Programming DCS/100 Procedural Programming Week 9: References, the Heap and the Stack Last Week: Learning Outcomes From last week you should be able to: write programs that are split into methods write programs that

More information

QUEEN MARY, UNIVERSITY OF LONDON

QUEEN MARY, UNIVERSITY OF LONDON QUEEN MARY, UNIVERSITY OF LONDON BSC IN THE FACULTY OF ENGINEERING AND MATHEMATICAL SCIENCES Examination No.: UserId. MODULE: DCS100: PROCEDURAL PROGRAMMING 2004 MOCK TEST 2 Time allowed: 2 hours (120

More information

CONDITIONAL EXECUTION: PART 2

CONDITIONAL EXECUTION: PART 2 CONDITIONAL EXECUTION: PART 2 yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Review: The if-else statement The switch statement A look

More information

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA DECISION STRUCTURES: USING IF STATEMENTS IN JAVA S o far all the programs we have created run straight through from start to finish, without making any decisions along the way. Many times, however, you

More information

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider: CS61B Summer 2006 Instructor: Erin Korber Lecture 5, 3 July Reading for tomorrow: Chs. 7 and 8 1 Comparing Objects Every class has an equals method, whether you write one or not. If you don t, it will

More information

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

Introduction to Software Development (ISD) Week 3

Introduction to Software Development (ISD) Week 3 Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process

More information

SELECTION. (Chapter 2)

SELECTION. (Chapter 2) SELECTION (Chapter 2) Selection Very often you will want your programs to make choices among different groups of instructions For example, a program processing requests for airline tickets could have the

More information

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Writing a Simple Java Program Intro to Variables Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d. Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

The compiler is spewing error messages.

The compiler is spewing error messages. Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

More information

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

In this lab, you will learn more about selection statements. You will get familiar to 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

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

Java Programming: from the Beginning. Chapter 8 More Control Structures. CSc 2310: Principle of Programming g( (Java) Spring 2013

Java Programming: from the Beginning. Chapter 8 More Control Structures. CSc 2310: Principle of Programming g( (Java) Spring 2013 CSc 2310: Principle of Programming g( (Java) Spring 2013 Java Programming: from the Beginning Chapter 8 More Control Structures 1 Copyright 2000 W. W. Norton & Company. All rights reserved. 81 8.1 Exceptions

More information

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1

More information

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the

More information

CS354 gdb Tutorial Written by Chris Feilbach

CS354 gdb Tutorial Written by Chris Feilbach CS354 gdb Tutorial Written by Chris Feilbach Purpose This tutorial aims to show you the basics of using gdb to debug C programs. gdb is the GNU debugger, and is provided on systems that

More information

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday

More information

If Statements, For Loops, Functions

If Statements, For Loops, Functions Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements

More information

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller Table of Contents Introduction!... 1 Part 1: Entering Data!... 2 1.a: Typing!... 2 1.b: Editing

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal 3 MAKING CHOICES Now that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer

More information

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to: Get JAVA To compile programs you need the JDK (Java Development Kit). To RUN programs you need the JRE (Java Runtime Environment). This download will get BOTH of them, so that you will be able to both

More information

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal 3 MAKING CHOICES ow that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer program

More information

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

Introduction to Programming Style

Introduction to Programming Style Introduction to Programming Style Thaddeus Aid The IT Learning Programme The University of Oxford, UK 30 July, 2013 Abstract Programming style is the part of the program that the human reads and the compiler

More information

Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

CS112 Lecture: Making Choices

CS112 Lecture: Making Choices CS112 Lecture: Making Choices Objectives: Last revised 1/19/06 1. To review the Java if and if... statements 2. To introduce relational expressions and boolean operators 3. To discuss nested if statements

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)... Remembering numbers (and other stuff)... Let s talk about one of the most important things in any programming language. It s called a variable. Don t let the name scare you. What it does is really simple.

More information

Week - 01 Lecture - 04 Downloading and installing Python

Week - 01 Lecture - 04 Downloading and installing Python Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and

More information

6.096 Introduction to C++

6.096 Introduction to C++ MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 6.096 Lecture 3 Notes

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

Lecture 19 CSE August You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii.

Lecture 19 CSE August You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii. Lecture 19 CSE 110 5 August 1992 You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii. 1 Left-Over Language Features Today was the day we saw the last

More information

CS 177 Recitation. Week 1 Intro to Java

CS 177 Recitation. Week 1 Intro to Java CS 177 Recitation Week 1 Intro to Java Questions? Computers Computers can do really complex stuff. How? By manipulating data according to lists of instructions. Fundamentally, this is all that a computer

More information

Lecture 10: for, do, and switch

Lecture 10: for, do, and switch Lecture 10: for, do, and switch Jiajia Liu Recall the while Loop The while loop has the general form while ( boolean condition ) { The while loop is like a repeated if statement. It will repeat the statements

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Chapter 6. More Complex Conditionals. 6.1 Nested Conditionals

Chapter 6. More Complex Conditionals. 6.1 Nested Conditionals 56 Chapter 6 More Complex Conditionals Despite the initial simplicity of if/else statements as introduced in Chapter 5, things can get interesting when if/else statements are composed of other if/else

More information

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017 CS 426 Fall 2017 1 Machine Problem 1 Machine Problem 1 CS 426 Compiler Construction Fall Semester 2017 Handed Out: September 6, 2017. Due: September 21, 2017, 5:00 p.m. The machine problems for this semester

More information

COMP : Practical 8 ActionScript II: The If statement and Variables

COMP : Practical 8 ActionScript II: The If statement and Variables COMP126-2006: Practical 8 ActionScript II: The If statement and Variables The goal of this practical is to introduce the ActionScript if statement and variables. If statements allow us to write scripts

More information

c122mar413.notebook March 06, 2013

c122mar413.notebook March 06, 2013 These are the programs I am going to cover today. 1 2 Javascript is embedded in HTML. The document.write() will write the literal Hello World! to the web page document. Then the alert() puts out a pop

More information

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #3

More information

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

Lecture 1: Overview

Lecture 1: Overview 15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

C Pointers 2013 Author Riko H i

C Pointers 2013 Author Riko H i http:/cdorm.net/understanding C Pointers 2013 Author Riko H i Copyright 2013 CDorm.net All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form

More information

QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

More information

Key questions to ask before commissioning any web designer to build your website.

Key questions to ask before commissioning any web designer to build your website. Key questions to ask before commissioning any web designer to build your website. KEY QUESTIONS TO ASK Before commissioning a web designer to build your website. As both an entrepreneur and business owner,

More information

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++ Comp Sci 1570 Introduction to C++ Outline 1 Outline 1 Outline 1 switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2;

More information

Racket Style Guide Fall 2017

Racket Style Guide Fall 2017 CS17 Integrated Introduction to Computer Science Hughes Racket Style Guide Fall 2017 Contents 1 Introduction 1 2 Naming 1 3 Formatting 1 4 Equality 3 5 Conditionals 4 5.1 Prefer Cond to If......................................

More information

Notes on Non-Chronologic Backtracking, Implication Graphs, and Learning

Notes on Non-Chronologic Backtracking, Implication Graphs, and Learning Notes on Non-Chronologic Backtracking, Implication Graphs, and Learning Alan J. Hu for CpSc 5 Univ. of British Columbia 00 February 9 These are supplementary notes on these aspects of a modern DPLL-style

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Errors (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

CS121: Computer Programming I

CS121: Computer Programming I CS121: Computer Programming I A) Practice with Java Control Structures B) Methods Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours

More information

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements CS121: Computer Programming I A) Practice with Java Control Structures B) Methods Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours

More information

How To Get Your Word Document. Ready For Your Editor

How To Get Your Word Document. Ready For Your Editor How To Get Your Word Document Ready For Your Editor When your document is ready to send to your editor you ll want to have it set out to look as professional as possible. This isn t just to make it look

More information

Ex: If you use a program to record sales, you will want to remember data:

Ex: If you use a program to record sales, you will want to remember data: Data Variables Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/19 for T$1.00. Customer Name:

More information

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming

More information

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace C++ Style Guide 1.0 General The purpose of the style guide is not to restrict your programming, but rather to establish a consistent format for your programs. This will help you debug and maintain your

More information

Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

More information

Part 2: The Material PART 2

Part 2: The Material PART 2 PART 2 With the introduction of what an object is, now we are ready to learn the CONSTRUCTOR concept. Just to refresh our memory, let s take a look at what we have learned in part 1. A sample class declaration,

More information

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals CS125 : Introduction to Computer Science Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals c 2005, 2004, 2003, 2002, 2001, 2000 Jason Zych 1 Lecture 6 : Compound Statements, Scope,

More information

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2012 Lab 3 Matrix Math Introduction Reading In this lab you will write a

More information

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014 Lesson 6A Loops By John B. Owen All rights reserved 2011, revised 2014 Topic List Objectives Loop structure 4 parts Three loop styles Example of a while loop Example of a do while loop Comparison while

More information

Understanding Recursion

Understanding Recursion Understanding Recursion Brian L. Stuart February 23, 2015 It has been suggested that the single most original contribution that the field of Computer Science has made to the tapestry of human intellect

More information

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent Programming 2 Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent information Input can receive information

More information

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

(Refer Slide Time 3:31)

(Refer Slide Time 3:31) Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 5 Logic Simplification In the last lecture we talked about logic functions

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

CS Programming I: Arrays

CS Programming I: Arrays CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics

More information

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2 Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2 Ziad Matni Dept. of Computer Science, UCSB Administrative This class is currently FULL and

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs What are we going to cover today? Lecture 5 Writing and Testing Programs Writing larger programs Commenting Design Testing Writing larger programs As programs become larger and more complex, it becomes

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

Starting to Program in C++ (Basics & I/O)

Starting to Program in C++ (Basics & I/O) Copyright by Bruce A. Draper. 2017, All Rights Reserved. Starting to Program in C++ (Basics & I/O) On Tuesday of this week, we started learning C++ by example. We gave you both the Complex class code and

More information

CS Summer 2013

CS Summer 2013 CS 1110 - Summer 2013 intro to programming -- how to think like a robot :) we use the Python* language (www.python.org) programming environments (many choices): Eclipse (free from www.eclipse.org), or

More information

Introduction to L A TEX for MCS-236

Introduction to L A TEX for MCS-236 Introduction to L A TEX for MCS-236 Max Hailperin, based on a version by Tom LoFaro September 14, 2011 1 Why L A TEX? L A TEX is a very strange document formatting system. Actually, it is a combination

More information

Controls Structure for Repetition

Controls Structure for Repetition Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power

More information

Chapter 6: The C Preprocessor

Chapter 6: The C Preprocessor C: Chapter6 Page 1 of 5 C Tutorial.......... The C preprocessor Chapter 6: The C Preprocessor AIDS TO CLEAR PROGRAMMING The preprocessor is a program that is executed just prior to the execution of the

More information

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Announcements. CS18000: Problem Solving And Object-Oriented Programming Announcements Exam 1 Monday, February 28 Wetherill 200, 4:30pm-5:20pm Coverage: Through Week 6 Project 2 is a good study mechanism Final Exam Tuesday, May 3, 3:20pm-5:20pm, PHYS 112 If you have three or

More information

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

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan Case Control Structure DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) Decision control

More information