1 Chapter 4 Simple Selections and Repetitions. 2 4.1 INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Lecture 2 Introduction to C Programming
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Program Design and Development
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Chapter 8 Arrays and Strings
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
UNIT II Decision Making And Branching Decision Making And Looping
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture Set 5 Control Structures Part A - Decisions Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CPS120: Introduction to Computer Science Decision Making in Programs.
sequence of execution of high-level statements
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Control Structures sequence of execution of high-level statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
- Standard C Statements
EGR 2261 Unit 4 Control Structures I: Selection
Programming Fundamentals
Control Structures – Selection
Chapter 4: Control Structures I (Selection)
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 3: Introduction to Problem Solving and Control Statements
Relational and Logical Operators
3 Control Statements:.
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Relational and Logical Operators
Basic Programming Concepts
Relational and Logical Operators
Dale Roberts, Lecturer IUPUI
Relational and Logical Operators
REPETITION Why Repetition?
Presentation transcript:

1 Chapter 4 Simple Selections and Repetitions

2 4.1 INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make decisions as to what code to execute and when (that is, selection) and the ability to execute certain portions.

3 4.2 PROGRAMMING FOR SIMPLE SELECTIONS if gross income is greater than $10,000 compute city_tax = * gross_income else set city_tax to 0 end_if

4 Two-Way Selection Using the if Statement selection expression must be enclosed in parentheses. in C a nonzero value is assigned to a predicate if it is true ; otherwise, its value will be zero. C considers any nonzero value compute for the selection expression in if statements to correspond to the Boolean true and the value zero to correspond to the Boolean false,

5 Using Relational Operators in Conditional Expressions 1.Equal to, = = 2.Not equal to, != 3.Less than, < 4.Less than or equal to, <= 5.Greater than, > 6.Greater than or equal to, >=

6 if (employee_status == ‘f’) printf(“%d” is full-time.”, employee_number); else printf(“%d” id part-time.”, employee_number); /* end if */

7 Precedence Level Operator Associativity 1 (highest)( )left 2 Unary + unary - right 3* /left 4+ -left 5 >= left 6= = !=left 7 (lowest)=right

8 Using Strings in Conditional Expressions string2=string1; - will not work - The reason is that the names of string variables declared as character arrays are the addresses of the memory locations where they are stored. - program must contain #include - function strcpy to copy one string into another

9 Using Strings in Conditional Expressions (Cont.) Example 4.11 char string1[21], string2[21]; strcpy(string2,string1); - strcmp can be used to compare two strings. - It returns an integer that is less than zero, equal to zero, or greater than zero if the first string parameter is less than, equal to, or greater than the second string parameter.

10 Using Strings in Conditional Expressions (Cont.) Example 4.12 char string1[21], string2[21]; if ( strcmp ( string1, string2 ) > 0 ) printf( “ %s is greater than %s ”, string1, string2 ); else printf( “ %s is not greater than %s ”, string1, string2 ); /* end if */

11 Nested if Statements and Multiway Selection if ( filing_status == 1 ) printf ( “Single” ); /* end if */ if ( filing_status == 2 ) printf ( “Married filing jointly” ); /* end if */ Figure 4.6 A multiway selection structure based on one variable: Use of unnested if statement

12 Nested if Statements and Multiway Selection (Cont.) if selection structure is more efficient from a program execution point of view. if ( filing_status == 1 ) printf ( “Single” ); else if ( filing_status = = 2 ) printf ( “Married filing jointly” ); /* end if */ Figure 4.6 A multiway selection structure based on one variable: Use of nested if statement

PROGRAMMING FOR CONTROLLED REPETITIONS Repetition Statements - to define controlled repetitions ( or loops ) on a program block - There are two types of repetition statements: 1. Pretest repetition statements 2. Posttest repetition statements - In C we have three statements for repetition: 1.The while statements, which is a pretest repetition statement 2. The for statement, which is also a pretest repetition statement 3. The do-while statement, which is a posttest repetition statement

14 Controlled Repetitions Using the while Statement while statement works as follows: 1. The loop control expression in parentheses is evaluated. 2. If the result of evaluation is a nonzero value, the loop body is executed. 3. If the loop control expression produces a zero value, the loop body is bypassed. while ( Expression ) Statement /* end while */

15 Controlled Repetitions Using the while Statement (Cont.) fact = 1; count = 2; while ( count <= n ) { fact = fact * count; count=count + 1; } /* end while */

STYLE CONSIDERATIONS FOR if AND while STATEMENTS The loop body for a while statement should be indented to visually emphasize the syntactical elements. While ( count < n ) { fact = fact * count; count = count + 1; } /* end while */

STRUCTURED PROGRAMMING conditional branch specified by the if and while statements. unconditional branches take the program control to another point in the program without providing a return to the point of branching. goto Label ; Label : Statement ;

STRUCTURED PROGRAMMING (Cont.) A new discipline called structured programming. Any program can be written using only three basic control structure: sequence, selection, and repetition. Unconditional branching and the goto statement are not essential control structure in programming.