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.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
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/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Announcements HW1 grades are announced at SUCourse You may see Belal Amro at his office hour for homework grades at FENS 2014 on Wednesday 10:40-12:30.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Flow of Control Part 1: Selection
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
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 05 (Part III) Control Statements: Part II.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
Chapter Making Decisions 4. Relational Operators 4.1.
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,
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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):
A First Book of C++ Chapter 4 Selection.
APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 3 Selection Statements
Data Types and Expressions
Chapter 5: Control Structures II (Repetition)
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Announcements General rules about homeworks
Chapter 4: Making Decisions.
Bools & Ifs.
CS 1430: Programming in C++ No time to cover HiC.
3 Control Statements:.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
2.6 The if/else Selection Structure
Announcements Homework 1 will be assigned this week,
Presentation transcript:

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 Quiz on Thursday (material you want to go over? tomorrow’s class) Homework - will stay late after class tomorrow.

Basic Control Structures Control order of execution of statements sequential selection iteration

Sequential Statement 1 Statement 2 Statement 3

Selection Statement 1 Statement 2 Statement 3

iteration statements (sequential, selective, or iterative

boolean Data Type if some condition is true, do this group of statements, otherwise do this other group of statements (selection) keep doing this group of statements until some condition is true (iteration) boolean Data Type holds the condition value true or false ANSI standard bool, true and false

Boolean Data Expressions constant (true or false ) variable bool notDone = true; relational (5 >= 3) logical (a || b)

Single-selection if statement if (condition) statement1 if (input < 0) cout << “Invalid input” << endl; (Note: indentation)

Double selection if if (condition) statement1 // Note: indentation else statement2; int numerator = 0, denominator = 0, quotient = 0; cout << “enter two integers: “; cin >> numerator >> denominator; if (0 == denominator) cout << “Division by zero is not allowed! “ << endl; else quotient = numerator/ denominator;

if (denominator == 0) cout << “Division by zero is not allowed! “ << endl; else quotient = numerator/ denominator; OR we could have written: if (denominator != 0) quotient = numerator/ denominator; else cout << “Division by zero is not allowed! “ << endl; OR we could have written: if (denominator) //boolean implemented as integer >0 is true quotient = numerator/ denominator; else cout << “Division by zero is not allowed! “ << endl;

Multi-selection if statements int score; cout << “Enter score: “; cin >> score; cout << “Your grade is “; if (score < 68) cout << “NR” << endl; else if (score < 78) cout << “C” << endl; else if (score < 88) cout << “B” << endl else cout << “A” << endl;

Compound Statements if (condition) { statement1; statement2; } else { statement3; statement4; } Documentation standard

Using braces What happens in the following code if we leave out braces around else statements? bool bFlag = false; if (bFlag){ cout << "condition is true " << endl; cout << "TRUE" << endl; } else cout << "condition is false " << endl; cout << "FALSE" << endl; Can use { } for single statement too.

Common programming errors forgetting one or both braces putting a ; after the condition in a single- selection if statement. if (bFlag); cout << "condition is true " << endl; confusing = with == if (denominator == 0) cout << “Division by zero! “ << endl; else quotient = numerator/ denominator; dangling else

Dangling Else if (grade > 68) if (grade >= 88) cout << “ You get an A” << endl; else cout << “You failed” << endl; what does programmer expect? grade = 75 what will really happen? fix with braces

1) What is the error in the following statement? Fix it. if (age >= 65); cout << “Age is greater than or equal to 65” << endl; 2) What is the error in the following statement? Fix it. if (count > 10 ) cout << “buffer is full” << endl; else; cout << “buffer is not full”; 3) What is displayed on the screen int x = 0; if (x = 0) cout << “x is equal to 0” << endl; else cout << “x is not equal to 0” << endl; 4) Create an if statement that checks a character variable and determines if it is an uppercase or lowercase letter a and displays “Found letter a!” if it is.

DeMorgan’s Laws Used to simplify logical expressions ! (condition1 && condition2) is logically equivalent to the expression (! condition1 || !condition2). Also, !(condition1 || condition2) is equivalent to the expression (!condition1 && ! condition2)

Group Exercise Use DeMorgan’s Laws to write equivalent expressions for each of the following: a) !(x = 7) b) ! (a == b) || ! (g != 5) c) !((x 4)) d) !((i>4) || (j <= 6))