Department of Computer Science Western Michigan University

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
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.
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
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.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Computer Science Department Relational Operators And Decisions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Logic Structures. If… else statements Meaning if the condition is true, execute this command. If the condition is false, execute the else command.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
COMP Loop Statements Yi Hong May 21, 2015.
CPS120: Introduction to Computer Science Decision Making in Programs.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University.
Random Functions Selection Structure Comparison Operators Logical Operator
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Control Flow (Python) Dr. José M. Reyes Álamo.
Java Programming Fifth Edition
Chapter 3: Decisions and Loops
Selection—Making Decisions
Control Structures.
Outline Altering flow of control Boolean expressions
Control Structures: Selection Statement
CS 1111 Introduction to Programming Spring 2019
Computer Science Core Concepts
Conditional Statements
Conditional Logic Presentation Name Course Name
Understanding Conditions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Selection—Making Decisions
Control Structures: Selection Statement
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Department of Computer Science Western Michigan University CS 1023 Intro to Engineering Computing 3: Computer Programming Conditions – Asking Questions if- and if-else-statements Mark Kerstetter Department of Computer Science Western Michigan University © Spring 2012

Conditions Generally used to control what to do next? Expressions that evaluate to True or False In C Programs conditions … Follow keyword if Follow keyword while Contained within ( and ) Relational Operations: >=, >, = =, != , <, <= Compare numeric expressions Compare characters and character strings Boolean (Logical) Operations: ! (not), && (and), | | (or) Only used with logical expressions, i.e., those whose values are True or False Never used with numeric or character expressions Important! = does not compare it assigns = = compares => and =< and =! are not legal

Selection Statements if and if-else Decision Statements Flow Control Statements – Making Decisions What statement(s) to perform next? Two basic instructions – if… and if…else… if-statement if (condition) { statement(s) to execute if condition is true ; } if-else-statements if (condition) { statement(s) to execute if condition is true ; } else { statement(s) to execute if condition is false ; } ; { and } needed to surround multiple statements. Does no harm to use them for single statements. Do not place semicolon after a condition. The if and if-else statements include the statements to do next! ; Caution! A common mistake when writing if and if-else statements is to place a semicolon immediately at the end of the first line of the statement thus creating an empty statement.

Examples of if-statement if ((nbr % 2) == 0 ) evenCount++; totalNbrs = totalNbrs + 1; Count Even Numbers if ((nbr % 2) == 0 ) evenCount++; else oddCount++; totalNbrs = totalNbrs + 1; Count Even & Odd Numbers if ( (side1 > 0) && (side2 > 0) ) { area = side1 * side2; perimiter = 2*(side1 + side2); } else { printf(“Cannot compute area or perimeter.\n”); } Multiple Instructions in Then-part

Examples of if-statement Nested if-else-statements if ((s1<s2+s3) && (s2<s1+s3) && (s3<s1+s2)) { printf(“legal triangle\n”) ; if ( (s1==s2) && (s2==s3) ) { printf(“equilateral triangle\n”) ; } else if (((s1==s2)&&(s1!=s3)) || (((s1==s3)&&(s1!=s2) || ((s2==s3)&&(s1!=s2))) printf(“isosceles triangle\n”) ; else printf(“scalene triangle\n”) ; } else printf(“not a triangle\n”) ;