Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.

Slides:



Advertisements
Similar presentations
Selection Structures: if and switch Statements
Advertisements

ICS103: Programming in C 4: Selection Structures Muhamed F. Mudawar.
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
Chapter 4 Selection Structures: if and switch Statements Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
1 ICS103 Programming in C Ch4: Selection Structures.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 4 Selection Structures: if and switch Statements Instructor: Alkar / Demirer.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
true (any other value but zero) false (zero) expression Statement 2
1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Visual C++ Programming: Concepts and Projects
1 ICS103 Programming in C Lecture 6: Selection Structures.
Chapter 4 Selection Structures: if and switch Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program.
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
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
chap4 Chapter 4 Selection Structures: if and switch Statements.
Department of CSE Control Structures: Selection.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Statements: Part1  if, if…else, switch 1.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Decision making If.. else statement.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
CNG 140 C Programming (Lecture set 3)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
EGR 2261 Unit 4 Control Structures I: Selection
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Java Programming: Guided Learning with Early Objects
Flow of Control.
Control Structures – Selection
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Selection Structures: if and switch Statements
Flow of Control.
Decision making If statement.
Decision I (if Statement)
Week 3 – Program Control Structure
Structured Program Development in C++
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4: Selection Structures: if and switch Statements
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

Chapter 4 : Selection Structures: if and switch statement By Suraya Alias

 Control structures Control the flow of execution in a program function. It enables you to combine individual instructions into a single logical unit with one entry point and one exit point. Instructions are organized into three kinds of control execution flow: 1.Sequence 2.Selection 3.Repetition  Compound Statement Is written as a group of statements bracketed by { and }; used to specify sequential flow. 1-2

 Example of compound statement: { Statement 1; Statement 2;.. }  It controls flow from statement 1 to statement 2 and so on  Selection Control Structure A control structure that chooses among alternative program statements 1-3

 An expression that is either false (represented by 0) or true (represented by 1) Example : rest_heart_rate > 75 Is true (1) if the value of rest_heart_rate is over 75 and false (0) if vice versa.  Relational and Equality Operator: 1-4 OperatorMeaningType <Less thanRelational >Greater thanRelational <=Less than or equal toRelational >=Greater than or equal toRelational ==Equal toEquality !=Not equal toequality

 Unary Operator – an operator that has one operand  Binary Operators – an operator that has two operands  Logical Operators 1. && (and) 2. || (or) 3. ! (not)  We can form logical expression using logical operators  Example : 1. salary 5 This expression evaluates to 1 (true) if either the condition salary 5 is true 2. Temperature > 90.0 && humidity > 0.90 This expression evaluates to true only when BOTH conditions are true 1-5

Operand 1Operand 2Operand1 && Operand2 True/1 False/0 True/1False/0 Operand 1Operand 2Operand1 || Operand2 True/1 False/0True/1 False/0True/1 False/0 Operand 1!Operand 1 TrueFalse

1-7 Short circuit evaluation – stopping evaluation of a logical Expression as soon as the value can be determined

1-8 Test whether x lies within the range min through max (inclusive) The inclusive range is shaded. The expression is 1(true) if x lies within this range and 0 (false) If x is outside the range.

1-9 X and Y are greater than Z The shaded area represents the values of x that yields a TRUE result

 If statement with TWO alternatives if (rest_heart_rate > 56) printf(“keep up your exercise program”) else Printf(“Your heart is in excellent health”) Form : if (condition) statement true; else statement false; 1- 10

 If statement with ONE alternative if (x !=0.0) product = product * x; Form : if (condition) statement true; 1- 11

1- 12 Flowchart – a diagram that shows the step by step execution of a control structure

if ( money > 10.00) { printf(“lunch at McD”); balance = money–10.00; } else { Printf(“lunch at home”) balance = money; } if (condition){ True task } else { false task } 1- 13

1- 14 Statement PartXYtempEffect ? If (x>y){12.5>5.0 is true temp = x;12.5Store old x in temp x = y;5.0Store old y in y y = temp;12.5Store old x in y Hand trace (desk check) – step by step simulation of an algorithm’s execution

1- 15

 Decision steps – an algorithm step that selects one of several action. Coded in IF statements.  Pseudo-code – a combination of English phrases and C constructs to describe algorithm steps. Example : Algorithm for comp_late_charge Psuedocode:C program; If unpaid > 0if (unpaid > 0) Assess late charge late_charge = LATE_C; Elseelse Access no late charge late_charge = 0.0; return (late_charge)  Cohesive Function – a function that performs a single operation 1- 16

1- 17

1- 18

1- 19

1- 20

1- 21

1- 22

1- 23

 Nested if statement An if statement with another if statement as its true task or its false task If (x>0) num_pos = num_pos + 1; Else if (x < 0) /*nested if*/ num_neg = num_neg + 1; else /*x equals 0*/ num_zero = num_zero + 1;

Syntax: If ( condition 1 ) statement 1 Else if ( condition 2 ) statement 2. Else if (condition n) statement n Else statement e 1- 25

Salary range ($)Base tax ($) Percentage of excess 0.00– 14, , , , , , , , , , , , ,

1- 27

1- 28

1- 29 Statement PartsalaryTaxEffect If (salary < 0.0) Else if (salary < ) Else if (salary < ) Tax = (salary ) * ? < 0 is false; < 15,000.0 is false; < 30,000.0 is true; Evaluates to Evaluates to Evaluates to

1- 30

1- 31 If ( road_status == ‘S’ ) If ( temp > 0 ) { printf(“wet roads ahead\n”); printf(“stopping time doubled\n”); } else { printf(“icy roads ahead\n”); printf(“stopping time quadrupled\n”); } Else Printf(“Drive carefully\n”);

 Can be used in C to select one of several alternative  Useful when the selection is based on the value of a single variable or of a simple expression (called the controlling expression)  The value of this expression may be type int or char but not double.  First the value in variable class is evaluated, then the list of case label is searched until it matches the value  Statement following the matching case label are executed until a break statement is encountered Class IDShip class B or b C or c D or d F or f Battleship Cruiser Destroyer Frigate

1- 33 Syntax: switch(controlling expression) { Label set1 statement 1 break; Label set 2 statement 2 break; … Default: statement d }

1- 34