Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.

Slides:



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

Monday, Feb 10, 2003Kate Gregory with material from Deitel and Deitel Week 6 Lab 2 is marked Hand in Lab 3 Questions from Last Week Operator Overloading.
Monday, Mar 24, 2003Kate Gregory with material from Deitel and Deitel Week 11 Exceptions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Objectives You should be able to describe:
 2007 Pearson Education, Inc. All rights reserved C Program Control.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Chapter 9 Additional Control Structures Dale/Weems.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
TK 1914 : C++ Programming Control Structures I (Selection)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Control Statements: Part1  if, if…else, switch 1.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Introduction to Computer Programming
Chapter 4 – C Program Control
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
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
3 Control Statements:.
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
CSC215 Lecture Control Flow.
Control Statements:.
Presentation transcript:

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Course Schedule

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Control Structures Normally execution falls from one statement to another: i++; j++; x = y;

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Conditions if else switch Immediate if / ternary operator

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Repetitions while for do / while

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel if if ( x > 0) y = 2; z = 3; if ( x > 0) { y = 2; z = 3; }

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel If Gotchas Round brackets around the condition are never optional The condition can be true or false, or a number. All non-zero numbers are true. Only zero is false. Never put a ; after the round brackets if ( x > 0); y = 2;

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Comparison Operators Greater Than > Greater Than or Equal To >= Less Than < Less Than or Equal To <= Equal To == Not Equal To !=

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Two conditions if ( x > 0) if (y < 0) cout << "lower right"; Alternatively: if ( x > 0 && y < 0) cout << "lower right";

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Logical Operators && ( logical AND ) –Returns true if both conditions are true || ( logical OR ) –Returns true if either of its conditions are true ! ( logical NOT, logical negation ) –Reverses the truth/falsity of its condition –Unary operator, has one operand Useful as conditions in loops ExpressionResult true && falsefalse true || falsetrue !falsetrue

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Expressions in Conditions if (x+3 > y) if (sin(x) > 0) if (sin(x) > 0 && foo(y) < 0 ) –Short-circuiting

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Else if ( x > 0) y = 2; z = 3; if ( x > 0) y = 2; else z = 3;

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Switch if (x == 1) cout << “one”; else if ( x == 2) cout << “two”; else if ( x == 3) cout << “three”;

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Switch switch (x) { case 1: cout << “one”; break; case 2: cout << “two”; break; case 3: cout << “three”; break; default: cout << “not 1, 2, or 3”; }

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Switch errors Forgetting the {} Forgetting the break statements Forgetting a default case

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Immediate if Also called ternary operator x = y>0? 3 : 6; Equivalent to if (y>0) x=3; else x=6;

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel while x = 0; while (x < 10) { cout << x << ‘\n’ ; x ++; }

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel for for (x = 0; x < 10; x ++) { cout << x << ‘\n’ ; } Eliminates forgetting to increment Remember to use ; not,

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel While over for If you know how many times, use for If you don’t, use while –While not at the end of the file –While the user still wants to continue

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Do x = 11; do { cout << x << ‘\n’ ; x ++; } while (x < 10)

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Break Used for exiting a loop immediately x = 0; while (x < 10) { if ( x > 4) break; cout << x << ‘\n’ ; x ++; }

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Continue Used for skipping the remainder of the body of a loop x = 0; while (x < 10) { if ( x > 4) continue; cout << x << ‘\n’ ; x ++; }

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Continue in for for (x = 0; x < 10; x ++) { if ( x > 4) continue; cout << x << ‘\n’ ; } Continue in a while is only safe after you’ve incremented or moved to the next

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Functions and Methods Really two words for the same thing. A function that is part of a class is called a member function. OO purists sometimes call it a method. A function that is not part of a class is called a global function. –The main() function is a global function

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Global functions Gather up some code to make a single thing Perhaps because it’s used in many places Perhaps because you can understand your code better if it’s not a 10,000 line monolith Perhaps to simplify team programming Library code may be available as functions

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Declare a function int foo (int x, int y); void bar(float f); void report(void); void deleteFiles(); Also called prototyping a function Placeholder names are optional, but always use them

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Where to declare All code that calls the function must declare it first, so the compiler knows about it If the declaration is missing the compiler will complain Often a number of declarations are gathered into a header file

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Implement a function void report(void) { cout << “This is a report”; return; } For void functions you can omit the return Only have the implementation in one file If it’s missing the linker will complain Also called defining a function

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Call a function int i; i = foo ( 3, 4); OK to ignore return values foo (5,6); Compiler will check types i = foo(“Hello”, 4.5);

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Call By Value void ChangeIt (int a) { a++ ; } Some code calls it int i=3; ChangeIt(i);

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Call By Reference void ChangeIt (int& a) { a++ ; } Some code calls it int i=3; ChangeIt(i);

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Reference Advantages When passing objects, the copy can be quite expensive You might want the function to change its parameters The C way, with pointers, makes code hard to read

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Simple function example int DoubleIt(int a) { return 2 * a; } int main() { int x; cout << “Enter a number ”; cin >> x; cout << “Double that is “ << DoubleIt(x); return 0; }

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Scope Variables come into scope when they are declared They go out of scope when they hit a brace bracket: { int i = 3; } i = 7; //compiler error Generally this is an issue when you declare something inside the block of an if/for/while and then try to use it afterwards

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel For Next class Complete Lab 1 –Late penalty is TERRIBLE Read chapters 4 and 5