1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

If Statements & Relational Operators Programming.
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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Announcements Quiz 1 Posted on blackboard Handed Back (and Gone Over)Next Monday “Only a Quiz” Counts 5% of Final Grade Exam 1 Counts 10%
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
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.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
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.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter#3 Part1 Structured Program Development in C++
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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,
Control Statements: Part1  if, if…else, switch 1.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
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):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
The Ohio State University
Branching statements.
Chapter 3 Control Statements
Sequence, Selection, Iteration The IF Statement
Announcements Quiz 1 Posted on blackboard
EGR 2261 Unit 4 Control Structures I: Selection
Bools & Ifs.
Selection (if-then-else)
Bools & Ifs.
3 Control Statements:.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structures Part 1
2.6 The if/else Selection Structure
Life is Full of Alternatives
Life is Full of Alternatives
Life is Full of Alternatives
Programming Fundamental
Presentation transcript:

1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm

2 Announcements Quiz grades to be posted on Blackboard Quizzes handed back next Wednesday.

3 Selection (if-then-else) Programming has 3 types of control: Sequential (normal): Control of execution proceeds one after the other. Selection (if-then-else): Control proceeds dependent on conditions. Iteration (looping): Control repeated until condition met.

4 C++ if Statement Syntax Syntax: if (condition) statement; If condition is true, statement is executed If condition is false, statement is not executed. Statement can be a single statement or a compound statement.

5 Compound Statement To make the statement to do multiple actions, a compound statement is needed if (condition) { statement; }

Simple Conditions How do we build if conditions? Simple conditions are built using Relational Operators:, >=, <= Equality Operators: ==, != Later we'll see how to build complex conditions.

Selection Examples if (age < 30) cout << "You are very very Young" << endl; if (grade == 'A') cout << "Congratulations!" << endl; if (grade != 'F') cout << "You passed!" << endl;

8 if-else Statement The if-else statement is a 2-way decision. Syntax: if (condition) { statement(s);//condition true } else { statement(s);//condition false }

9 if-else Example if (yourGrade >= 60) { cout << "You passed!!" << endl; } else { cout << "Uh...." << endl; }

10 Compound Statements Compound statement: Sequence of statements surrounded by curly braces. Must use compound statement if more than one statement is supposed to be under control of if or else conditional. Include curly braces after if so if other statements are added in future, curly braces are already there.

Try This: Take 5 min Write a program that prompts the user to enter an integer. Store the integer in a variable If the integer entered is greater than 10, display the words “Greater than 10!” to the display If the integer entered is not greater than 10, display the words “Not greater than 10!” to the display You have 5 minutes – go!!

12 /* This program prompts for an integer and prints out whether it is greater than GREAT_NUM or not */ #include using namespace std; const int GREAT_NUM = 10; int main() { int numEntered; cout << “Enter an integer: “; cin >> numEntered; if (numEntered > GREAT_NUM) cout << "Greater than ” << GREAT_NUM << “!!”; else cout << “Not greater than ” << GREAT_NUM << “!!”; }

13 The Boolean Type The two Boolean values are true and false. In C++, Type bool. Example: bool done = false;... if (currentLetter == 'Z') done = true;...

14 Boolean Expressions if/else conditionals must evaluate to true or false, and are therefore called boolean expressions. In C++, any non-zero value is considered true; any expression evaluating to zero is considered false. But usually we use true and false

15 Logical Operators A Logical Operator is one that manipulates logical values && Is Logical “AND”: Both operands must be true for entire expression to be true. || Is Logical ‘OR”: One (or both) of the operands must be true for entire expression to be true.

16 Operator Precedence () -, ! (not)(Unary [1-operand] operators) *, /, %(Multiplicative operators) +, -(Addition operators), >=(Relational operators) ==, !=(Equality operators) &&(Logical AND) ||(Logical OR) =(Assignment)