Conditions and Decisions. Assignments Reading – Chapter 4 – 4.5-4.8.

Slides:



Advertisements
Similar presentations
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
Nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Selection in C.
Examples from “c++ how to program” book. SELECTIONS WITH IF-ELSE Example: if ( grade >= 60) cout = 60) cout
Chapter 4: Basic C Operators
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Control statements Mostafa Abdallah
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
1 CS161 Introduction to Computer Science Topic #6.
Decision Statements, Short- Circuit Evaluation, Errors.
Control Statements: Part1  if, if…else, switch 1.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Rational Expressions relational operators logical operators order of precedence.
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Selection (also known as Branching) Jumail Bin Taliba by
Rational Expressions. relational operators. logical operators
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Bools & Ifs.
Bools & Ifs.
Selection Control Structure
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.
Branching statements Kingdom of Saudi Arabia
The boolean type and boolean operators
Presentation transcript:

Conditions and Decisions

Assignments Reading – Chapter 4 –

Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant

Relational Operators < > <= >= == –NOT the same as = !=

Examples x=5 y=8 z=5 MAX=10 initial=‘s’ x<y y>MAX x<=z z>=MAX initial==‘r’ x!=z

Logical Operators && || x=5 y=8 z=5 MAX=10 initial=‘s’ x MAX

Precedence function calls unary operators (! + - &) * / % + - = > == != && || =

Short-Circuit Evaluation Stop evaluation when true/false value is determined x=6 y=9 x>2 || y > 13 x 13

Logical Assignment and Negation int in_range; //1 if x between 1-10, 0 otherwise in_range = (x>0 && x<=10); int same_initials; int not_same_initials; same_initials = (first_initial==‘S’&& last_initial==‘R’); not_same_initials = ! (first_initial==‘S’&& last_initial==‘R’); not_same_initials = (first_initial!=‘S’ || last_initial!=‘R’);

DeMorgan’s Theorem !(a && b) => (!a || !b) !(a || b) => (!a && !b)

if Statement if(condition) { statements } if(condition) single statement

Example if(age >= 16) printf(“You can legally drive a car.”); if(age >= 21) { printf(“You can purchase alcohol.”); printf(“You can gamble.”); }

if/else Statement if(condition) { statements } else { statements } if(condition) single statement else single statement

Example if(grade > 60) printf(“You pass the class.”); else printf(“You failed, try again.”); if(grade > 60) { printf(“You pass the class.”); printf(“Move on to the next class in the sequence.”); } else { printf(“You failed, try again.”); printf(“You cannot advance to the next class.”); }