CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.

Slides:



Advertisements
Similar presentations
Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS.
Advertisements

CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
Chapter 4 Program Control Statements
Fundamentals of C and C++ Programming Control Structures and Functions.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CS102 Introduction to Computer Programming Chapter 6 Functions Continued.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Iteration Statements CGS 3460, Lecture 17 Feb 17, 2006 Hen-I Yang.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Control Structures (Repetition structure) Jump Statements
CSE 220 – C Programming Loops.
Chapter 6: Loops.
EKT120 COMPUTER PROGRAMMING
C Program Controls + Flow Structure
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Control Structures II
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Looping.
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.2 Control Structures (Iteration)
CHAPTER 21 LOOPS 1.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 13 Control Structures
Presentation transcript:

CSCI 130 Advanced Program Control Chapter 8

Program Controls so far for loop while loop do…while loop

The break statement Can be placed inside the body of a loop When break is encountered, loop is exited (regardless of the condition) Execution passes to first statement outside the loop A break inside a nested loop only causes exiting of the innermost loop

break example for (count = 0; count <= 10; count ++) { if (count == 5) break; } Without break loop iterates 11 times With break, loop stops during 6th iteration

Another break example Write a for loop that will search a 10 element array for the value 12. for (i = 0; i < 10; i ++) {//for written with no break if (arrayName[i] = 12)//this is less efficient foundFlag = ‘Yes’;//Entire array always }//searched for (i = 0; i < 10; i++) {//for written with break if (arrayName[i] = 12) {//more efficient foundFlag = ‘Yes’;//if 12 is first element break;//than loop only entered }//once }

The continue statement Can be placed within the body of a loop When continue encountered, control passes to the next iteration of the loop Statements between continue and end of loop not executed Significantly different than break

Example of continue for (count = 0; count <= 10; count ++) { if (count == 5) continue; printf(“%d ”, count); } Loop iterates 11 times, with or without continue statement Output: –5 is skipped

Another continue example Write the code to check an array of 100 elements. Write out only those elements in the array that are not prime for (i = 0; i < 100; i++) { if (numIsPrime(arrayName[i])) continue; printf(“\n%d”, arrayName[i]); } Note: numIsPrime will return a positive number for primes

The goto statement C’s unconditional branching statement goto and target statement must be in the same function goto can always be performed using better structures (do…while, etc.) NEVER use a goto

Example of a goto void main ( ) { int count= 3; printf("Before any goto %d\n", count); goto location0; printf("This will not print out\n"); location0: ; printf("At location 0\n"); location1: ; printf("At location 1\n"); }

Strong suggestion about goto NEVER USE A GOTO

Infinite Loops Condition will never be evaluated as false Theoretically would run forever Avoid infinite loops

Examples of an infinite loop for (i = 0; i < 10; i+1)//i+1 does not change printf(“%d”, i);//the value of i while ( i < 10) {//Programmer thinks printf(“%d”, i);//i always starts greater i-=1;//than 10 } for (i = 1; i !=10; i +=2)//Printing out odd ints printf(“%d”, i);//up to 10?

The switch statement Most flexible program control statement Program control based on an expression with more than 2 possible values

Referencing elements in an array General form of switch statement: switch(expression) { case template1: statements; case template2: statements; … case templaten: statements; default: statements; }

Concrete example of switch switch(i) { case 1: printf(”The number is 1”); case 2: printf(“The number is 2”); case 3: printf(”The number is 3”); default: printf(”The number is not 1, 2, or 3"); }

Evaluation of a switch statement If expression matches a template, control passes to first statement within that template If no match, control passes to first statement within default If no match and no default, control passed to first statement after switch structure

Output of switch statement switch(i) { case 1: printf(”The number is 1\n”); case 2: printf(“The number is 2\n”); case 3: printf(”The number is 3\n”); default: printf(”The number is not 1, 2, or 3\n"); } If i = 1 Output: The number is 1 The number is 2 The number is 3 The number is not 1, 2, or 3

Correct way to code a switch switch(i) { case 1: printf(”The number is 1\n”); break; case 2: printf(“The number is 2\n”); break; case 3: printf(”The number is 3\n”); break; default: printf(”The number is not 1, 2, or 3\n"); }

System functions All within stdlib.h file (must be included) exit( ) –terminates execution atexit( ) –performs functions at program termination system( ) –executes operating system commands

exit ( ) function Terminates program execution #include void main ( ) { char i; exit(0); printf("Enter a character");//These statements will scanf("%c", &i);//not be executed }

exit ( ) function continued If 0 is passed into function it means program executed normally If 1 is passed into function it means program abnormally terminated stdlib.h has two symbolic constanst: –#define EXIT_SUCCESS 0 –#define EXIT_FAILURE 1 can call exit(EXIT_SUCCESS) can call exit(EXIT_FAILURE)

atexit ( ) function Specifies one (or more) functions that are automatically executed at termination time Up to 32 functions can be registered in this way Executed in reverse order

atexit( ) function continued #include void cleanup(); void cleanupLast(); void main ( ) { char i; atexit(cleanupLast); atexit(cleanup); printf("Enter a character"); scanf("%c", &i); }

system ( ) function Executes operating system commands Example: system(“c:\dir *.exe”); Must capture any output in a file –will not open up another console –will not print to current console Can execute any command line system(“c:\winnt\system32\notepad.exe”);

system( ) does not work in CW system( ) function does not work in Code Warrior Help states: “The system() function is an empty function that is included in the Metrowerks stdlib.h to conform to the ANSI C Standard Library specification.”