True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
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.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
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.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
Selection. Flow Chart If selection If/else selection Compound statement Switch.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
 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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Branching statements.
Introduction to Computer Programming
Chapter 3 Selection Statements
REPETITION CONTROL STRUCTURE
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 5: Control Structures II
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements.
Iteration with While You can say that again.
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Presentation transcript:

True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and), || (or) and ! (not) Also can create logical expressions with relational operators: >= greater than or equal to; != not equal or inequality = = equal or equivalent Boolean type: bool Example in puTTy (boolean.cpp)

Exercises 1.bool f1=false; bool f2=true; bool f3=false; What is the value of the following expression? (!(!f3&&f1))||f2 2. int i= 3; int j=10; int k=20; What is the value of the following expression (j!=i) && (i =20)

Simple “if” Statement if (boolean expression) statement The compiler will evaluate the expression, and try to figure out if it is true or false If true, the statement after the ( ) is executed If not, the statement is skipped, and execution continues after this statement expr stmt true false

Compound statement Often, however, we want to do a series of actions in response to the conditional expression In this case, we use a compound statement if (boolean expression) { statements } Two styles if ( z == 0) { x = 0; y = 0; } if ( z == 0) { x = 0; y = 0; }

If-else statement if ( expression ) statement1 else statement2 z==0 x = 0; y = 0; x = 1; y = 1; true false if (z == 0) { x = 0; y = 0; } else { x = 1; y = 1; }

Exercise Take actions while reading the following segment of pseudo code: Exercise 1: if (your age%2==0) stand on your right foot; else stand on your left foot; hop three times; sit down and rest Exercise 2: if (your age <=50) if (your age%2==0) stand up else site and watch (question to ponder: who will site and watch? The people who age over 50? Or the people whose age under 50 and age is an odd number?) use appropriate indention and { }, and it will make the association explicit

Example #include using namespace std; int main () { int value1=0, value2=0; cout << “Please enter two numbers ? “; cin >> value1 >> value2; if (value1 < 0) value1 = -value1; if (value2 < 0) value2 = -value2; ; if (value1 > value2) { cout << “__________ “ << value1 << endl; } else { cout << “__________“ << value2 << endl; } Return 0;}

Nested If Statement Any statement can be part of the statement portion of the “if” Thus, “if” statements can be nested. if you use nested if-else statements, the else is associated with the closest if (unless in different compound statements) Example: finding the smallest of 3 numbers int value1=0, value2=0, value3=0; int smallest = 0; cout << “Please enter 3 integers ? “; cin >> value1 >> value2 >> value3; if (value1 < value2) { if (value1 < value3) smallest = value1; else smallest = value3; } else { can you fill in the code in this session? }

if..else if..else if..else if (expression) action else if (expression) action else if (expression) action else action The multi version appears often enough that it has it's own special form: The switch statement Example in puTTy (selection.cpp)

In class exercises 1. Write the boolean expression of: 1) a ≠b or a ≤ b 2) 20<x<30 2. Write a segment of code to test if integer x is multiple of 3 and 5 or not. If it is, output “yes”, if not, output “no”. 3. What will the following code do? char c; cin>>c; if ( c>=‘a’&& c<=‘z’) c=c-32; if (c>=‘A’ && c<=‘Z’) c=c+32;

while Loop (iteration) Repeated Jobs: – Your teacher told you: “write ‘I am sorry’ on the blackboard 100 times!” Like if-statements, loops are controlled by a Boolean condition. The code inside the loop will continue to execute over and over until the exit condition is satisfied. – May mean that the loop body executes zero times if the exit condition is satisfied when the loop first is executed – You may get an infinite loop if your program never satisfies the exit condition

“while” Loops Body The body of the loop just continues to iterate while the iterateCondition is satisfied. When it fails, execution continues at the statement immediately following the while loop x>0 true false Body

While Loops Example #include using namespace std; int main () { string inString = "junk"; int count = 0; cout << "Enter several lines "; cin >> inString; while ( inString != "junk") { count++; cin >> inString; } cout << "You entered " << count << " words before 'junk'" <<endl; }

Do-While Loop Very similar to a While loop, with one exception The exit condition is placed at the end of the loop, and not evaluated until after the loop body has been executed Thus, the loop body always executes at least once Format: do Statement while ( conditionExpr); x>0 true false Body While Loop Do-While Loop Body x>0 true false

Do-While Loop Example #include using namespace std; int main() { char ans; do{ cout<<“hello\n”; cout<<“Do you want another greatings?\n”; <<“Press y for yes and n for no\n”; << “and then press return:”; cin>>ans; } while (ans==‘y’ || ans ==‘Y’); cout<<“good bye\n”; return 0; }

ITEM Mnemonic i = 1; -- Initialize the loop control variable (lcv) while (i != 10) { -- Test the lcv -- do something -- Execute the action (the reason for the loop to exist) i++; -- Modify the lcv }