Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

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.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Flow of Control is Sequential unless a “control structure” is used to change that there are 2 general types of control structures: Selection (also called.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
1 ICS103 Programming in C Lecture 6: Selection Structures.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection.
Chapter 4 Selection Structures: if and switch Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Decision Structures and Boolean Logic
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
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
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
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.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
chap4 Chapter 4 Selection Structures: if and switch Statements.
Department of CSE Control Structures: Selection.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
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.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
CNG 140 C Programming (Lecture set 3)
Sequence, Selection, Iteration The IF Statement
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.
Selection—Making Decisions
Operator Precedence Operators Precedence Parentheses () unary
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Topics The if Statement The if-else Statement Comparing Strings
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Flow of Control.
Chapter 7 Conditional Statements
Life is Full of Alternatives
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Flow of Control.
Relational and Logical Operators
ICS103: Programming in C 4: Selection Structures
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition 4 A compound statement is used to specify sequential flow

Control Structures 4 In sequential flow, control flows from one statement to the next in the compound statement 4 This is the control structure we have been using up until now 4 In this lesson, we will introduce the selection control structures which let us choose between alternative statements

Conditions 4 A program chooses among alternative statements by evaluating program variables –age > 65 /* age an int variable */ –This expression is an example of a condition –A condition establishes a criterion for executing or skipping a group of statements –A condition evaluates to either true (represented by 1) or false (represented by 0)

Relational & Equality Operators 4 The most common conditions will have one of the following forms: –variable relational-operator variable –variable relational-operator constant –variable equality-operator variable –variable equality-operator constant –The constant may also be the first operand

Relational & Equality Operators 4 The relational and equality operators in C are the following: –< less than –> greater than –<= less than or equal to –>= greater than or equal to –== equal to –!= not equal to

Logical Operators 4 We can form more complicated conditions using the logical operators && (and), || (or), ! (not) –salary 5 –temperature > 90.0 && humidity > 0.90 –n >= 0 && n <= 100 –0 <= n && n <= 100 –!(0 <= n && n <= 100) 4 Logical expressions are evaluated as are arithmetic expressions

The && Operator (and)

The || Operator (or)

The ! Operator (not)

Operator Precedence  We can mix arithmetic and logical operators ( x+5 == y ) thus we must know the precedence of both types of operators –! + - & (unary operators) –* / % –+ - – = > –== != –&& –|| –=

Operator Precedence  (x 0.0  x + y < min + max  (x + y) < (min + max)

Operator Precedence 4 Let us assume that the following variables have the following values: x 3.0, y 4.0, z 2.0, flag 0. –!flag –x + y /z <= 3.5 –!flag || (y + z >= x - z) –!(flag || (y + z >= x - z))

Short-Circuit Evaluation 4 If we have a logical expression with an ‘or’ operator and the first operand evaluates to ‘true’, C doesn’t evaluate the second operand (Why?). This is known as short- circuit evaluation 4 This also works for the ‘and’ operator where the first operand is ‘false’ 4 Why is it important to know this feature of C?

Comparing Characters 4 We can also compare characters using the relational and equality operators –‘9’ >= ‘0’ –‘a’ < ‘e’ –‘Z’ == ‘z’ –‘a’ <= ‘A’

Logical Assignment 4 The simplest form of a logical expression in C is a single int value or variable –0 represents false –A non-zero value represents true 4 We can use the assignment statement to give a logical value to a variable: –senior_cititzen = 1; –senior_citizen = (age >= 65);

Logical Assignment 4 We can now use the variable in a logical expression –senior_citizen && gender == ‘M’ –Evaluates to 1 (true) if senior_citizen is 1 (true) and the character in gender is M

Complementing a Condition 4 A logical expression may be complemented by preceding it with the ! Operator 4 A simple condition can be complemented by changing the condition 4 DeMorgan’s theorem tells us that the complement of expr 1 && expr 2 is comp 1 || comp 2, the complement of expr 1 || expr 2 is comp 1 && comp 2

Complementing a Condition 4 We can write the complement of –age > 25 && (status == ‘S’ || status == ‘D’) –age <= 25 || (status != ‘S’ && status != ‘D’)

The if Statement 4 Now that we are able to write conditions, we will use these conditions to choose among alternative groups of statements 4 The C statement which accomplishes this is the if statement 4 An example of an if statement is if (rest_heart_rate> 56) printf(‘Keep up your exercise program!\n’);

The if Statement if (rest_heart_rate> 56) printf(‘Keep up your exercise program!\n’); else printf(‘Your heart is in great health!\n’); 4 This is an example of an if statement with two alternatives –Here, each alternative is a single statement, but in general each alternative can be a compound statement

The if Statement 4 We can also write an if statement with a single alternative if (x != 0.0) product = product * x; 4 In this case, if the condition is false, we simply go on to the statement following the if

The if Statement 4 If we want to have more than one statement in an alternative, we must use the compound statement construct if (x != 0.0) result = y / x; else { printf(‘You can’t do this!\n’); result = 0.0; }

The if Statement if (x != 0.0) result = y / x; else printf(‘You can’t do this!\n’); result = 0.0; 4 What happens in this case? –Indentation has no effect on program execution!

The if Statement 4 The general form of the if statement with two alternatives where both alternatives have multiple statements is: if (condition){ true task } else { false task }

The if Statement 4 Imagine that we have two variables in a program. One, sex, is a char variable which will have the value ‘m’ or ‘f’. The second, age, is an int variable which holds a person’s age. Write an if statement which examines the variables and prints out ‘Eligible’ for females 63 and older and males 65 and older, and ‘Ineligible’ for everyone else

The if Statement 4 We may also need to choose among multiple alternatives (e.g. different prices for men, women, and children). For these cases, we use the nested form of the if statement if (type == ‘m’) price = 25; else if (type == ‘f’) price = 20; else /* type is ‘c’ */ price = 15;

The if Statement 4 The general form for the nested if statement is (each statement may be compound): if (condition 1 ) statement 1 else if (condition 2 ) statement 2... else if (condition n ) statement n else statement e