Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
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.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
CONTROLLING PROGRAM FLOW
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 4 – C Program Control
JavaScript: Control Statements I
JavaScript: Control Statements.
JavaScript: Control Statements I
Loop Control Structure.
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
CSC215 Lecture Control Flow.
Chapter 6 Control Statements: Part 2
Week 6 CPS125.
2.6 The if/else Selection Structure
Week 6 CPS125.
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Control Structures Sequence Structure Selection Structure Want your programs to make decisions regarding which calculations to perform Repetition Structure May want to repeat calculations without writing the statement over and over. This is called looping.

4.1 Relational and Logical Operations Relational Operators and Relational Expressions Logical Operator and Logical Expressions

Relational Operators C contains six relational operators: < less than < = less than or equal to = = equal to > greater than > = greater than or equal to ! = not equal to

4.2 Selection Structures Two-Way Selection Structures Two-Way Selection Structures Compound Conditions Compound Conditions Multiway Structures Multiway Structures

Conditional Operator How does the ? : conditional operator work? How does the ? : conditional operator work? The ? : operator requires three operands. The ? : operator requires three operands. expression1 ? expression2 : expression3 expression1 ? expression2 : expression3 If expression1 is true, expression2 is executed. If expression1 is true, expression2 is executed. If expression1 is false, expression3 is executed. If expression1 is false, expression3 is executed. Can be used to find the smaller of two numbers. Can be used to find the smaller of two numbers. x = (y < z) ? y : z x = (y < z) ? y : z Assigns x the value of the smaller of y and z. Assigns x the value of the smaller of y and z. Statements using the ? : operator are good shorthand for longer if-else type control structures. Statements using the ? : operator are good shorthand for longer if-else type control structures.

Logical Expressions The results of a logical expression (using the symbols A and B to indicate relational expressions) A B A && B A | | B !A !B T T T T F F T F F T F T F T F T T F F F F F T T

Precedence of Operators What are the precedence and associativity of logical, relational, and arithmetic operators? Operator Name Associativity Precedence () parentheses L to R 1 (highest) ++, -- post-increment L to R 2 ++, -- pre-increment R to L 2 ! Logical NOT L to R 3 +, - Positive, negative sign L to R 3 +=,-=,*=,/=,%= compound assignment R to L 3 *, / multiplication, division L to R 4 +, - Addition, subtraction L to R 5 ==,>=,,<,!= relational operator L to R 6 && Logical AND L to R 7 || Logical OR L to R 8 = Assignment R to L 9 (lowest)

Precedence of Operators x = (a>b || b>c && a==b) x = ( a>b || b>c && a==b ) (6) x = ((a>b)) || (b>c) && (a==b)) x = ((4>-2) || (-2>0) && 4==-2) x = ( True || False && False ) (7) x = ( True || False ) (8) x = ( True )

Switch Control Structures What does the switch statement do? Commonly is constructed similarly to the if-else-if control structure. Transfer control Syntax switch(expression) { case constant1: statement1a statement1b … case constant2: statement2a statement2b … default: statements }

4.3 Repetition Structures Iterative Loops Iterative Loops Nested Iterative Loops Nested Iterative Loops Conditional Loops Conditional Loops

While Loop: Part I Topics Topics Using while loops Using while loops A test condition part A test condition part An execution part An execution part C provides a number of iterative control structures, known like looping. C provides a number of iterative control structures, known like looping. The repeated execution of one or more statements. The repeated execution of one or more statements.

While Loop The structure of a C while loop: The structure of a C while loop: while (expression) while (expression) { statement1 statement1 statement2 statement2 … } The expression is a relational expression (variable, constant, or an arithmetic expression) that results in either True or False. The expression is a relational expression (variable, constant, or an arithmetic expression) that results in either True or False. If the result is true, the statements between the braces are executed If the result is true, the statements between the braces are executed

A Generic Illustration Code before loop {Block of Statements} Loop keyword Code after loop yes No

Source Code #include #include void main(void) void main(void) { { int i; int i; i = 1; i = 1; while (i<= 5 ) while (i<= 5 ) { { printf (" Loop number %d in the while loop\n", i); printf (" Loop number %d in the while loop\n", i); i++; i++; } } } Test expression Incrementing counter variable Statement block is repeatedly executed until test expression becomes false.

Do-While Loops Topics Topics Using do-while loops Using do-while loops Differences between do-while loops and while loops Differences between do-while loops and while loops What is the structure of a do-while loop? What is the structure of a do-while loop?

Do-while Loops What is the structure of a do-while loop? What is the structure of a do-while loop? do statement while (expression); do statement while (expression);or do do { statement1; statement1; statement2; statement2; … } while (expression); while (expression);

Do-while Loops The statements between the braces are executed at least once regardless of whether the expression is True or False. The statements between the braces are executed at least once regardless of whether the expression is True or False. If the expression is True, the statements are executed again. If the expression is True, the statements are executed again. What are the differences between do-while loops and while loops? What are the differences between do-while loops and while loops? In the while loops, the test expression is tested first, and, if the test result is false, the loop body is not executed. In the while loops, the test expression is tested first, and, if the test result is false, the loop body is not executed. In the do-while loops, the loop body is executed once. After that, if the test expression is False, the loop body is not executed again. In the do-while loops, the loop body is executed once. After that, if the test expression is False, the loop body is not executed again.

void main(void) { … {…}{…} {…}{…} {…}{…} {…}{…} ……}……} do while

Source code L4_9.C /*For Lesson 4_9 */ /*For Lesson 4_9 */ #include #include void main(void) void main(void) { { int i=4, j=1; int i=4, j=1; do do { { printf("old_i=%2d, ",i); printf("old_i=%2d, ",i); i--; i--; printf("new_i=%2d\n",i); printf("new_i=%2d\n",i); } while(i); } while(i); do ++j; do ++j; while(j>999); while(j>999); printf("j=%2d\n",j); printf("j=%2d\n",j); } } Test expression Statement block is executed repeatedly until test expression becomes false Test expression is always false

Simple For Loop Topics Topics The for loop control structure The for loop control structure Structure of a simple for loop Structure of a simple for loop Difference between for loops and while loops Difference between for loops and while loops Appropriate when you know how many times the operations needs to be repeated. Appropriate when you know how many times the operations needs to be repeated.

Simple For Loop What is a for loop? What is a for loop? The simplest for loop The simplest for loop for (loop expressions) for (loop expressions) single statement for_loop body; single statement for_loop body; for (day = 1; day <= 3; day++) for (day = 1; day <= 3; day++) printf( “ Day = %2d\n ”, day); printf( “ Day = %2d\n ”, day); The loop expression must consist three parts: The loop expression must consist three parts: An initialization expression An initialization expression A loop repetition condition A loop repetition condition An increment expression An increment expression

Simple For Loop The general structure of a for loop The general structure of a for loop for (loop_expression) for (loop_expression) { for_loop_body for_loop_body } loop_body Example loop_body Example { minutes = 60 * hour; minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); printf("Hour = %2d, Minutes=%3d\n",hour, minutes); }

Simple For Loop Difference between for loops and while loops Difference between for loops and while loops Item for loop while loop Initialization one of the loop expressions given prior to the loop Test expression one of the loop expressions one of the loop expressions Increment expression one of the loop expressions must be in the loop body The number of iteration convenient and clear less convenient and clear Is know The number of iteration less convenient and clear more convenient and clear Is unknow than for loop

/*For Lesson 4_10 */ /*For Lesson 4_10 */ #include #include void main(void) void main(void) { int day, hour, minutes; int day, hour, minutes; for(day=1; day<=3; day++) for(day=1; day<=3; day++) printf("Day=%2d\n", day); printf("Day=%2d\n", day); for (hour=5; hour>2; hour--) for (hour=5; hour>2; hour--) { { minutes = 60 * hour; minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); printf("Hour = %2d, Minutes=%3d\n",hour, minutes); } } } initialization Test expression “Increment” expression Body of for loop

void main(void) { … Repetition condition ……}……} An increment expression for initialization No yes

Nested For Loops The syntax of a nested for loop The syntax of a nested for loop for (loop_1_expressions) for (loop_1_expressions) { loop_body_1a /* optional loop_body for outer loop */ loop_body_1a /* optional loop_body for outer loop */ for (loop_2_expressions) /* this is an inner loop */ for (loop_2_expressions) /* this is an inner loop */ { /* start inner loop */ { /* start inner loop */ loop_body_2 /* loop_body for inner loop */ loop_body_2 /* loop_body for inner loop */ } /* end of inner loop */ } /* end of inner loop */ loop_body_1b /* optional loop_body for outer loop */ loop_body_1b /* optional loop_body for outer loop */ } /* end of outer loop */ } /* end of outer loop */

Nested For Loops How to make the code easy to read? How to make the code easy to read? Indentation Indentation for ( … ) for ( … ) { … … { … … } … … }

#include #include void main(void) void main(void) { { int i, j, k, m=0; int i, j, k, m=0; for (i=1; i<=5; i+=2) for (i=1; i<=5; i+=2) { { for (j=1; j<=4; j++) for (j=1; j<=4; j++) { { k = i+j;; k = i+j;; printf("i=%3d, j=%3d, k=%3d\n", i, j, k); printf("i=%3d, j=%3d, k=%3d\n", i, j, k); } } m=k+i; m=k+i; } } Inner loop Outer loop

4.4 Stacking and Nesting of Control Structures Control Structure Stacking Nested Control Structures

4.5 Sample Problems and Programs Impedance and Inductance of an Electrical Coil Impedance and Inductance of an Electrical Coil Altitude of a Projectile Altitude of a Projectile Shear Stress of a Metallic Member Shear Stress of a Metallic Member Table of Periods of Pendulum Table of Periods of Pendulum Compression Stress and Strain in Steel Rods Compression Stress and Strain in Steel Rods Types of Triangles Types of Triangles