Fundamentals of Programming Session 9

Similar documents
بسم اهلل الرمحن الرحيم

Chapter 4 C Program Control

Fundamentals of Programming Session 7

Chapter 4 C Program Control

Fundamentals of Programming Session 12

Fundamentals of Programming Session 4

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming

Fundamentals of Programming Session 20

Chapter 2 - Control Structures

Introduction to Programming

IS 0020 Program Design and Software Tools

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Programming for Engineers Iteration

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

Fundamentals of Programming Session 15

Fundamentals of Programming Session 13

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Fundamentals of Programming Session 20

Control Statements: Part Pearson Education, Inc. All rights reserved.

Fundamentals of Programming Session 24

Fundamentals of Programming Session 25

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Fundamentals of Programming Session 19

Fundamentals of Programming Session 8

Control Statements. Musa M. Ameen Computer Engineering Dept.

Chapter 3 Structured Program Development

Structured Program Development in C

Fundamentals of Programming Session 19

Conditional Statement

Introduction to Programming

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 2 - Control Structures

Chapter 2 - Control Structures

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Introduction. C provides two styles of flow control:

Fundamental of Programming (C)

Fundamentals of Programming Session 14

Fundamentals of Programming Session 23

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Fundamentals of Programming

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Lecture 7 Tao Wang 1

CS110D: PROGRAMMING LANGUAGE I

Visual Basic 2010 How to Program by Pearson Education, Inc. All Rights Reserved.

Programming Logic and Design Sixth Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

DECISION CONTROL AND LOOPING STATEMENTS

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Fundamentals of Programming. Lecture 6: Structured Development (part one)

5. Selection: If and Switch Controls

C: How to Program. Week /Mar/05

Chapter 4. Flow of Control

CONDITIONAL EXECUTION

Prepared by: Shraddha Modi

Repetition Structures

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 3. Selections

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 2, Part III Arithmetic Operators and Decision Making

Selection Statements. Pseudocode

Lecture 5 Tao Wang 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Flow Control. CSC215 Lecture

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Chapter 4: Basic C Operators

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

BIL101E: Introduction to Computers and Information systems Lecture 8

Conditional Statement

Conditionals and Loops

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Operators & Expressions

COMP 208 Computers in Engineering

At the end of this module, the student should be able to:

LESSON 3 CONTROL STRUCTURES

Fundamentals of Programming Session 2

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

PLD Semester Exam Study Guide Dec. 2018

A Look Back at Arithmetic Operators: the Increment and Decrement

Checking Multiple Conditions

Conditions, logical expressions, and selection. Introduction to control structures

Flow Charts. Visual Depiction of Logic Flow

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Language Reference Manual simplicity

CS201 Some Important Definitions

Transcription:

Fundamentals of Programming Session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology

Outlines do while Repetition Statement break and continue Statements Logical Operators Structured Programming Summary 2

do while Repetition Statement 3 The do while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition is tested at the beginning of the loop before the body of the loop is performed. The do while statement tests the loop-continuation condition after the loop body is performed. Therefore, the loop body will be executed at least once. When a do while terminates, execution continues with the statement after the while clause.

4 do while Repetition Statement

5 do while Repetition Statement

break and continue Statements The break and continue statements are used to alter the flow of control. The break statement, when executed in a while, for, do while or switch statement, causes an immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement (as in Fig. 4.7). 6

break and continue Statements Figure 4.11 demonstrates the break statement in a for repetition statement. When the if statement detects that x has become 5, break is executed. This terminates the for statement, and the program continues with the printf after the for. The loop fully executes only four times. 7

8 break and continue Statements

9 break and continue Statements

break and continue Statements In while and do while statements, the loopcontinuation test is evaluated immediately after the continue statement is executed. In the for statement, the increment expression is executed, then the loop-continuation test is evaluated. 10

break and continue Statements 11 Earlier, we said that the while statement could be used in most cases to represent the for statement. The one exception occurs when the increment expression in the while statement follows the continue statement. In this case, the increment is not executed before the repetition-continuation condition is tested, and the while does not execute in the same manner as the for. Figure 4.12 uses the continue statement in a for statement to skip the printf statement and begin the next iteration of the loop.

12 break and continue Statements

13 break and continue Statements

Question 1 What is the output of the following code? int i; for(i=1;i<=10;i++) { if (i==5) continue; printf("%d \n", i); } printf("end\n"); 14 Answer: 1 2 3 4 6 7 8 9 10 End

Question 2 What is the output of the following code? int i; i=1; while (i<=10) { if (i==5) continue; printf("%d \n", i); i++; } printf("\nend\n"); 15 Answer: 1 2 3 4

Logical Operators 16 C provides logical operators that may be used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), (logical OR) and! (logical NOT also called logical negation). We ll consider examples of each of these operators. Suppose we wish to ensure that two conditions are both true before we choose a certain path of execution.

Logical Operators In this case, we can use the logical operator && as follows: if ( gender == 1 && age >= 65 ) ++seniorfemales; The table shows all four possible combinations of zero (false) and nonzero (true) values for expression1 and expression2. Such tables are often called truth tables. 17

18 Logical Operators

Logical Operators Now let s consider the (logical OR) operator. Suppose we wish to ensure at some point in a program that either or both of two conditions are true before we choose a certain path of execution. In this case, we use the operator as in the following program segment if ( semesteraverage >= 90 finalexam >= 90 ) printf( "Student grade is A\n" ); 19

Logical Operators The && operator has a higher precedence than. Both operators associate from left to right. An expression containing && or operators is evaluated only until truth or falsehood is known. Thus, evaluation of the condition gender == 1 && age >= 65 will stop if gender is not equal to 1 (i.e., the entire expression is false), and continue if gender is equal to 1 (i.e., the entire expression could still be true if age >= 65). 20

Question 3 What is the output of the following code? int a=3, b=4, c=4; if (c==3 b==4 && a==3) printf("%d", (c>=b>=a? 100 : 200)); Answer: 200 21

Logical Operators C provides! (logical negation) to enable a programmer to reverse the meaning of a condition. Unlike operators && and, which combine two conditions (and are therefore binary operators), the logical negation operator has only a single condition as an operand (and is therefore a unary operator). The logical negation operator is placed before a condition when we re interested in choosing a path of execution if the original condition (without the logical negation operator) is false, such as in the following program segment: if (!( grade == sentinelvalue ) ) printf( "The next grade is %f\n", grade ); 22 The parentheses around the condition grade == sentinelvalue are needed because the logical negation operator has a higher precedence than the equality operator.

Logical Operators In most cases, you can avoid using logical negation by expressing the condition differently with an appropriate relational operator. For example, the preceding statement may also be written as follows: if ( grade!= sentinelvalue ) printf( "The next grade is %f\n", grade ); 23

Logical Operators 24 There is one type of error that C programmers, no matter how experienced, tend to make so frequently that we felt it was worth a separate section. That error is accidentally swapping the operators == (equality) and = (assignment). What makes these swaps so damaging is the fact that they do not ordinarily cause compilation errors. Rather, statements with these errors ordinarily compile correctly, allowing programs to run to completion while likely generating incorrect results through runtime logic errors.

Logical Operators For example, suppose we intend to write if ( paycode == 4 ) printf( "You get a bonus!" ); but we accidentally write if ( paycode = 4 ) printf( "You get a bonus!" ); The first if statement properly awards a bonus to the person whose paycode is equal to 4. 25 The second if statement the one with the error evaluates the assignment expression in the if condition.

26 Logical Operators

Logical Operators The other side of the coin can be equally unpleasant. Suppose you want to assign a value to a variable with a simple statement like x = 1; but instead write x == 1; Here, too, this is not a syntax error. Rather the compiler simply evaluates the conditional expression. 27

Question 4 What is the output of the following code? int i = 1, j = 1; for(; j; printf("%d%d\t", i, j)) j = i++ <= 4; Answer: 21 31 41 51 60 28

Structured Programming Summary Figure 4.17 summarizes the control statements discussed in Chapters 3 and 4. Small circles are used in the figure to indicate the single entry point and the single exit point of each statement. Connecting individual flowchart symbols arbitrarily can lead to unstructured programs. For simplicity, only single-entry/single-exit control statements are used there is only one way to enter and only one way to exit each control statement. 29

30 Structured Programming Summary

31 Structured Programming Summary

32 Structured Programming Summary

Structured Programming Summary Figure 4.18 shows the rules for forming structured programs. Applying the rules of Fig. 4.18 always results in a structured flowchart with a neat, building-block appearance. Notice that Rule 2 generates a stack of control statements; so we call Rule 2 the stacking rule. Rule 3 is called the nesting rule. Repeatedly applying Rule 3 to the simplest flowchart results in a flowchart with neatly nested control statements. 33

Structured Programming Summary Rule 4 generates larger, more involved, and more deeply nested structures. The flowcharts that emerge from applying the rules in Fig. 4.18 constitute the set of all possible structured flowcharts and hence the set of all possible structured programs. 34

35 Structured Programming Summary

36 Structured Programming Summary

37 Structured Programming Summary