SlideShare a Scribd company logo
Control 
Statements (2) 
Lecture 4 
Dr. Hakem Beitollahi 
Computer Engineering Department 
Soran University
Objectives of this lecture 
 In this chapter you will learn: 
 Iteration statements (Loop statements) 
 for repetition statement 
 while statement 
 do…while repetition statement 
 break and continue statements 
Control Statements (2)— 2
The for loop 
Control Statements (2)— 3
For repetition statement (I) 
 General form of the for statement 
 for ( initialization; loopContinuationCondition; increment or decrement) 
statement; 
 The initialization is an assignment statement that is used to set the loop 
control variable. 
 The condition is a relational expression that determines 
when the loop exits. 
 The increment/decrement defines how the loop control 
variable changes each time the loop is repeated. 
 You must separate these three major sections by 
semicolons. 
 The for loop continues to execute as long as the 
condition is true. 
 Once the condition becomes false, program execution 
resumes on the statement following the for. 
Control Statements (2)— 4
For repetition statement (II) 
 for statement examples 
 Vary control variable from 1 to 100 in increments of 1 
o for ( int i = 1; i <= 100; i++ ) 
 Vary control variable from 100 to 1 in increments of -1 
o for ( int i = 100; i >= 1; i-- ) 
 Vary control variable from 7 to 77 in steps of 7 
o for ( int i = 7; i <= 77; i += 7 ) 
 Vary control variable from 20 to 2 in steps of -2 
o for ( int i = 20; i >= 2; i -= 2 ) 
 Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 
o for ( int i = 2; i <= 20; i += 3 ) 
 Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 
33, 22, 11, 0 
o for ( int i = 99; i >= 0; i -= 11 ) 
Control Statements (2)— 5
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i 0 
Control Statements (2)— 6
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i 0 
Control Statements (2)— 7
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i 0 
Control Statements (2)— 8
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i 0 
Control Statements (2)— 9
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i 1 
Control Statements (2)— 10
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i 1 
Control Statements (2)— 11
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i 1 
Control Statements (2)— 12
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i 1 
Control Statements (2)— 13
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i 2 
Control Statements (2)— 14
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i 2 
Control Statements (2)— 15
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i is 2 
i 2 
Control Statements (2)— 16
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i is 2 
i 2 
Control Statements (2)— 17
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i is 2 
i 3 
Control Statements (2)— 18
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i is 2 
i 3 
Control Statements (2)— 19
Execution Trace 
for (int i = 0; i < 3; ++i) { 
Console.WriteLine("i is“+i); 
} 
Console.WriteLine("all done“); 
i is 0 
i is 1 
i is 2 
all done 
i 3 
1 
Control Statements (2)— 20
for repetition statement (III) 
 Example 
Control Statements (2)— 21
for repetition statement (IV) 
 In for loops, the conditional test is always 
performed at the top of the loop. 
 This means that the code inside the loop 
may not be executed at all if the condition 
is false to begin with. 
Control Statements (2)— 22
for repetition statement (V) 
 for Loop Variations 
 One of the most common variations uses the 
comma operator to allow two or more 
variables to control the loop. 
 Example: 
Control Statements (2)— 23
for repetition statement (VI) 
 The Infinite Loop with for 
 for loop is traditionally used for this purpose 
 Since none of the three expressions that form 
the for loop are required, you can make an 
endless loop by leaving the conditional 
expression empty: 
Control Statements (2)— 24
for repetition statement (VII) 
 You can exit from an infinite using break 
statement 
 Example 
Control Statements (2)— 25
for repetition statement (VIII) 
 for Loops with no Bodies 
 A statement may be empty 
 This means that the body of the for loop (or 
any other loop) may also be empty. 
 You can use this fact to improve the efficiency 
of certain algorithms and to create time delay 
loops. 
 Example 
for(i=0; i<10000; i++); 
Control Statements (2)— 26
Good Programming Practice 5.1 
 Control counting loops with integer values. 
 Place a blank line before and after each 
major control structure to make it stand out 
in the program. 
Control Statements (2)— 27
Common Programming Error 5.1 
 Floating-point values may be approximate, 
so controlling counting loops with floating-point 
variables can result in imprecise 
counter values and inaccurate tests for 
termination. 
Control Statements (2)— 28
Common Programming Error 5.3 
 When a for structure declares its control 
variable in the initialization section of the 
for structure header, using the control 
variable after the for structure’s body is a 
compiler error. 
 Example: 
Control Statements (2)— 29
Common Programming Error 5.4 
 Using commas in a for structure header 
instead of the two required semicolons 
is a syntax error. 
 Placing a semicolon immediately to the 
right of a for structure header’s right 
parenthesis makes the body of that for 
structure an empty statement. This is 
normally a logic error. 
Control Statements (2)— 30
Other math operations in for portions 
 The initialization, loop-continuation 
condition and increment or decrement 
portions of a for structure can contain 
arithmetic expressions. 
 Assume x = 2 and y = 10; 
 is equivalent to the statement 
Control Statements (2)— 31
Common Programming Error 5.6 
 Not using the proper relational operator in 
the loop-continuation condition of a loop 
that counts downward (e.g., using i <= 1 in 
a loop counting down to 1) is usually a 
logic error that will yield incorrect results 
when the program runs. 
Control Statements (2)— 32
The while loop 
Control Statements (2)— 33
The while loop (I) 
 The second popular loop available in C/C++/C# 
is the while loop. 
 The general form is 
while(condition) 
statement; 
 Example 
Control Statements (2)— 34
Example: calculate an average 
Control Statements (2)— 35
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
Control Statements (2)— 36
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 0 
Control Statements (2)— 37
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 0 
sum 0 
Control Statements (2)— 38
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 0 
sum 0 
Control Statements (2)— 39
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 0 
sum 0 
value -- 
Control Statements (2)— 40
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 0 
sum 0 
value 1 
Control Statements (2)— 41
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
42/77 
numberProcessed 0 
sum 1 
value 1
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
43/77 
numberProcessed 1 
sum 1 
value 1
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 1 
sum 1 
value 1 
Control Statements (2)— 44
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 1 
sum 1 
value 1 
Control Statements (2)— 45
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 1 
sum 1 
value 5 
Control Statements (2)— 46
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 1 
sum 6 
value 5 
Control Statements (2)— 47
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 2 
sum 6 
value 5 
Control Statements (2)— 48
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 2 
sum 6 
value 5 
Control Statements (2)— 49
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 2 
sum 6 
value 5 
Control Statements (2)— 50
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 2 
sum 6 
value 3 
Control Statements (2)— 51
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 2 
sum 9 
value 3 
Control Statements (2)— 52
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 3 
sum 9 
value 3 
Control Statements (2)— 53
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 3 
sum 9 
value 3 
Control Statements (2)— 54
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 3 
sum 9 
value 3 
Control Statements (2)— 55
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 3 
sum 9 
value 1 
Control Statements (2)— 56
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 3 
sum 10 
value 1 
Control Statements (2)— 57
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 4 
sum 10 
value 1 
Control Statements (2)— 58
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 4 
sum 10 
value 1 
Control Statements (2)— 59
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 4 
sum 10 
value 1 
average 2.5 
Control Statements (2)— 60
Execution Trace Suppose input contains: 1 5 3 1 6 
listSize 4 
numberProcessed 4 
sum 10 
value 1 
average 2.5 
Control Statements (2)— 61
Another Example with while loop 
Class Math include all mathematic functions. 
Pow(a,b) = ab 
Initialize counter variable x to 1 
The condition is true (x=1<=10) 
Continue looping as long as x’s value 
is less than or equal to 10 
Increment x by 1, which 
causes x to exceed 10 
eventually 
Control Statements (2)— 62
While loop 
Control Statements (2)— 63
Common Programming Error 
 Forgetting initializing the control value of 
the while loop is a logical error. 
Example: 
Control Statements (2)— 64
The do-while loop 
Control Statements (2)— 65
The do-while loop (I) 
 Unlike for and while loops, which test the loop 
condition at the top of the loop, the do-while loop 
checks its condition at the bottom of the loop 
 This means that a do-while loop always executes 
at least once. 
 The general form of the do-while loop is 
do { 
statement; 
} while(condition); 
Control Statements (2)— 66
The do-while loop (II) 
Declare and initialize 
control variable counter 
do…while loop displays counter’s 
value before testing for counter’s 
Control Statements (2)— 67
Action 
true 
Expression 
false 
Control Statements (2)— 68 
The do-while loop (III)
Loop iterations 
 Declaring Variables within Selection and Iteration 
Statements 
 it is possible to declare a variable within if, switch, for, 
while, do..while loops 
 A variable declared in one of these places has its 
scope limited to the block of code controlled by that 
statement 
/* i is local to for loop; j is known outside 
loop. */ 
int j; 
for(int i = 0; i<10; i++) 
j = i * i; 
/* i = 10; // *** Error *** -- i not known here! 
*/ 
Control Statements (2)— 69
Jump Statements 
Control Statements (2)— 70
 The return statement 
 The break statement 
 The exit() statement 
 The continue statement 
Control Statements (2)— 71
The return statement 
 The return statement is used to return from a 
function 
 It is categorized as a jump statement because it 
causes execution to return (jump back) to the 
point at which the call to the function was made. 
Control Statements (2)— 72
The break Statement (I) 
 Causes immediate exit from control 
structure 
 Used in while, for, do…while or switch 
statements 
 The break statement has two uses: 
 You can use it to terminate a case in the 
switch statement 
 You can also use it to force immediate 
termination of a loop, bypassing the normal 
loop conditional test 
Control Statements (2)— 73
prints the numbers 0 through 10 on the screen. Then the loop terminates because break 
causes immediate exit from the loop, overriding the conditional test t<100. 
Control Statements (2)— 74
Programming Tip 
Programmers often use the break statement 
in loops in which a special condition can 
cause immediate termination. 
Control Statements (2)— 75
The exit() Function (I) 
Control Statements (2)— 76
Control Statements (2)— 77
The continue Statement (I) 
 Instead of forcing termination, however, continue forces 
the next iteration of the loop to take place, skipping any 
code in between. 
 For the for loop, continue causes the conditional test 
and increment portions of the loop to execute. 
 For the while and do-while loops, program control 
passes to the conditional tests. 
Control Statements (2)— 78
Control Statements (2)— 79
Control Statements (2)— 80
Control Statements (2)— 81

More Related Content

What's hot

Os module 2 c
Os module 2 cOs module 2 c
Os module 2 c
Gichelle Amon
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
Ramakrishna Reddy Bijjam
 
Closed-loop step response for tuning PID fractional-order filter controllers
Closed-loop step response for tuning PID fractional-order filter controllersClosed-loop step response for tuning PID fractional-order filter controllers
Closed-loop step response for tuning PID fractional-order filter controllers
ISA Interchange
 
Sort presentation
Sort presentationSort presentation
Sort presentation
Ramakrishna Pulikonda
 
Modeling and Simulation of an Active Disturbance Rejection Controller Based o...
Modeling and Simulation of an Active Disturbance Rejection Controller Based o...Modeling and Simulation of an Active Disturbance Rejection Controller Based o...
Modeling and Simulation of an Active Disturbance Rejection Controller Based o...
IJRES Journal
 
control system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functionscontrol system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functions
nalan karunanayake
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
Control Systems Lab 2
Control Systems Lab 2Control Systems Lab 2
Control Systems Lab 2
Julia London
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
sgpraju
 

What's hot (9)

Os module 2 c
Os module 2 cOs module 2 c
Os module 2 c
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
 
Closed-loop step response for tuning PID fractional-order filter controllers
Closed-loop step response for tuning PID fractional-order filter controllersClosed-loop step response for tuning PID fractional-order filter controllers
Closed-loop step response for tuning PID fractional-order filter controllers
 
Sort presentation
Sort presentationSort presentation
Sort presentation
 
Modeling and Simulation of an Active Disturbance Rejection Controller Based o...
Modeling and Simulation of an Active Disturbance Rejection Controller Based o...Modeling and Simulation of an Active Disturbance Rejection Controller Based o...
Modeling and Simulation of an Active Disturbance Rejection Controller Based o...
 
control system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functionscontrol system Lab 01-introduction to transfer functions
control system Lab 01-introduction to transfer functions
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Control Systems Lab 2
Control Systems Lab 2Control Systems Lab 2
Control Systems Lab 2
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 

Viewers also liked

CLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and ResponsibilitiesCLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and Responsibilities
Alberto Rocha
 
Bet strikerz updated
Bet strikerz updatedBet strikerz updated
Bet strikerz updated
Ali Al-Enzi
 
Governoor
GovernoorGovernoor
Governoor
Frenki Niken
 
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
Cyber Espionage and Insider Threat: House of Commons Panel DiscussionCyber Espionage and Insider Threat: House of Commons Panel Discussion
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
Dr. Lydia Kostopoulos
 
Manual de Arborizacao Urbana
Manual de Arborizacao UrbanaManual de Arborizacao Urbana
Manual de Arborizacao Urbana
Aline Naue
 
Pengukuran aliran b.(variable area)
Pengukuran aliran b.(variable area)Pengukuran aliran b.(variable area)
Pengukuran aliran b.(variable area)
Frenki Niken
 
Sistem hydrolik
Sistem hydrolikSistem hydrolik
Sistem hydrolik
Frenki Niken
 
Craft oil interview questions and answers
Craft oil interview questions and answersCraft oil interview questions and answers
Craft oil interview questions and answers
AlanWright789
 
Leadership development model npm
Leadership development model npmLeadership development model npm
Leadership development model npm
Anna Leth Clante
 
Our numbers
Our numbersOur numbers
Our numbers
Anna Leth Clante
 
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิตความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิตประกายทิพย์ แซ่กี่
 
PDHPE 5P
PDHPE 5PPDHPE 5P
PDHPE 5P
JPeronchik
 
อะตอมและนิวเคลียส
อะตอมและนิวเคลียสอะตอมและนิวเคลียส
อะตอมและนิวเคลียส
ประกายทิพย์ แซ่กี่
 
Malware
MalwareMalware
"America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene...
"America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene..."America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene...
"America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene...
Clean Energy Canada
 

Viewers also liked (17)

CLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and ResponsibilitiesCLI, Inc. Contract Manager Roles and Responsibilities
CLI, Inc. Contract Manager Roles and Responsibilities
 
Bet strikerz updated
Bet strikerz updatedBet strikerz updated
Bet strikerz updated
 
Governoor
GovernoorGovernoor
Governoor
 
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
Cyber Espionage and Insider Threat: House of Commons Panel DiscussionCyber Espionage and Insider Threat: House of Commons Panel Discussion
Cyber Espionage and Insider Threat: House of Commons Panel Discussion
 
Manual de Arborizacao Urbana
Manual de Arborizacao UrbanaManual de Arborizacao Urbana
Manual de Arborizacao Urbana
 
Pengukuran aliran b.(variable area)
Pengukuran aliran b.(variable area)Pengukuran aliran b.(variable area)
Pengukuran aliran b.(variable area)
 
Sistem hydrolik
Sistem hydrolikSistem hydrolik
Sistem hydrolik
 
Craft oil interview questions and answers
Craft oil interview questions and answersCraft oil interview questions and answers
Craft oil interview questions and answers
 
Leadership development model npm
Leadership development model npmLeadership development model npm
Leadership development model npm
 
Our numbers
Our numbersOur numbers
Our numbers
 
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิตความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
ความรู้เบื้องต้นเกี่ยวกับสิ่งมีชีวิต
 
PDHPE 5P
PDHPE 5PPDHPE 5P
PDHPE 5P
 
อะตอมและนิวเคลียส
อะตอมและนิวเคลียสอะตอมและนิวเคลียส
อะตอมและนิวเคลียส
 
Angels
AngelsAngels
Angels
 
Malware
MalwareMalware
Malware
 
ระบบประสาท
ระบบประสาทระบบประสาท
ระบบประสาท
 
"America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene...
"America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene..."America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene...
"America's Clean Energy Maverick: How and Why Texas Grabbed the Renewable Ene...
 

Similar to Lecture 4

Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
Software Verification, Validation and Testing
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and Testing
Dr Sukhpal Singh Gill
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Loop control statements
Loop control statementsLoop control statements
Loop control statements
Jaya Kumari
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Prashant Sharma
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
JAYDEV PATEL
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
Panimalar Engineering College
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
indra Kishor
 
Chapter 1 Automatic Controloverview
Chapter 1 Automatic ControloverviewChapter 1 Automatic Controloverview
Chapter 1 Automatic Controloverview
Water Birds (Ali)
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
PM1
PM1PM1
PM1
ra na
 
Control tutorials for matlab and simulink introduction pid controller desig...
Control tutorials for matlab and simulink   introduction pid controller desig...Control tutorials for matlab and simulink   introduction pid controller desig...
Control tutorials for matlab and simulink introduction pid controller desig...
ssuser27c61e
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
ssuser10ed71
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
Sriman Sawarthia
 
Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
Student
 

Similar to Lecture 4 (20)

Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Software Verification, Validation and Testing
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and Testing
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Loop control statements
Loop control statementsLoop control statements
Loop control statements
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
Chapter 1 Automatic Controloverview
Chapter 1 Automatic ControloverviewChapter 1 Automatic Controloverview
Chapter 1 Automatic Controloverview
 
04a intro while
04a intro while04a intro while
04a intro while
 
PM1
PM1PM1
PM1
 
Control tutorials for matlab and simulink introduction pid controller desig...
Control tutorials for matlab and simulink   introduction pid controller desig...Control tutorials for matlab and simulink   introduction pid controller desig...
Control tutorials for matlab and simulink introduction pid controller desig...
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
 

More from Soran University

Lecture 9
Lecture 9Lecture 9
Lecture 9
Soran University
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Soran University
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
Soran University
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
Soran University
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
Soran University
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Soran University
 
Administrative
AdministrativeAdministrative
Administrative
Soran University
 

More from Soran University (8)

Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Administrative
AdministrativeAdministrative
Administrative
 

Recently uploaded

Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 

Recently uploaded (20)

Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 

Lecture 4

  • 1. Control Statements (2) Lecture 4 Dr. Hakem Beitollahi Computer Engineering Department Soran University
  • 2. Objectives of this lecture  In this chapter you will learn:  Iteration statements (Loop statements)  for repetition statement  while statement  do…while repetition statement  break and continue statements Control Statements (2)— 2
  • 3. The for loop Control Statements (2)— 3
  • 4. For repetition statement (I)  General form of the for statement  for ( initialization; loopContinuationCondition; increment or decrement) statement;  The initialization is an assignment statement that is used to set the loop control variable.  The condition is a relational expression that determines when the loop exits.  The increment/decrement defines how the loop control variable changes each time the loop is repeated.  You must separate these three major sections by semicolons.  The for loop continues to execute as long as the condition is true.  Once the condition becomes false, program execution resumes on the statement following the for. Control Statements (2)— 4
  • 5. For repetition statement (II)  for statement examples  Vary control variable from 1 to 100 in increments of 1 o for ( int i = 1; i <= 100; i++ )  Vary control variable from 100 to 1 in increments of -1 o for ( int i = 100; i >= 1; i-- )  Vary control variable from 7 to 77 in steps of 7 o for ( int i = 7; i <= 77; i += 7 )  Vary control variable from 20 to 2 in steps of -2 o for ( int i = 20; i >= 2; i -= 2 )  Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 o for ( int i = 2; i <= 20; i += 3 )  Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 o for ( int i = 99; i >= 0; i -= 11 ) Control Statements (2)— 5
  • 6. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i 0 Control Statements (2)— 6
  • 7. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i 0 Control Statements (2)— 7
  • 8. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i 0 Control Statements (2)— 8
  • 9. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i 0 Control Statements (2)— 9
  • 10. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i 1 Control Statements (2)— 10
  • 11. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i 1 Control Statements (2)— 11
  • 12. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i 1 Control Statements (2)— 12
  • 13. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i 1 Control Statements (2)— 13
  • 14. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i 2 Control Statements (2)— 14
  • 15. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i 2 Control Statements (2)— 15
  • 16. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i is 2 i 2 Control Statements (2)— 16
  • 17. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i is 2 i 2 Control Statements (2)— 17
  • 18. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i is 2 i 3 Control Statements (2)— 18
  • 19. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i is 2 i 3 Control Statements (2)— 19
  • 20. Execution Trace for (int i = 0; i < 3; ++i) { Console.WriteLine("i is“+i); } Console.WriteLine("all done“); i is 0 i is 1 i is 2 all done i 3 1 Control Statements (2)— 20
  • 21. for repetition statement (III)  Example Control Statements (2)— 21
  • 22. for repetition statement (IV)  In for loops, the conditional test is always performed at the top of the loop.  This means that the code inside the loop may not be executed at all if the condition is false to begin with. Control Statements (2)— 22
  • 23. for repetition statement (V)  for Loop Variations  One of the most common variations uses the comma operator to allow two or more variables to control the loop.  Example: Control Statements (2)— 23
  • 24. for repetition statement (VI)  The Infinite Loop with for  for loop is traditionally used for this purpose  Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty: Control Statements (2)— 24
  • 25. for repetition statement (VII)  You can exit from an infinite using break statement  Example Control Statements (2)— 25
  • 26. for repetition statement (VIII)  for Loops with no Bodies  A statement may be empty  This means that the body of the for loop (or any other loop) may also be empty.  You can use this fact to improve the efficiency of certain algorithms and to create time delay loops.  Example for(i=0; i<10000; i++); Control Statements (2)— 26
  • 27. Good Programming Practice 5.1  Control counting loops with integer values.  Place a blank line before and after each major control structure to make it stand out in the program. Control Statements (2)— 27
  • 28. Common Programming Error 5.1  Floating-point values may be approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination. Control Statements (2)— 28
  • 29. Common Programming Error 5.3  When a for structure declares its control variable in the initialization section of the for structure header, using the control variable after the for structure’s body is a compiler error.  Example: Control Statements (2)— 29
  • 30. Common Programming Error 5.4  Using commas in a for structure header instead of the two required semicolons is a syntax error.  Placing a semicolon immediately to the right of a for structure header’s right parenthesis makes the body of that for structure an empty statement. This is normally a logic error. Control Statements (2)— 30
  • 31. Other math operations in for portions  The initialization, loop-continuation condition and increment or decrement portions of a for structure can contain arithmetic expressions.  Assume x = 2 and y = 10;  is equivalent to the statement Control Statements (2)— 31
  • 32. Common Programming Error 5.6  Not using the proper relational operator in the loop-continuation condition of a loop that counts downward (e.g., using i <= 1 in a loop counting down to 1) is usually a logic error that will yield incorrect results when the program runs. Control Statements (2)— 32
  • 33. The while loop Control Statements (2)— 33
  • 34. The while loop (I)  The second popular loop available in C/C++/C# is the while loop.  The general form is while(condition) statement;  Example Control Statements (2)— 34
  • 35. Example: calculate an average Control Statements (2)— 35
  • 36. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 Control Statements (2)— 36
  • 37. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 0 Control Statements (2)— 37
  • 38. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 0 sum 0 Control Statements (2)— 38
  • 39. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 0 sum 0 Control Statements (2)— 39
  • 40. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 0 sum 0 value -- Control Statements (2)— 40
  • 41. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 0 sum 0 value 1 Control Statements (2)— 41
  • 42. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 42/77 numberProcessed 0 sum 1 value 1
  • 43. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 43/77 numberProcessed 1 sum 1 value 1
  • 44. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 sum 1 value 1 Control Statements (2)— 44
  • 45. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 sum 1 value 1 Control Statements (2)— 45
  • 46. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 sum 1 value 5 Control Statements (2)— 46
  • 47. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 1 sum 6 value 5 Control Statements (2)— 47
  • 48. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 sum 6 value 5 Control Statements (2)— 48
  • 49. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 sum 6 value 5 Control Statements (2)— 49
  • 50. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 sum 6 value 5 Control Statements (2)— 50
  • 51. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 sum 6 value 3 Control Statements (2)— 51
  • 52. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 2 sum 9 value 3 Control Statements (2)— 52
  • 53. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 sum 9 value 3 Control Statements (2)— 53
  • 54. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 sum 9 value 3 Control Statements (2)— 54
  • 55. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 sum 9 value 3 Control Statements (2)— 55
  • 56. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 sum 9 value 1 Control Statements (2)— 56
  • 57. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 3 sum 10 value 1 Control Statements (2)— 57
  • 58. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 sum 10 value 1 Control Statements (2)— 58
  • 59. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 sum 10 value 1 Control Statements (2)— 59
  • 60. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 sum 10 value 1 average 2.5 Control Statements (2)— 60
  • 61. Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4 numberProcessed 4 sum 10 value 1 average 2.5 Control Statements (2)— 61
  • 62. Another Example with while loop Class Math include all mathematic functions. Pow(a,b) = ab Initialize counter variable x to 1 The condition is true (x=1<=10) Continue looping as long as x’s value is less than or equal to 10 Increment x by 1, which causes x to exceed 10 eventually Control Statements (2)— 62
  • 63. While loop Control Statements (2)— 63
  • 64. Common Programming Error  Forgetting initializing the control value of the while loop is a logical error. Example: Control Statements (2)— 64
  • 65. The do-while loop Control Statements (2)— 65
  • 66. The do-while loop (I)  Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop  This means that a do-while loop always executes at least once.  The general form of the do-while loop is do { statement; } while(condition); Control Statements (2)— 66
  • 67. The do-while loop (II) Declare and initialize control variable counter do…while loop displays counter’s value before testing for counter’s Control Statements (2)— 67
  • 68. Action true Expression false Control Statements (2)— 68 The do-while loop (III)
  • 69. Loop iterations  Declaring Variables within Selection and Iteration Statements  it is possible to declare a variable within if, switch, for, while, do..while loops  A variable declared in one of these places has its scope limited to the block of code controlled by that statement /* i is local to for loop; j is known outside loop. */ int j; for(int i = 0; i<10; i++) j = i * i; /* i = 10; // *** Error *** -- i not known here! */ Control Statements (2)— 69
  • 70. Jump Statements Control Statements (2)— 70
  • 71.  The return statement  The break statement  The exit() statement  The continue statement Control Statements (2)— 71
  • 72. The return statement  The return statement is used to return from a function  It is categorized as a jump statement because it causes execution to return (jump back) to the point at which the call to the function was made. Control Statements (2)— 72
  • 73. The break Statement (I)  Causes immediate exit from control structure  Used in while, for, do…while or switch statements  The break statement has two uses:  You can use it to terminate a case in the switch statement  You can also use it to force immediate termination of a loop, bypassing the normal loop conditional test Control Statements (2)— 73
  • 74. prints the numbers 0 through 10 on the screen. Then the loop terminates because break causes immediate exit from the loop, overriding the conditional test t<100. Control Statements (2)— 74
  • 75. Programming Tip Programmers often use the break statement in loops in which a special condition can cause immediate termination. Control Statements (2)— 75
  • 76. The exit() Function (I) Control Statements (2)— 76
  • 78. The continue Statement (I)  Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.  For the for loop, continue causes the conditional test and increment portions of the loop to execute.  For the while and do-while loops, program control passes to the conditional tests. Control Statements (2)— 78