1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.

Slides:



Advertisements
Similar presentations
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Advertisements

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/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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 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.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4 Selection Structures: Making Decisions.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Chapter 4 Selection: the if-else and switch-case instructions.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 CS161 Introduction to Computer Science Topic #8.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
for Repetition Structures
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS150 Introduction to Computer Science 1
JavaScript: Control Statements I
Compound Assignment Operators in C++
Unary Operators ++ and --
Summary Two basic concepts: variables and assignments Basic types:
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Life is Full of Alternatives
Understanding Conditions
Arithmetic Operations
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Life is Full of Alternatives Part 3
Presentation transcript:

1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives

2 9/26/05CS150 Introduction to Computer Science 1 Intro  Last time we o Introduced the idea of selection structures o Illustrated selection structures using if statements o Covered logical operators  Today we will o Complete talking about selection structures using multiple alternative if statements and if/else

3 9/26/05CS150 Introduction to Computer Science 1 Single Alternative if  The if selection structures we saw last time all have a single alternative if (condition) or if (condition) one statement; { next statement; multiple statements; } next statement;  If condition is true, statement(s) following if execute  if condition is false, statement(s) following if are skipped.

4 9/26/05CS150 Introduction to Computer Science 1 Multiple Statements  If you have multiple statements that need to be executed if the condition is true, they should be surrounded by curly braces { }

5 9/26/05CS150 Introduction to Computer Science 1 Examples if (x >= 0) cout << “x is positive” << endl; if (x < 0) x = -x; if ((x == 0) && (y == 0)) { x = 1; y = 1; }

6 9/26/05CS150 Introduction to Computer Science 1 Program  9.1: Write a program segment that allows the user to input two integer values into variables num1 and num2. Your program is to then exchange the values in the variables num1 and num2 only if num1 is greater than num2

7 9/26/05CS150 Introduction to Computer Science 1 if/else Selection Structure  This is the multiple alternative if  Used when different statements should execute if the condition is falseif (condition) statementT;{ else statementsT; statementF;} else { statementsF; }

8 9/26/05CS150 Introduction to Computer Science 1 UML Activity Diagram if (x >= 0) cout << “x is positive” << endl; else cout << “x is negative” << endl; output “positive” [x >= 0][x < 0] output “negative”

9 9/26/05CS150 Introduction to Computer Science 1 Examples x = 25.0; if (y != (x )) x = x ; else x = x / 2.0; if ((y = 0.0)) x = 5 * y; else x = 2 * y;

10 9/26/05CS150 Introduction to Computer Science 1 Program  9.2: Write a program that inputs an integer number and outputs if its even or odd  9.3: Write a program that computes the area of a triangle or a rectangle based on the user typing in ‘t’ or ‘r’ first

11 9/26/05CS150 Introduction to Computer Science 1 Problem  9.4: What is the output of the following program segment i = 5; j = 2; if((i % j) == 0) i = j; j = i; cout << i << j; cout << "That's all folks" << endl;

12 9/26/05CS150 Introduction to Computer Science 1 Conditional Operator ?:  C++ provides the conditional operator ?: as a shortcut way of writing simple if/else structures  For example, the structure if (x >= 0) cout << “positive” << endl; else cout << “negative” << endl;  Could be written as cout = 0 ? “positive” : “negative” ) << endl;

13 9/26/05CS150 Introduction to Computer Science 1 Conditional Operator ?:  The format of the operator is o ( condition ? true-statement : false-statement )  The conditional operator works if there is only one statement for a true evaluation and only one statement for a false evaluation

14 9/26/05CS150 Introduction to Computer Science 1 Nested if/else Selection Structures  What if there are more than two alternatives? if (condition1) statement1; else if (condition2) statement2; … else default statement;

15 9/26/05CS150 Introduction to Computer Science 1 Problem  9.5: Write a C++ program segment that allows the user the ability to input an integer from the keyboard. If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1

16 9/26/05CS150 Introduction to Computer Science 1 Solution cin >> intvalue; if(intvalue > 0) poscount = poscount + 1; else if(intvalue < 0) negcount = negcount + 1; else zerocount = zerocount + 1;  Can you come up with another way of doing this?

17 9/26/05CS150 Introduction to Computer Science 1 Solution  Will this solution work? cin >> intvalue; if(intvalue > 0) poscount = poscount + 1; if(intvalue < 0) negcount = negcount + 1; if(intvalue = 0) zerocount = zerocount + 1;

18 9/26/05CS150 Introduction to Computer Science 1 Problem  9.6: Write a program that displays a letter grade corresponding to an exam score A B C D 0-59 F

19 9/26/05CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o if/else selection structures o if structures with multiple statements { } o Nested if/else selection structures  Readings o P : if/else selection structures o P : conditional operator ?: o P : nested if/else selection structures and if structures with multiple statements