Decision Making and Branching

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

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.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
true (any other value but zero) false (zero) expression Statement 2
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
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.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Decision Making and Branching
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
Decision Making and Branching (cont.)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
‘C’ Programming Khalid Jamal.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
JavaScript: Control Statements I
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Programming Fundamentals
Programming Fundamentals
JavaScript: Control Statements.
Control Structures.
Lecture 2: Logical Problems with Choices
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Structured Program
Week 3 – Program Control Structure
Chapter 5: Control Structures II (Repetition)
CSC215 Lecture Control Flow.
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Decision Making and Branching

Introduction C Language supports the following decision-making statements: if statement switch statement Conditional operator statement goto statement while statment for statement In this lecture we study if and while statements.

Decision making with if statment if is used to control the flow of the execution of statements. If (test expression) It allows the computer to evaluate the expression first and them depending on whether the value of the expression (relation of condition) is true (non-zero) of false (zero) it transfers the control to a particular statement. The if statement is called branching statement of selection statement because it provide a junction where the program has to select which of two paths to follow. Entry if (bank balance is zero) borrow money if (room is dark) turn on lights if (code is 1) person is mal if (age is more than 55) person is retired Test expression? False True

Simple if statement The statement-block may be a single a statement or a group of statements. If the test_expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. The program tests the type of category of the student. If the student belongs to the SPORTS category, then additional bonus_marks are added to his marks before the are printed.

Illustration of simple if statment The result of the first run is printed as Ratio = -3.181818 The second run has neither produced any results nor any message. During the second run, the value (c - d) is equal to zero and therefore, the statements contained in the statemt0block are skipped. To avoid truncation due to integer division.

The shoes1.c program The program takes your shoes size and tells you how long your foot is in inches.

The while Statement The statement part can be a simple statement with a terminating semicolon, or it can be a compound statement enclosed in braces. This cycle of test and execution is repeated until expression becomes false (zero). Each cycle is called an iteration.

The while loop: shoes2.c When the program first reaches the while statement, it checks to see whether the condition within parentheses is true. shoe < 18.5 The < symbol means "is less than." The variable shoe was initialized to 3.0, which certainly is less than 18.5. Therefore, the condition is true and the program proceeds to the next statement. shoe = shoe + 1.0; The program returns to the while portion to check the condition (because the next line is a closing brace (})). This continues until shoe reaches a value of 19.0. Now the condition becomes false because 19.0 is not less than 18.5. When this happens, control passes to the first statement following the while loop.

The shoes2.c Program

Example The program reads in a list of daily low temperatures and reports the total number of entries and the percentage that were below freezing. A sample run: Enter the list of daily low temperatures. Use Celsius, and enter q to quit. 12 5 -2.5 0 6 8 -3 -10 5 10 q 10 days total: 30.0% were below freezing.

Applying De Morgan’s Rule While designing decision statements, we often come across a situation where logical NOT operator is applied to a compound logical expression like ! (x && y || !z) However, a positive logic is always easy to read an comprehend than a negative logic. In such case, we may apply what is known as De Morgan’s rule to make the total expression positive. Remove the parentheses by applying the NOT operator to every logical expression component, while complementing the relational operators x becomes !x && becomes || !x becomes x || becomes && Examples: !(x && y || !z) becomes !x || !y && z !(x <= 0 || !condition) becomes x > 0 && condition

False-block statement The if … else statement If the test expression is true, then the true-block statement(s), immediately following the if statements are executed; otherwise, the false-block statement(s) are executed. In either case, either true-block or false-block will be executed, not both. Entry Test expression? True False True-block statement False-block statement statement-x

Adding else to the if Statement C enables to choose between two statements by using the if else form. if (expression) statement1 else statement2 No need for second If

Examples The test determines whether or not the student is a boy or girl and increment corresponding variable. When the value (c – d ) is zero, the ratio is not calculated and the program stops without any message. In such cases we may not know whether the program stopped due to a zero value of some other error.

Example A program evaluates the power series 𝑒 𝑥 =1+𝑥+ 𝑥 2 2! + 𝑥 3 3! +…+ 𝑥 𝑛 𝑛! , 0<𝑥<1. The power series contains the recurrence relationship of the type 𝑇 𝑛 = 𝑇 𝑛−1 𝑥 𝑛 for 𝑛>1. 𝑇 1 =𝑥 for 𝑛=1, 𝑇 0 =1 If 𝑇 𝑛−1 is known, then 𝑇 𝑛 can be easily found by multiplying the previous term by 𝑥/𝑛. Then 𝑒 𝑥 = 𝑇 0 + 𝑇 1 + 𝑇 2 +…+ 𝑇 𝑛 =𝑠𝑢𝑚

Use braces to create a single block If you want more than one statement between the if and the else, you must use braces to create a single block. Wrong Right

Nesting of if…else statments When a series of decisions are involved, we may have to use more than one if…else statement in nested form. Example: Here are the rates one company charges for electricity, based on kilowatt-hours (kWh): First 360 kWh: $0.12589 per kWh Next 320 kWh: $0.17901 per kWh Over 680 kWh: $0.20971 per kWh Let’s prepare a program to calculate our energy costs using multiple choice statement.

The electric.c Program Please enter the kwh used. 580 The charge for 580.0 kwh is $84.70.

The else if is a variation on what you already knew You can string together as many else if statements as you need

Pairing else with if When is Sorry, you lose a turn! printed? When you have a lot of ifs and elses, how does the computer decide which if goes with which else? For example, consider the following program fragment: When is Sorry, you lose a turn! printed? When number is less than or equal to 6, or When number is greater than 12? In other words, does the else go with the first if or the second? The answer is, the else goes with the second if.