More on conditional statements

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26,
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 In studying digital integrated circuits, one must start with the simplest group of circuit, the SSIs or Small Scale Integrated Circuits. Since these.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Boolean Values The true story ;=P. Expressions.
Boolean Values The true story ;=P Thanks to Margaret Reid-Miller for her original ideas at Carnegie Mellon’s CSE.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Further Structures for Branching and Looping.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there.
The Ohio State University
If/Else Statements.
Week 3 Part 2 Kyle Dewey.
Thinking Mathematically
Truth Tables and Equivalent Statements
Decisions Chapter 4.
The if Statement Format or No Condition satisfied? Yes
A mechanism for deciding whether an action should be taken
Logic Gates.
Control Statements: Part 2
Expressions and Control Flow in JavaScript
Type boolean boolean: A logical type whose values are true and false.
Control Statement Examples
Conditions and Ifs BIS1523 – Lecture 8.
Selection CSCE 121 J. Michael Moore.
Logic Gates.
Conditions and Boolean Expressions
Program Control Topics While loop For loop Switch statement
Chapter 6 Control Statements: Part 2
Conditionals.
Functions continued.
Arrays.
Chap 7. Advanced Control Statements in Java
CSCE 206 Lab Structured Programming in C
Controlling Program Flow
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

More on conditional statements

If-else in UML 2018 Risto Heinsar

If-else if-else in UML 2018 Risto Heinsar

Logical operators Operator Operation Result will be true when … && Logical AND Conjunction … both sides are true || Logical OR Disjunction ... at least one side is true ! Logical negation Inversion ... initial condition is false 2018 Risto Heinsar

Truth table AND OR NOT a b a && b 1 a b a || b 1 a !a 1 2018 1 a b a || b 1 a !a 1 2018 Risto Heinsar

Examples if (sorted == 0) if (!sorted) if (input >= MIN && input <= MAX) if (input < MIN || input > MAX) if ((input >= MIN && input <= MAX) ||(ignoreLim == 1)) if (input == 0 || input == 1 || input == 15) 2018 Risto Heinsar

De Morgan’s law Useful laws of transformation for Boolean algebra 𝐴∨𝐵 ⟺ 𝐴 ∧ 𝐵 𝐴∧𝐵 ⟺ 𝐴 ∨ 𝐵 Can be extended The following equations have the same result The condition is true when the variable value is neither 1 or 2 if (value != 1 && value != 2) if (!(value == 1 || value == 2)) 2018 Risto Heinsar

Switch statement Uses an expression instead of a condition Tries to find a matching case When no cases match, default case is looked for If a case matches, it will run all statements until either switch ends or break/continue statement is encountered! This includes other cases below the matching one! Default case, break and continue statements are optional but usually desired 2018 Risto Heinsar

The code switch () if / else if / else switch (expression) { case constant or expression: statements break; default: } if (condition) { statements } else if (condition) else 2018 Risto Heinsar

Conditionals in UML 2018 Risto Heinsar

Examples of switch switch (value) { case 1: case 2: case 3: printf("Value is either 1, 2 or 3\n"); break; default: printf("Value not identified\n"); } switch (value) { case 1: printf("Value is 1\n"); break; case 2: printf("Value is 2\n"); default: printf("Value not identified\n"); } 2018 Risto Heinsar

Create an algorithm for a program… … that mimics a typical food scale in the supermarket Get the weight of the product (from user input) Get the type of the product (e.g. banana, cucumber, …) (from user input) Price will be calculated based on the weight and the product type The cost will be outputted along with the product type Initially, a menu of possible food items is shown An error is printed when an unknown product number is chosen. An error is printed when the cost will calculate to 0 2018 Risto Heinsar

Create a product that would emulate a food scale in a supermarket #include <stdio.h> int main(void) { float weight; int productCode; printf(„Insert the weight of the product\n> „); scanf("%f", weight); switch (productCode) case 0: printf(„Product %d – Banana\n“, select); price = 1.10; break; default: price = 0; } cost = price * weight; printf(„Cost: %f\n“, cost); return 0; Create a product that would emulate a food scale in a supermarket 5 products (4 of them create by yourself) Show the list of available products to the user User must be able to input the product weight and type Verify that all of the variables needed are declared Create an exception when cost will be 0 2018 Risto Heinsar