1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer 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 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.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Perform Calculations and Control Flow Telerik Software Academy Learning & Development Team.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
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 01b: C++ review Topics: if's and switch's while and for loops.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Decision Statements, Short- Circuit Evaluation, Errors.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
COMP Loop Statements Yi Hong May 21, 2015.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 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):
Lecture 3 Selection Statements
Branching statements.
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
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.
LESSON 4 Decision Control Structure
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Compound Assignment Operators in C++
Control Structures: Selection Statement
3 Control Statements:.
Chapter#3 Structured Program Development in C++
Summary Two basic concepts: variables and assignments Basic types:
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Control Statements Paritosh Srivastava.
CS 101 First Exam Review.
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

1 CS 101 Fall 2001 Lecture 3

2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y, x>=y, x 4); would assign 0 to x.

3 if(.03) cout <<3; will print 3 to the screen since.03 is not 0 and therefore true. If x = 3, y = 5; an assignment statement x = y; will have a value of 5 and so if we have if(x=y) cout << 1, this will print 1 since 5 is true. In math, we can write x<y<z and this will mean x<y and y<z, but in C++, if x=3, y=2, z=3 then if(x<y<z)cout<<1 will print 1 to the screen since x<y<z is treated as (x<y)<z, which is 0<z since x<y is false and therefore equal 0, and 0<z is true. We have to write if(x<y&&y<z) to get the effect we want. Parentheses are not necessary since < is stronger than &&. Thus writing x=y when we mean x==y is not a syntax, but a logical error, and similarly with x<y<z.

4 Ambiguity of nested if elses Suppose we have x=3, y=4, z=5 and if(x>y) if(y y) {if(y y) { if(y<z) cout<<y;} else cout<<z; cout<<x;

5 Sequential is, else if, else if,... Suppose that we want to change a number score to a letter grade and that th integer variable x holds the numerical score. Then if(x>=90)cout =80)cout’B’; else if(x>=70)cout =60)cout =90)cout =80)cout’B’; else{ if(x>=70)cout =60)cout<<‘D’; else cout<<‘F’;}}} but we can avoid writing the enclosing braces. Thus this counts as one statement.

6 if, else if, else if, … program Here is a program which uses a sequence of if, else if, … #include main( ){ cout >x;cout =90)cout =85)cout =80)cout =75)cout =70)cout =60)cout<<‘D’; else cout<<“F’; cout<<“.\n”; } You should copy, compile and run this to see that it works.

7 The switch statement If x is an integer variable holding a numerical score as in the previous two slides, and / is integer division, then we may write switch(x/5) { case (20): case(19):case 18: cout<<‘A’; break; case(17): cout<<“B+”;break; case(16): cout<<‘B’;break; case(15): cout<<“C+”;break; case(14): cout<<‘C’;break; case(12): cout<<‘D’;break; default: cout<<‘F’; The reason for writing break statements is that the switch statement exhibits what is called drop through behavior, i.e., if the break statements weren’t there and x was 15, C+ would print out, but also, CDF would also print out.

8 switch program The program in slide 13 can also be written as #include main( ){ cout >x;cout<<“You made an “; switch(x/5){ case 20: case 19:case 18:cout<<‘A’;break; case 17:cout<<“B+”; break; case 16:cout<<‘B’; break; case 15:cout<<“C+”; break; case 14:cout<<‘C’; break; case 12:cout<<‘D’; break; default: cout<<“F’; cout<<“.\n”; } You should copy, compile and run this to see that it works.