1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University

Slides:



Advertisements
Similar presentations
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Advertisements

ICS103: Programming in C 4: Selection Structures Muhamed F. Mudawar.
Chapter 4 Selection Structures: if and switch Statements Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering.
Week 4: Control Structures - Repetition
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.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
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 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Visual C++ Programming: Concepts and Projects
1 ICS103 Programming in C Lecture 6: Selection Structures.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
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.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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)
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Visual Basic Programming
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
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.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter#3 Part1 Structured Program Development in C++
chap4 Chapter 4 Selection Structures: if and switch Statements.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Department of CSE Control Structures: Selection.
CPS120: Introduction to Computer Science Decision Making in Programs.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
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
Selection—Making Decisions
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
SELECTIONS STATEMENTS
Topics discussed in this section:
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Computer Programming Basics
Selection—Making Decisions
The Selection Structure
ICS103: Programming in C 4: Selection Structures
Control Structures.
Presentation transcript:

1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University Spring 2009

2015/10/16 Lecture 4 Selection Structures: if and switch Statements

2015/10/16 3 Objectives Control Structure Conditions The if Statement The switch Statement

2015/10/16 4 Control Structures Control Structures control the flow of execution Three kinds of execution flow: Sequence The execution of the program is sequential Selection A control structure which chooses alternative to execute Repetition A control structure which repeats a group of statements. We will focus on the selection control structure in this lecture.

2015/10/16 5 Conditions A program may choose among alternative statements by testing the value of key variables. if (grade > 60) printf(“Passed!”) Condition is an expression that is either false (represented by 0) or true (represented by 1). grade > 60 Conditions may contain relational or equality operators.

2015/10/16 6 Relational and Equality Operators

2015/10/16 7 Sample Conditions

2015/10/16 8 Logical Operators To form more complicated conditions by the three logical operators && (and) || (or) ! (not) Logical Expressions is an expression which uses one or more logical operators, e.g., salary 5 temperature > 90.0 && humidity > 0.9 !(0 <= n && n<= 100)

2015/10/16 9 && (and), || (or), ! (not)

2015/10/16 10 Operator Precedence An operator ’ s precedence determines its order of evaluation. - x – y * z x + y < min + max

2015/10/16 11 Evaluation Tree & Step-by-Step Evaluation

2015/10/16 12 Short Circuit Evaluation Stopping evaluation of a logical expression as soon as its value can be determined. e.g. a && b must be false if a is 0.

2015/10/16 13 Writing English Conditions in C

2015/10/16 14 Comparing Characters

2015/10/16 15 Complementing a condition Complement a logical expression with the symbol ! just change its operator item == SENT !(item == SENT) item != SENT !( a <= b) a > b

2015/10/16 16 DeMorgan ’ s Theorem A way to simplify the logical expression If comp_1 is the complement of expr_1 The complement of expr_1 && expr_2 is comp_1 || comp_2 The complement of expr_1 || expr_2 is comp_1 && comp_2. e.g. the complement of age > 25 && (status == ‘S’|| status == ‘D’) age <= 25 ||(status != ‘S’&& status!= ‘D’)

2015/10/16 17 The if statement with 1 or 2 alternatives :: Flowchart

2015/10/16 18 The if statement with 1 or 2 alternatives Syntax if (condition) statement ; else statement ; Ex-1. if (rest_heart_rate > 56 ) printf(“keep up your exercise program!\n”); else printf(“Your heart rate is in excellent health\n”); Ex-2. if (x != 0.0) product = product * x;

2015/10/16 19 Example Write a C program that reads in a number N from the user and then displays its parity (odd or even)

2015/10/16 20 If statement with compound statements if (condition) { true task } else { false task }

2015/10/16 21 Example Write a C program that determines the maximum value in a list of three numbers entered by the user.

2015/10/16 22 Nested if Statements An if statement with another if statement as its true task or its false task

2015/10/16 23 Nested if Example: Road Sign Decision Process

2015/10/16 24 Road Sign Decision Process

2015/10/16 25 Dangling Else Which if is associated with the else? if (x > 0) if (y > 0) a = a + 1; else b = b + 1;

2015/10/16 26 Multiple-Alternative Decision

2015/10/16 27 Order of Conditions in a Multiple-Alternative Decision When more than one condition in a multiple- alternative decision is true, only the first task following the first true condition executes. Textbook examples

2015/10/16 28 Example Write a C program that reads in a grade on a scale of 100 and displays the equivalent letter grade (A, B, C, D or F)

2015/10/16 29 Example Write a C program that asks a user about his or her gender and height and then decides if the user is Short, Medium or Tall based on the decision tree shown below:

2015/10/16 30 Example Write a C Program that reads in three integer numbers; Num1, Num2 and Num3 and displays the list of entered numbers in ascending order. For example, if the user enters the following list of integers: Your program should display the following: The ordered list is:

2015/10/16 31 The switch statement When the selection is based on the value of a single variable or of a simple expression (called the controlling expression).

2015/10/16 32 Example of a switch Statement

2015/10/16 33 Example Write a program using the switch statement to simulate a calculator that supports the following arithmetic operations: + - * /. You program should start by asking the user to select an arithmetic operation and the two double operands, calculates and then displays the result.

2015/10/16 34 Summary Control Structure Conditions The if Statements The switch Statements