1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Selection Structures (if & switch statements) (CS1123)
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CONTROLLING PROGRAM FLOW
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
1 CS161 Introduction to Computer Science Topic #8.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Branching statements.
Chapter 3 Selection Statements
Chapter 3 Control Statements
Section 3 Review Mr. Crone.
Summary Two basic concepts: variables and assignments Basic types:
Let’s all Repeat Together
Life is Full of Alternatives
Arithmetic Operations
Life is Full of Alternatives
Life is Full of Alternatives
(Dreaded) Quiz 2 Next Monday.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194

2 9/25/06CS150 Introduction to Computer Science 1 Exam Review 1. unsigned int bigNumber = ; char singleChar = ‘A’; 3. value = value / (5 % 3 ); value -= 4 + z; // value = String is not a keyword: This means it is not part of the C++ language. In order to use the string data type, the string library must be included using #include

3 9/25/06CS150 Introduction to Computer Science 1 Exam Review 5. bool bLessThan100; cout << “Please enter... : “<<; cin >> value; // be careful of value == 100 bLessThan100 = ( value < 100 ); if( bLessThan100 ) { cout << “Value is < 100\n”; } else { cout << “Value is not < 100\n”; }

4 9/25/06CS150 Introduction to Computer Science 1 Exam Review 6. z = static_cast (x) / y; 7. #include “stdafx.h” #include using namespace std; int main() { int radius = 16; const double PI = ; area = PI * radius * radius; cout << setprecision(2) << fixed; cout << “The radius is “ << radius; cout << “ the area is “ << area << endl; return 0; }

5 9/25/06CS150 Introduction to Computer Science 1 Logical Operators  Logical operators allow us to join expressions in a conditional expression LogicalOp expression  How many logical operators can you name?  Where do you think these fit into the precedence hierarchy with respect to other operators?

6 9/25/06CS150 Introduction to Computer Science 1 Precedence (page 1125) Precedence Operators (Highest to Lowest) - (unary negation) * / % - + > < == != = += -= *= /= %= Relational Operators Assignment Operators Arithmetic Operators

7 9/25/06CS150 Introduction to Computer Science 1 Logical Operators  How are these used? int x = 99, y = -9; if ( expression LogicalOp expression ) { // statements } Practice: Set y = 42 if x is greater than 100 or y is less than 0; otherwise set x = 42.

8 9/25/06CS150 Introduction to Computer Science 1 exit()  To terminate a program we can use the exit(int status) function o This is a function, not part of the language  #include o The status is returned to the operating system to denote program success or failure  Success: 0  Failure: non-zero

9 9/25/06CS150 Introduction to Computer Science 1 Practice  Write a program that will ask the user for two integers. Display both integers to the screen if they are each greater than 1000 and terminate the program with exit() otherwise. Use exactly one if/else #include using namespace std; int main() {

10 9/25/06CS150 Introduction to Computer Science 1 Practice  Are these two code snippets equivalent? int x, y; if ( x > y ) { x += y; } if ( y < x) { y += x; } int x, y; if ( x > y ) { x += y; } else { y += x; }

11 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs if ( x > y ) { } else { if ( x == 9 ) { } else { }  The second if is only executed if the first if conditional is false  Note the indentation of the inner if  There may be code between the { with the first else and the second if

12 9/25/06CS150 Introduction to Computer Science 1 Using nested ifs …  Write a snippet of code that will: o add y to x if x > y o add x to y if y > x o add 1 to x if x == y int x, y; if (

13 9/25/06CS150 Introduction to Computer Science 1 C++ Shortcut if ( x > y ) { // do A } else { if ( x == 9 ) { // do B } else { // do C } if ( x > y ) { // do A } else if ( x == 9 ) { // do B } else { // do C }

14 9/25/06CS150 Introduction to Computer Science 1 Using nested ifs …  Write a snippet of code that will: o add y to x if x > y o add x to y if y > x o add 1 to x if x == y int x, y; if (

15 9/25/06CS150 Introduction to Computer Science 1 Chained If/Else statements if ( x > y ) { // do A } else if ( x == 9 ) { // do B } else if ( y > 1 ) { // do C } else { }

16 9/25/06CS150 Introduction to Computer Science 1 Using nested ifs …  Write a snippet of code that will do the following: o add y to x if x == y o add x to y if y > x o add 1 to x if (2 * y) == x int x, y; if (

17 9/25/06CS150 Introduction to Computer Science 1 Exercise  Write a snippet of code that will print the letter grade (A,B,C,D,F) of a student’s exam and set a bool flag to track if it is a passing grade. int examScore; // from 0 to 100 bool bPassingGrade; if (