Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
True or false A variable of type char can hold the value 301. ( F )
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.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Copyright 2003 Scott/Jones Publishing Making Decisions.
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.
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
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.
Decision Statements, Short- Circuit Evaluation, Errors.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
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.
Branching statements.
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
Selections Java.
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 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Control Structures – Selection
Other Conditional Methods
Flow Control Statements
Structured Program Development in C++
Presentation transcript:

Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing the list of statements in curly braces ({ }). The statements in side of the braces will be executed sequentially, and the block counts as one statement.

Compound Statement Example if (a > b) { c = a; cout << a << endl; } else { c = b; cout << b << endl; }

IF statement If there is nothing to do if the test is fals e, then the else part of the statement can be omitted, leaving just an if statement: if (n > 0) n = n + 1;

Nested If statements If statements can be nested, that is, the statement to be done when the test is true (or false) can also be an If statement. Consider printing out three numbers in order:

An Extended Example if(a > b) if(b > c) cout << a << “ “ << b << “ “ << c << endl; else // b <= c if(a > c) cout << a << “ “ << c << “ “ << b << endl; else // a <= c cout << c << “ “ << a << “ “ << b << endl; else // a <= b if(a > c) cout << b << “ “ < a << “ “ << c << endl else // a <= c...

Multiway If-else Statement When the statement that is done when the test is false is also an If statement, it is customary to write the else and the if on one line: if(b > c) cout << a << “ “ << b << “ “ << c << endl; else if(a > c) is written if (b > c) cout << a << “ “ << b << “ “ << c << endl; else if (a > c)

More on Multi-way If Statements This is common when there are a number of possibilities based on the value of a variable: if (bedSoftness > 10) cout << “Too hard!” << endl; else if(bedSoftness > 5) cout << “Just right!” << endl; else cout << “Too soft!” <<< endl;

Dangling Else Problem Consider the following code: if (a > b) if (b > c) cout << c << endl; else cout << b << endl; Does the else go with if (a > b) or with if (b > c) ?

Answer It goes with the last if statement that is without an else, that, is if (b > c).

The Switch Statement If there are many cases the depend on the exact value of an int or char variable, rather than on ranges, we could use a multi-way if statement: if (class == 1) cout << “COSC 150” << endl; else if (class == 2) cout << “COSC 160” << endl; else if (class == 3) cout << “COSC 507” << endl;

The Switch Statement (cont'd) Or, we could use the switch statement, which implements multi-way branches based on the value of an expression. Switch (class) { case 1: cout << “COSC 150” << endl; break; case 2: cout << “COSC 160” << endl; break; case 3: cout << “COSC 507” << endl; break; default: cout << “Illegal value” << endl; break; }

The Switch statement (cont'd) What the switch statement does depends on the controlling expression, given in parentheses. If the value matches one of the cases, that code is done. Without the break statement, the next case is also done. The break statement forces the program to bypass the rest of the cases. The default case is done if the value doesn't match any of the cases.

The Break Statement The use (or non-use) of the break statement gives the programming greater flexibility. You decide if the next case is to be done or not. The reason you might want the next case to be done is that often different values should be processed the same way: switch (inputCommand) { case 'A': case 'a': cout << “command a”; break; case 'C': case 'c': cout << “command c”; break; }

Multi-way If vs. Switch The switch statement is more compact than the multi-way if statement and should be used when the code to be done is based on the value of an int or char expression. However, the switch statement cannot be used for string or floating point expressions and so the multi-way if statement must be used.

Enumerated Types Often, a problem uses a set of named literals (constants), e.g., the months of the year, or the days of the week. Good programming practice says that we should use mnemonic names rather that numbers to represent such items. Instead of creating twelve (or seven) constants, we can use an enumerated type – that is create a set of new literals to represent the items.

Enumerated Type Examples enum Months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEPT, OCT, NOV, DEC }; enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; You could also use enumerated types to assign integer values to the literals: enum MyNums { ONE=1, TWO=2, THREE=3 };

The Conditional Operator The conditional operator (my personal favorite of all the operators) is a trinary operator (three operands) which evaluates a test (Boolean expression) and based on the value of the expression, takes its value from one of two other expressions (the true-case value or the false-case value). It is similar to an if- else statement, but can be used in an expression.

Conditional Operator Example cout b ? a : b) << endl; is more succinct than the if-else statement if (a < b) cout << a << endl; else cout << b << endl;

Exercise Write a C++ program that reads in a char from the user and then a number (double). If the char is 'C' or 'c', then the number is the radius of a circle. If the char is 'S' or 's', then the number is the side of a square. If the letter is 'G' or 'g' then the number is the radius of a sphere. The program should then print out the area or volume of the object.

Exercise (cont'd) Your program should: Read in a char shape Read in a double d Use a multi-way if statement or a switch statement to distinguish the choices Calculate the area (vol) of the object and print it out Circle = PI * d^2; Square == d^2; Sphere: 4/3 * PI * d^3