Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
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
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
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.
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.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Flow of Control Part 1: Selection
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
Controlling Program Flow with Decision Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
CNG 140 C Programming (Lecture set 3)
The if…else Selection Statement
More on the Selection Structure
Unit VI- Selection – Making Decisions, Repetition
Decisions Chapter 4.
Selection—Making Decisions
Flow of Control.
Control Structures – Selection
Chapter 4: Control Structures I (Selection)
Structured Program
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
The Java switch Statement
CSC215 Lecture Control Flow.
Controlling Program Flow
Structural Program Development: If, If-Else
Presentation transcript:

Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013

3 Learning Outcomes At the end of this session, student will be able to: Demonstrate usage of selection control in C programming language (LO2 & LO3) T Algorithm and Programming

4 Sub Topics Program Control – Selection: –Selection Definition –If –If-Else –Nested If –Program Examples Using If –Switch-Case –?: Operator –Error Type T Algorithm and Programming

5 Selection Definition  In an algorithm implementation, an instruction or block of instructions may be executed (or not) with certain predetermined condition  Syntax: –if –if-else –switch-case T Algorithm and Programming

6 Selection: IF Syntax : if (boolean expression) statement; or if (boolean expression) { statement1; statement2; Block of statements …… } If boolean expression resulting in True, then a statement or some statements will be executed. T Algorithm and Programming

7 Selection: IF  Flow Chart of IF Statement T Algorithm and Programming true false statements condition

8 Selection: IF-ELSE Syntax : if (boolean expression) statement1; else statement2; or if (boolean expression){ statement1; statement2; Block statement1 …… } else { statement3; statement4; Block statement2 … } T Algorithm and Programming If boolean expression resulting in TRUE, then statement1 or block statement1 will be executed, if FALSE then statement2 or block statement2 be executed.

9 Selection: IF-ELSE  Flow Chart of IF-ELSE Statement T Algorithm and Programming true false statements 1 condition statements 2

10 Selection: NESTED-IF Nested selection occurs when the word IF appears more than once within IF statement. Syntax : if (boolean expression) statement1; if (boolean expression) statement2; if (boolean expression) statement3; or if (boolean expression) statement1; else if (boolean expression) statement2; else if (boolean expression) statement3; T Algorithm and Programming

11 Example Using IF Example: Program example to find the roots of a quadratic equation Algorithm : 1. Get the value of coefficients a, b, and c from keyboard 2. Calculate discriminant d = b*b – 4*a*c 3. if d >= 0 then calculate x1 and x2 if d < 0 then stated imaginer, stop. 4. Stop calculate x1 using : calculate x2 using : T Algorithm and Programming -b +  d 2*a -b -  d 2*a

12 Example Using IF-ELSE Example: Wrong IF (unclear IF statement) T Algorithm and Programming #include int main() { int degree: printf(“Input degree: “); scanf(“%d”, &degree); if (degree < 80) if (degree > 30) printf (“Hot\n”); else printf (“Cool\n”); }

13 Selection: SWITCH-CASE Switch-Case Operation This statement is used in exchange of IF-ELSE, when if-else nested number of level is enormous and difficult to read Syntax: switch (expression) { case constant1 : statements1; break;. case constant2 : statements2; break; default : statements; } T Algorithm and Programming

14 Selection: SWITCH-CASE Switch statement evaluate an expression by looking up for each case constant value. If an expression value matches with a case constant value then related statement/s is executed. If nothing match then default statement is executed. Note: Expression and constant type should be integer (including char) T Algorithm and Programming

15 Selection: SWITCH-CASE  Flow Chart of SWITCH-CASE Statement T Algorithm and Programming true false case a case a action(s) break case b case b action(s)break false case z case z action(s)break true default action(s)

16 Program Examples Using SWITCH-CASE  Example: T Algorithm and Programming #include int main() { float val1, val2; char op; while(1) { printf(“\n Type val1 operator val2 \n”); scanf(“%f %c %f”, &val1, &op, &val2); switch(op){ case(‘+’): printf(“ = %f”, val1 + val2); break; case(‘-’) : printf(“ = %f”, val1 - val2); break; case(‘*’) : printf(“ = %f”, val1 * val2); break; case(‘/’) : printf(“ = %f”, val1 / val2); break; default : printf(“ unknown operator!”); } return(0); } Note: case (’+’) can also written case ’+’

17 ?: Operator The operator ? : is similar to the IF statement, but it returns a value Syntax: condition ? then-expression : else-expression Using this operator, you can rewrite if(a > b) max_value = a; else max_value = b; as max_value = (a > b) ? a : b; T Algorithm and Programming

18 Go To and Label C is still providing the old fashion goto statement Syntax: goto label; …… label : …… label is written using colon symbol Avoid using goto to improve code readability T Algorithm and Programming

19 Error Type Compile-Time Error –caused by syntax error Link-Time Error –success fully compiled, but cause link error –caused by no object code at link time Run-Time Error –successfully compiled, but error at runtime. Usually caused by numerical operation such as: overflow, floating point underflow, division by zero, etc. Logical Error –wrong result caused by incorrect logical flow/algorithm T Algorithm and Programming

20 Error Type Among those Error Types the most difficult to debug is Logical Error. Example of Compile-Time Error: T Algorithm and Programming Dev-C compiler will give message: In function main missing terminating ” character, syntax error before return

21 Error Type Some C compiler merge the compile and link processes, causing in difficulty to distinguish between Compile- Time Error with Link-Time Error Example of Link-Time Error (Visual C++) T Algorithm and Programming

22 Error Type Example for Run-Time Error T Algorithm and Programming successfully compiled and linked, but RUN result, causing by overflow (char range max 127)

23 Error Type Example for Run-Time Error T Algorithm and Programming Error cause: Division by Zero

24 Error Type Example for Logical-Error T Algorithm and Programming Should be: x1 = 5.00 and x2 = 2.00 Can you find the logic error ??

25 Summary  In an algorithm implementation, an instruction or block of instructions may be executed (or not) with certain predetermined condition, that’s why we use selection  3 types of selection in C: –if –if-else –switch-case T Algorithm and Programming

26 References Paul J. Dietel,Harvey M. Deitel, C : how to program. PEAPH. New Jersey. ISBN: Chapter 3 & 4 Choosing between Alternatives: Getting Controls: T Algorithm and Programming

27 END T Algorithm and Programming