If/Else Statements.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Decision Structures and Boolean Logic
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Week 4 Program Control Structure
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
More on the Selection Structure
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Control Structures.
While Loops.
For & do/while Loops.
Topics The if Statement The if-else Statement Comparing Strings
Control Structures: Selection Statement
3 Control Statements:.
Chapter 7 Conditional Statements
Topics 4.1 Relational Operators 4.2 The if Statement
Chapter 4: Control Structures I (Selection)
Conditionals.
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
PROGRAM FLOWCHART Selection Statements.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
CprE 185: Intro to Problem Solving (using C)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

If/Else Statements

Blocks A Block is a set of 0 or more statements between braces { } It allows several statements to syntactically operate as one statement.

Blocks Blocks usually mark the begin and end of some control statement, such as the main() program void main() { // 0 or more statements here }

Blocks Style 1: recommended The braces should be on their own line, lined up with each other and the control statement, with all statements between indented: void main() { // 0 or more statements here, indented }

Blocks Style 2: common (space saver) Another common style is to put the { at the end of the control statement, the } on a line below, lined up with the control statement, and all lines between indented. void main() { // 0 or more statements here }

Conditional Expressions A Conditional Expression is an expression that evaluates to true or false. Relational Operators are used to create conditional expressions. Logical Operators may also be used.

Relational Operators Relational Operators compare two values of compatible types and result in the value true or false. C++ operator Meaning < Less Than <= Less Than or Equal To > Greater Than >= Greater Than or Equal To == Equal To != Not Equal To

Relational Operators = vs. == x = 3 is a command: Change the value of x to 3 x == 3 is a question: Is x equal to 3? (yes or no)

Relational Operators Examples: 3 == 3.0 true 4 < 4 false string x=”Apple”; x == ”apple” false (case sensitive)

Logical Operators operate on one or two Conditional Expressions and result in true or false. C++ Operator Meaning and Syntax && AND: Syntax: CondEx && CondEx || OR: Syntax: CondEx || CondEx ! NOT: Syntax: !CondEx

Logical Operators Semantics: The Truth Table A B A && B A || B ! A true false

Logical Operators Examples: (4 > 5) || (3 != 3) false (4 > 5) || (3 == 3) true (4 > 5) && (3 == 3) false (4 < 5) && (3 == 3) true !(4 != 5) false

if/else statements if/else statements are used to determine whether or not certain actions are carried out. They work the same as in Matlab, Python and other languages; the syntax is slightly different.

if/else statements Syntax: if (conditionalExpression) oneStatement_T else oneStatement_F The else oneStatement_F is optional. Note the parentheses are ALWAYS required.

if/else statements Syntax: oneStatement can be either: - Exactly one statement with a semicolon Or - A Block (no semicolon after the } )

if/else statements Semantics: When the conditionalExpression is true - execute oneStatement_T - skip oneStatement_F When the conditionalExpression is false - skip oneStatement_T - execute oneStatement_F (when it exists)

if/else statements Style: one statement if (conditionalExpression) oneStatement_T; //indent else //line up with if oneStatement_F; //indent

if/else statements Style: blocks #1 if (conditionalExpression) { //line up with if statements //indent } //line up with { else oneStatement_F;

if/else statements Style: blocks #2 if (conditionalExpression) { statements //indent } //line up with if else oneStatement_F;

if/else statements Style: blocks #2 if (conditionalExpression) { statements //indent } //line up with if else oneStatement_F;

if/else statements Examples: int a = 3; if ((a >= 2) || (a < 0)) Prints: cout << ”Apple\n”; Apple else Banana cout << ”Orange\n”; cout << ”Banana\n”; // STYLE doesn’t matter!

if/else statements Examples: int a = 3; if ((a >= 2) || (a < 0)) Prints: cout << ”Apple\n”; Apple else { cout << ”Orange\n”; cout << ”Banana\n”; }

if/else statements Examples: int a = 3; if ((a >= 2) && (a < 0)) { Prints: cout << ”Apple\n”; Banana cout << ”Orange\n”; } cout << ”Banana\n”;

if/else statements Examples: Nested if/else int a = 3, b = 2; if (a < 0) Prints: if (b > 3) Banana cout << ”Apple\n”; else // proper style cout << ”Orange\n”; cout << ”Banana\n”;

if/else statements Examples: Nested if/else int a = 3, b = 2; if (a < 0) Prints: if (b > 3) Banana cout << ”Apple\n”; else // improper style, but cout << ”Orange\n”; // no effect on semantics cout << ”Banana\n”;

if/else statements Examples: Nested if/else int a = 3, b = 2; if (a < 0) { Prints: if (b > 3) Orange cout << ”Apple\n”; Banana } else cout << ”Orange\n”; cout << ”Banana\n”;

Multiway Decisions Instead of just “this or that”, sometimes one of several (3 or more) paths needs to be chosen. The following slide has an example of setting a letter grade based on a percentage (no D’s in this course!)

Multiway Decisions if (perc >= 90) // Multiway Style: grade = ’A’; // just a different else if (perc >=80) // style grade = ’B’; else if (perc >=70) grade = ’C’; else //default decision grade = ’F’;

Multiway Decisions if (perc >= 90) // ”Standard” Style grade = ’A’; // same Semantics else if (perc >=80) grade = ’B’; if (perc >=70) grade = ’C’; else //default decision grade = ’F’;

switch statement Used to make a multi-way decision when 1. The decision is based on ONE variable 2. The variable is not string

switch statement Syntax: switch (variable) { case literal1: statement(s) break; // optional case literal2: default: // optional statement(s) // optional }

switch statement Semantics: Top to bottom, checks if variable == literal If true: begins executing statement(s) does NOT stop at the next case If false: tries the next case break; means skip to end }

switch statement Semantics: default: Execute the following statement(s) if the variable did not match ANY literal

switch statement Style: Varies greatly depending on the problem being solved. As always, choose a style to make the code clear. It should allow for “quick spotting” of case literals.

switch statement Example: typical, compact style char letterGrade = ’C’; string s = ”?”; switch (letterGrade) { case ’A’: s = ”Excellent”; break; case ’B’: s = ”Good”; break; case ’C’: s = ”Average”; break; default: s = ”Invalid”; }

switch statement Example: multiple values per “case” (2 styles) char letterGrade = ’C’; string s=”?”; switch (letterGrade) { case ’A’: // long style case ’a’: s = ”Excellent”; break; case ’B’: case ’b’: s = ”Good”; break; }

switch statement Example: without breaks char letterGrade = ’B’; switch (letterGrade) { Prints: case ’A’: cout << ”Excellent\n”; Good case ’B’: cout << ”Good\n”; Average case ’C’: cout << ”Average\n”; Invalid default: cout << ”Invalid\n”; }

Vocabulary Term Definition Block Set of 0 or more statements between braces. Syntactically acts as one statement Conditional Expression Expression that evaluates to TRUE or FALSE Relational Operator Operators that compare two values and evaluate to TRUE or FALSE. Ex: < <= > >= == != Logical Operator Operators that operate on one or two conditional expressions and evaluate to TRUE or FALSE. Ex: && || ! Nested When one statement is inside the control of another statement of the same type. Ex: if/else inside an if/else.