Wednesday, 10/30/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/30/02  QUESTIONS??  Today:  Finish discussion of random numbers  Logical operators.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

If Statements & Relational Operators Programming.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Overview Go over parts of quiz? Another iteration structure for loop.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Expressions Methods if else Statements Loops Potpourri.
Review Expressions and operators Iteration – while-loop – for-loop.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3 Selection Statements
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Sequence, Selection, Iteration The IF Statement
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
CMPT 201 Functions.
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.
Control Structures – Selection
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.
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
Chapter 7 Additional Control Structures
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Alternate Version of STARTING OUT WITH C++ 4th Edition
Life is Full of Alternatives
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Programming Fundamental
Presentation transcript:

Wednesday, 10/30/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/30/02  QUESTIONS??  Today:  Finish discussion of random numbers  Logical operators and bool objects  For-loops  Reading: Chap. 7 through 7.4  Exercises: p. 178 #4, 10, 11  New files/handouts: FileWordCount.cpp

Wednesday, 10/30/02, Slide #2 Random probabilities and random integers in a range  Random probabilities, i.e., random fractional values between 0 and 1  double(rand()) / RAND_MAX  Random integers in a range, i.e., random whole numbers from a low value to a high value  rand() % (hi - lo + 1) + lo  Utility file RandomUtils.h contains precoded functions to do these tasks:  void InitRand();  int RandLoHi(int lo, int hi);  double RandProb();

Wednesday, 10/30/02, Slide #3 Compound Expressions: Logical Operators  Logical operators:  AND: &&  OR: ||  NOT: !  Warning: &, | are also operators (bitwise and, or)  Examples:  if ((Ans == ‘Y’) || (Ans == ‘y’)) cout << “YES!”;  if ((x >= y) && (x >= z)) cout << “x is biggest.”;  if (!(Num % 3 == 0)) cout << “Not a multiple of 3”;  Question: Give a legal C++ expression saying 90  Score  100

Wednesday, 10/30/02, Slide #4 Operator Precedence Revisited  Precedence of operators (from highest to lowest)  Parentheses (, )  Unary operators -, ++, --, !  Multiplicative operators *, /, %  Additive operators +, -  Relational ordering, =  Relational equality ==, !=  Logical and &&  Logical or ||  Assignment =

Wednesday, 10/30/02, Slide #5 Operator Precedence Revisited  Examples 5 != 6 || 7 <= 3 (5 != 6) || (7 <= 3) 5 * == 13 && 12 < 19 || !(3 < 0)

Wednesday, 10/30/02, Slide #6 Short-circuit evaluation  SHORT-CIRCUIT EVALUATION: As with other operators, C++ will evaluate logical operators with the same precedence from left to right. But, it will stop evaluating as soon as the boolean value of the full expression is determined. Example 1: Example 2: int x = 5; float y = ; if (x > 0 || x == -1) if (y > 0 && sqrt(y) 0 || x == -1) if (y > 0 && sqrt(y) < 10) cout << “HI”; cout << “BYE”; cout << “HI”; cout << “BYE”;

Wednesday, 10/30/02, Slide #7 The type bool  The latest C++ standard includes a type bool  This type has two possible values: true or false  bool is actually an integer type: false corresponds to 0, and true to a non- zero value  Examples: bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || false; bool U = !(R && !Q);

Wednesday, 10/30/02, Slide #8 Using bool objects  Objects of type bool can be used to help readability of code and simplify expressions:  cin >> Guess; //should be 0 or 1 bool ValidData = (Guess == 0) || (Guess == 1) if (ValidData) { //Continue with program }

Wednesday, 10/30/02, Slide #9 The for-loop  The for-loop is a looping structure equivalent in capability to the while-loop, that builds into the structure:  Initialization  Loop condition  Updating for (Initialization statement; Loop condition; Update statement) { Loop body }

Wednesday, 10/30/02, Slide #10 For-loops for counting  For-loops are a very straightforward structure to use when you want simply to repeat an action a predetermined number of times: for (int i = 1; i <= 500; ++ i) {int FlipNum = RandLoHi(0,1); if (FlipNum == 0) ++HCount; else //FlipNum == 1 ++TCount; }

Wednesday, 10/30/02, Slide #11 Nesting for-loops  As with while loops, we can nest for-loops. The inner loop must do all its repetitions for each iteration of the outer loop  Example: Write a nested for-loop to draw a box of stars with numRows rows and numCols columns

Wednesday, 10/30/02, Slide #12 Other uses of for-loops  This for-loop determines if a string S is a palindrome (same forwards and backwards):  Here is a for-loop that counts all the “words” (strings separated by white space) in a file (see FileWordCount.cpp): for (int count = 0; inFile >> word; ++count); //No body needed!! bool Same = true; int N = S. size(); for (int i = 0; Same && i < N/2; ++i) {Same = (S[i] == S[N-1-i]); }