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.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Structures.
The if-else Statements
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
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
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Java Programming: From The ground Up  Chapter 4 Selection and Decision: if Statements.
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.
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.
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):
Basic Of Computer Science
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
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.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Chapter 5: Structured Programming
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Java Programming Fifth Edition Chapter 5 Making Decisions.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 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):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Lecture 3 Selection Statements
Branching statements.
If/Else Statements.
Chapter 3 Selection Statements
Selection (also known as Branching) Jumail Bin Taliba by
Lecture 3- Decision Structures
More Selections BIS1523 – Lecture 9.
Chapter 7 Conditional Statements
Flow Control Statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Control Structures Part 3
Lecture Notes – Week 2 Lecture-2
HNDIT11034 More Operators.
Presentation transcript:

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. For this we use conditionals if statements switch statements

2 Conditionals Example: IF score is higher than 50 THEN grade is PASS ELSE grade is FAIL In C++ this corresponds to one statement with 3 parts: if (score > 50) { grade = PASS; } else { grade = FAIL; }

3 Conditionals if (score > 50) { grade = PASS; } else { grade = FAIL; } Part 1 : the condition. An expression that evaluates to TRUE or FALSE

4 Conditionals if (score > 50) { grade = PASS; } else { grade = FAIL; } Part 2 : the TRUE part. A block of statements that are executed if the condition evaluates to TRUE

5 Conditionals if (score > 50) { grade = PASS; } else { grade = FAIL; } Part 3 : the FALSE part. A block of statements that are executed if the condition evaluates to FALSE

6 Conditionals Sometimes, we do not need a FALSE part: In that case, if the condition is FALSE, execution will continue at the statement following the if-statement. if (gas_tank_state == EMPTY) { fill_up_tank(); }

7 Conditionals If the TRUE or the FALSE part consists of only one statement, the curly braces may be omitted. The following statements are equivalent: if (score > 50) { grade = PASS; } else { grade = FAIL; } if (score > 50) grade = PASS; else grade = FAIL;

8 Conditionals We often use cascading if-statements: if (score > 90) lettergrade = 'A'; else if (score > 75) lettergrade = 'B'; else if (score > 60) lettergrade = 'C'; else if (score > 50) lettergrade = 'D'; else lettergrade = 'F';

9 Conditionals Cascading if-statements may sometimes be replaced with a switch statement: if (lettergrade == 'A') cout << "Very good!"; else if (lettergrade == 'B') cout << "Good!"; else if (lettergrade == 'C') cout << "Adequate"; else cout << "Work harder!"; switch (lettergrade) { case 'A': cout << "Very good!"; break; case 'B': cout << "Good!"; break; case 'C': cout << "Adequate"; break; default: cout << "Work harder!"; break; }

10 Conditionals switch (expression) { case value1: statements; break; case value2 : statements; break;... default : statements; break; } In English: Check the value of expression. Is it equal to value1 ? If yes, execute the statements and break out of the switch. If no, Is it equal to value2 ? etc. If it's not equal to any of the above, execute the default statements and then break out of the switch

11 Conditionals switch (expression) { case value1: statements; break; case value2 : statements; break;... default : statements; break; } -- expression should evaluate to either an int or a char -- NEVER omit break; (see next slide for an example of what may happen) -- ALWAYS have a default to cover the case when none of the above values match

12 Conditionals switch (lettergrade) { case 'A': case 'B': case 'C': case 'D': cout << "You passed!"; break; case 'F' : cout << "You failed!"; break; default: cout << "You received a " << lettergrade; } This is equivalent to: if (lettergrade == 'A' || lettergrade == 'B' || lettergrade == 'C' || lettergrade == 'D') cout << "You passed!"; else if (lettergrade == 'F') cout << "You failed!"; else cout << "You received a " << lettergrade;

13 Conditionals int x = -1; int y; switch ( x ) { case -1: y = 10; case 1 : y = 20; default : y = 30; } cout << y; This piece of code prints 30 on the screen x is -1, so the first case applies. y is assigned the value 10. Since there is no break statement, execution continues to the next case and eventually y becomes 30 which is not what we intended. This event is called fall-through.