Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.

Slides:



Advertisements
Similar presentations
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical.
1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
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.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
1 SELECTION using IF and IF-ELSE Chapter 4. 2 Agenda Background  One Way selection (if) Two Way selection (if-else) Compound Statements Nested if-else.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
1 CS161 Introduction to Computer Science Topic #8.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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,
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 3 Selection Statements
Review 1.
Data Types and Expressions
Selection (also known as Branching) Jumail Bin Taliba by
Bill Tucker Austin Community College COSC 1315
For & do/while Loops.
Counting Loops.
Chapter 7 Selection Computing Fundamentals with C++ 3rd Edition
Flow Control Statements
Summary Two basic concepts: variables and assignments Basic types:
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Repetition Statements (Loops) - 2
CS 101 First Exam Review.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CS31 Discussion 1D Fall18: week 2
Presentation transcript:

Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.

Class on Saturday (Tomorrow)

§How do we define a block in C++?

§A block in C++ is created by placing a sequence of statements between curly braces.

Good programming practices §Meaningful Variable names §Code indentation §Comments There will be marks for style in homeworks/assignments.

Terminology § Syntax errors l reported by the compiler §Linker errors l reported by the linker §Execution/Run-time errors l reported by the operating system §Logic errors l not reported

Relational Operators > >= < <= = != Logical Operators && | !

Implementing the Branch §if-else statement is used in C++ to perform a branch if (hours > 40) { gross_pay = rate * * rate * (hours-40); } else { gross_pay = rate * hours; }

Boolean expressions Logical AND is represented by && Example.

Boolean expressions  Pitfall: Do not use strings of inequalities. They may be legal C++ code but will not do what you expect them to do. int x=2, y=9, z=3; //Is y is between x and z? cout<<(x < y < z)<<endl; //WRONG!

Boolean expressions  Pitfall: Do not use strings of inequalities. They may be legal C++ code but will not do what you (for now) expect them to do. int x=2, y=9, z=3; //Is y is between x and z? cout<<(x < y < z)<<endl; //WRONG! Instead, you have to write: cout<< ((x < y) && (y < z)) <<endl;

Boolean expressions §Given x=3 and y=4. What's the value of !(( (x y-2) ) && ( x*2 < y ) )

Boolean expressions §Given x=3 and y=4. What's the value of cout y-2) ) && ( x*2 < y ) ); prints 1

Boolean expressions §What's wrong with these? (y > x) & (z > x) (z !>= y) (a < x < b) !(x = 3)

Boolean expressions I want to put some balls in a box if the number of balls in not 3 or 5. How do we write the statement in C++?

If: selective execution Example: Bank withdrawal checker int balance; //… some other code here int withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; if(withdrawal <= balance) { cout << "Here is your money" << endl; } else { cout << "Sorry, insufficient balance" <<endl; }

If: selective execution §What is wrong here? int age; cout << "Please enter your age: "; cin >> age; if (age < 0) cout << "Wow, you are really young!"; cout << "Your age is negative."; cout << endl; cout << "Your age is " << age << endl;

If: selective execution §Pitfall: Indentation by itself is not enough. int age; cout << "Please enter your age: "; cin >> age; if (age < 0) cout << "Wow, you are really young!"; cout << "Your age is negative."; cout << endl; cout << "Your age is " << age << endl;

If: selective execution §What's wrong with this? if (x > y); x = y; cout << x;

If: selective execution Pitfall: There is no semi-colon after the if-condition!

Stringing if-then-elses together  If-then-else statements can be strung together.  Exactly one of the alternatives will be executed.

Stringing if-then-elses together if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton for punishment" << endl; }

Another way of writing this? if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton for punishment" << endl; }

If: selective execution int main () { int age; cout << "Please enter your age: "; cin >> age; if(age <= 0) { cout 0\n"; return 0; } cout << "Your age is " << age << endl; return 0; }

While loop int main () { int age; cout << "Please enter your age: "; cin >> age; while(age <= 0) { cout 0\n"; cout << "Please enter your age again: "; cin >> age; } cout << "Your age is " << age << endl; return 0; }

Nested if statements  The inside the braces can contain any valid C++ statements, including if statements! // … some other code here char answer; if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance? "; cin >> answer; if (answer == 'y') cout<< "Your balance is "<<balance<< endl; } else { cout << "Here is your money." << endl; } cout << "Good bye." << endl;

Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; //bad style- no indentation (code with proper indentation on next slide)

Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; /*else statement always refers to nearest if statement that is within same block as else and not already associated with another else*/

Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else //Error cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

Nested if statements // what is output here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z){ cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; } else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0; }

§Output is 4