CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.

Slides:



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

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CS 117 Spring 2002 IDAHO TRANSPORTATION DEPARTMENT Internship Announcement POSITION TITLE: Computer Science Intern JOB DESCRIPTION: Translate technical.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Fundamentals of C and C++ Programming Control Structures and Functions.
Decision Structures and Boolean Logic
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
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
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Statements: Part1  if, if…else, switch 1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 4: Control Structures I (Selection)
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3 Control Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
Topics The if Statement The if-else Statement Comparing Strings
Chapter 8 JavaScript: Control Statements, Part 2
Relational Operators Operator Meaning < Less than > Greater than
3 Control Statements:.
CSC215 Lecture Control Flow.
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
The System.exit() Method
Control Structure Chapter 3.
Control Statements Paritosh Srivastava.
CSC215 Lecture Control Flow.
Control Structure.
Presentation transcript:

CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes

Material Covered Hanly –Chapter 3 –Chapter 4 –Chapter 5 Friedman-Koffman –Chapter 3 –Chapter 4 –Chapter 5 –Chapter 6

Boolean Expressions needed for both selection and repetition don't confuse == with = a==b has value of either true or false a=b has same value as b de Morgan's laws !(p && q) == !p || !q !(p || q) == !p && !q

Boolean Operators comparison operators <less than <=less than or equal >=greater than or equal > greater than ==equal !=not equal

Boolean operators unary !logical NOT binary operators &&logical and ||logical or short circuit evaluation

Selection Review Hanly Chapter 3 –p 84 #1-4 –p. 92 #1-2 –p. 94 # 1-3 –p 105 # 1-5 Friedman-Koffman Chapter 4 –p 186 # 1-2 –p 210 #3 –215 #1-2 –p 220 #8-12

Selection select between several alternative blocks of code two types –if else –switch

Selection selective execution of parts of the program if if (condition) statement; condition is a boolean expression statement is single statement or block of statements enclosed by {}

if..else if (condition) thenDoThis; else doThat; thisAlwaysDone; body of if and else one statement unless { } used

Multiple if statements Sometimes there are more than two cases. if (condition) thenDoThis; else if (condition2) doThat; else if (condition3) … else doInAllOtherCases; thisAlwaysDone;

format of switch switch (variable) { case value1: action1: break; case value2: action2; break; default:// if no other case holds default action; }

switch statement The first case for which variable has the value given in the case is executed break forces exit from the switch statement; without it, execution falls through to next case

switch example Suppose you want to write a simple calculator program. Your program will read in a character into the variable op and two numbers v1 and v2. You want to use a switch statement to decide what to do with v1 and v2. Provide cases for +, -, / and *, using the first case as a model.

code switch (op) { case ' % ': ans = v1 % v2; break; default: cout "error\n"; } case '+': ans = v1 + v2; break; case '-': ans = v1 - v2; break; case '*': ans = v1 * v2; break; case '/': ans = v1 / v2; break;

Repetition Review Hanly Chapter 4 –p 116 # 1-3 –P 125 # 1-5 –p 134 #1, 5 –p 140 #2 Friedman-Koffman Chapter 5 –p 232 # 2-5 –p238 # 1-2 –p 245 # 1, 2, 4-6 –p 265 #1-2 –p 268 #1-2 –p 273 #2-3 –p274 #1 –p 284 #1-5, 7-10 –p 285 # 6-7

Repetition executing same block of code multiple times three forms –while –do - while –for

Loop Review :while Most commonly used when repetition is not counter controlled condition test precedes each loop repetition loop body may not be executed at all while (condition) dothis;

Loop Review: do-while Convenient when at least one repetition of loop body must be ensured test condition after execution of body do that; while (condition);

Loop Overview Counting loop - number of repetitions –known ahead of time –can be controlled by a counter also convenient for loops involving non counting loop control with simple initialization and updates condition test precedes the execution for (initialization; condition; modification) doSomething;

Function Review Hanly Chapter 5 –p 165 # 1-4 –p 179 # 1-2 –p 185 #1-2 –p 188 # 1-2 Friedman-Koffman Chapter 3 & 6 –p 119 #1 –p 142 #1 –p 165 #10 (just write the functions) –p 305 #3 –p 313 # 1 –p 337 #1, 5

Functions know the difference between a declaration and a definition know how and when to use void understand the difference between pass by value and pass by reference reference parameters

Function Declarations also called prototype or signature can declare a function without knowing how to write the function

Parts of a declaration double someFunction( int param1, int param2); The return type of the function is double. It can be any type, either built-in or a class type. If nothing is to be returned, it should be void. The function name is someFunction. –The rules for naming functions are the same as for naming variables. The parameter list is contained within the parentheses. There –can be any number of parameters, including 0.

Calling a function You call a function by giving its name followed by a list of arguments in parentheses. double x = someFunction( arg1, arg2); The list of arguments should match the function's parameter list in number of arguments and type. arg1 and arg2 may be variables, numbers or expressions. They are evaluated and the values stores in the corresponding function parameters.

Function Definition The function definition contains the code that is executed when the function is called. double someFunction( int param1, int param2) { // code that is executed return aDouble; // required for function that returns a value } Note: param1 and param 2 are declared in the function signature; don't redeclare them or they will be hidden by the newly declared variables.

The function parameters are places to store the values that are passed in. void functions return no value - don't use them with assignment

Value and Reference Parameters by default function arguments are passed by value –they can't be changed by the function reference parameters have a & between the type and the argument name void swap( int & a, int & b); Reference parameters can be changed by the function.