true (any other value but zero) false (zero) expression Statement 2

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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Control Flow C and Data Structures Baojian Hua
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
UNIT II Decision Making And Branching Decision Making And Looping
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
 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.
CNG 140 C Programming (Lecture set 3)
Selection (also known as Branching) Jumail Bin Taliba by
Introduction to C++ Programming Language
Selections Java.
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.
Selection—Making Decisions
Flow of Control.
Programming Fundamentals
JavaScript: Control Statements I
Control Structures.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Topics discussed in this section:
Computer Programming Basics
CprE 185: Intro to Problem Solving (using C)
CSC215 Lecture Control Flow.
Presentation transcript:

true (any other value but zero) false (zero) expression Statement 2 Selection Selection allows you to choose between two or more alternatives. In C this means that the course of your executing program will depend on the result of an expression. true (any other value but zero) false (zero) expression Statement 2 Statement 1 Logical Flow

Selection Logical data in C - C recognizes zero as a false value and any other nonzero value is considered true. Logical Operators - logical operators form conditions or logical expressions. The not operator ( ! ) changes a true value (nonzero ) to false ( zero ) and a false value ( zero ) to true (one ).

Selection The and operator ( && ) is a binary operator with four distinct possible combinations of values in its operands. The or operator ( || ) is a binary operator with four distinct combinations of values in its operands.

Selection Short-circuit evaluation - C will stop evaluation when it knows for sure what the final result will be. false && ( anything ) true || ( anything ) after the first operand is evaluated and found to be false and the operator is the and operator ( && ) the second operand will not be evaluated ( this could cause unexpected results if the second operand has side effects ) Relational Operators - Relational operators support logical relations. They are all binary operators that accept two operands and compare them. The result is logical data, that is, it is always a zero or one.

Syntactical rules for if…else statements: Selection The if …. else Statement - An if…else statement is a composite statement used to make a decision between two alternatives. Syntax: if ( expression ) statement 1 else statement 2 The expression can be any C expression. After it has been evaluated, if its value is true (not zero ), statement 1 is executed: otherwise, statement2 is executed. It is impossible for both statements to be executed in the same evaluation. Syntactical rules for if…else statements: The expression must be enclosed in parentheses. No semicolon ( ; ) is needed for an if..else statement. Statement 1 and statement 2 may have a semicolon as required by their types. The expression can have a side effect. Both the true and false statements can be any statement (even another if…else statement) or can be a null statement.

expression statements Selection We can swap the position of statement 1 and statement2 if we use the complement of the original expressions. if ( x > y) printf( “ x is greater than y\n”) ; else printf(“ y is greater than x\n”) ; An if…else with a compound statement if ( x != y) { printf( “ x is not equal to y\n”) ; x = y; } printf(“ x is equal to y\n”) ; The semicolons belong to the expression statements not to the if…else statement Curly brackets

printf( “ x is either 8 or 9\n”) ; } Selection A null else statement: if ( x > 7 && x < 10) { printf( “ x is either 8 or 9\n”) ; } else is null

The compiler pairs this if and else! Selection Nested if statements - when an if…else is included within an if…else, it is known as a nested if. if ( x <= y) if ( x < y) printf( “ %d < %d\n”, x ,y); else printf( “ %d == %d\n”, x ,y); printf(“ %d > %d\n”, x ,y); Dangling else problem - This problem is created when there is no matching else for every if. Simple rule: else is always paired with the most recent unpaired if The compiler pairs this if and else!

Selection Multiway selection - multiway selection chooses among several alternatives. There are two different ways to implement multiway selection in C. The first is by using the switch statement. The other is a programming technique known as the else-if that provides a convenient style to nest if statements. The else-if -There is no such C construct as the else-if. Rather, it is a style of coding that is used when you need a multiway selection based on a value that is not integral. if ( score >= 90 ) grade = ‘A’ ; else if (score >= 80 ) grade = ‘B’ ; else if (score >= 70 ) grade = ‘C’ ; else if (score >= 60 ) grade = ‘D’ ; else grade = ‘F’ ; The else-if is used when: The selection variable is not an integral and The same variable is being tested in the expression

Selection The switch Statement - Switch is a composite statement used to make a decision between many alternatives. The selection condition must be one of the C integral types. Syntax: switch ( expression ) { case constant-1 : statement; statement; case constant-2 : statement; …… case constant-3 : statement; ……. case constant-n : statement; default : statement; } /* end switch */

Selection Syntactical rules for the switch statement: There must be at least one case statement. Each case expression is associated with a constant. The case expression is followed by a colon ( : ) and then the statement with which it is associated. There may be one or more statements for each case. The case label simply provides an entry point to start executing the code. Default is executed whenever none of the previous case values matched the value in the switch expression. The default is optional. When the statements associated with one case have been executed, the program flow continues with the statements for the next case unless a break statement is used. The break statement causes the program to jump out of the switch statement (goes to the closing brackets and continues with code following the switch