Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.

Slides:



Advertisements
Similar presentations
Chapter 3 section 2. Please form your groups The 1 st column represents all possibilities for the statement that can be either True or False. The 2 nd.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
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
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Truth Tables for Negation, Conjunction, and Disjunction.
Section 1-4 Logic Katelyn Donovan MAT 202 Dr. Marinas January 19, 2006.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
BUILDING COMPUTER CIRCUITS prepared by Burak Galip ASLAN September, 2006 BOOLEAN LOGIC AND GATES CONTROL CIRCUITS.
Decision Structures and Boolean Logic
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Math 240: Transition to Advanced Math Deductive reasoning: logic is used to draw conclusions based on statements accepted as true. Thus conclusions are.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 05 (Part III) Control Statements: Part II.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
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.
Conditional Statements
How do I show that two compound propositions are logically equivalent?
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
CEC 220 Digital Circuit Design Boolean Algebra I Wed, Sept 2 CEC 220 Digital Circuit Design Slide 1 of 13.
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
CPS120: Introduction to Computer Science Decision Making in Programs.
CSI 3125, Preliminaries, page 1 Control Statements.
Fundamental Logic Gates And, Or, Not. Logic Gates: The Basics Regulate the flow of electricity within circuits to perform desired functionalities Each.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
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)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 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.
CNG 140 C Programming (Lecture set 3)
Sequence, Selection, Iteration The IF Statement
Introduction to C++ Programming Language
Selection—Making Decisions
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Control Structures.
Flow of Control.
Flow of Control.
Topics The if Statement The if-else Statement Comparing Strings
Data Types, Identifiers, and Expressions
Chapter 8 JavaScript: Control Statements, Part 2
Flow of Control.
Computer Programming Basics
Flow of Control.
Controlling Program Flow
Presentation transcript:

Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1

Introduction Here we would discuss various constructs of C language which would be used to control the flow of the program. “Control flow” is the order in which statements are executed. SACS Programming Fundamentals Workshop 2

Flow of Control There are three flow of control in the programming languages: –The sequential control flow –The conditional control flow –The iteration control flow We will be discussing the Conditional Control Flow in this lecture today. SACS Programming Fundamentals Workshop 3

What is a Condition??? What is the value of a conditional expression??? How can we facilitate the conditional expression with Boolean operators??? Use of Flow Charts… What is short circuiting??? SACS Programming Fundamentals Workshop 4

Condition In parentheses is a condition, also called a “logical” or “Boolean” expression. Made up of variables, constants, arithmetic expressions, and the relational operators: Math symbols:, , =,  in C:, >=, ==, != SACS Programming Fundamentals Workshop 5

Truth Tables for the Conditional Operators pqp && q TTT TFF FTF FFF pqp || q TTT TFT FTT FFF SACS Programming Fundamentals Workshop 6

The ‘if’ statement Performs an action if a condition is true. The condition, which is a C expression, evaluates to zero (false) or nonzero (true). Syntax: if (conditional expression){ statement; } condition statement TrueFalse SACS Programming Fundamentals Workshop 7

The ‘if-else’ Statement Perform if-action if a condition is true. Otherwise, perform else-action. Syntax: if (conditional expression){ statement 1 } else{ statement 2 } condition statement 1 True False SACS Programming Fundamentals Workshop 8

The ‘else-if’ Statement You can connect conditional constructs to form longer sequences of conditional tests: Syntax: if(conditional expression 1 ){ statement 1 } else if (expression 2 ){ statement 2 } else if (expression 3 ){ statement 3 } else{ statement 4 } SACS Programming Fundamentals Workshop 9

Points to remember… An else is associated with the closest unassociated if. if (expression 1 ){ if (expression 2 ) statement 2 else statement 3 } if (expression1) { if (expression2) statement2 else statement3 } if (expression1) { if (expression2) statement2 } else statement3 Correct Interpretation Just as parentheses modify the order of evaluation of expressions... braces modify how statements are executed. SACS Programming Fundamentals Workshop 10

The ‘Switch’ Statement Some of the confusions in the Switch keywords: – Switch – Case – Break – Default SACS Programming Fundamentals Workshop 11

The ‘switch’ Statement Performs actions based on a series of tests of the same variable. Form: switch ( conditional expression ) { case const-expr : statements default: statements } The break statement causes an immediate exit from the switch. Because cases serve only as labels, execution falls through to the next unless there is explicit action to escape. case 1...; break; TrueFalse...; break; TrueFalse...; break; TrueFalse case 2 case 3 SACS Programming Fundamentals Workshop 12

Any Questions??? SACS Programming Fundamentals Workshop 13