Conditionals, Loops, and Other Kinds of Statements

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Discussion of Assignment #2 CS-2301, B-Term Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Digression on Loop Invariants CS-2303, C-Term Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Conditionals, Loops, and Other Statements CS-2301 D-term Conditionals, Loops, and Other Kinds of Statements CS-2301 System Programming C-term 2009.
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.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Conditionals, Loops, and Other Statements CS-2301, B-Term Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
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.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
Administrative things
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Programming Languages and Paradigms
Loop Structures.
Quick Test What do you mean by pre-test and post-test loops in C?
Lecture 13 & 14.
Control Structures II (Repetition)
Programming Paradigms
CiS 260: App Dev I Chapter 4: Control Structures II.
Control Structures.
Chapter 13 Control Structures
Arrays, For loop While loop Do while loop
CSS161: Fundamentals of Computing
- Additional C Statements
Outline Altering flow of control Boolean expressions
More about Numerical Computation
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
CSC215 Lecture Control Flow.
Chapter 6: Repetition Statements
Computing Fundamentals
Alternate Version of STARTING OUT WITH C++ 4th Edition
Digression on Loop Invariants
Chapter 5: Control Structures II (Repetition)
Dale Roberts, Lecturer IUPUI
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Flow of Control.
CSC215 Lecture Control Flow.
Chapter 13 Control Structures
Presentation transcript:

Conditionals, Loops, and Other Kinds of Statements Assumptions: Graduate level Operating Systems Making Choices about operation systems Why a micro-century? …just about enough time for one concept CIS 1057 Computer Programming in C Fall 2013 (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements

Conditionals, Loops, and Other Statements Review Functions CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 2

Conditionals, Loops, and Other Statements If-else Statements if (expr) statement1 /*do this if expr is non-zero*/ else statement2 /*do this if expr is zero*/ The else clause is optional! CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 3

Conditionals, Loops, and Other Statements If-else Examples if (j > limit) return j; /* note semicolon*/ else j += stepValue; /* note semicolon*/ ... if (++k >= 4) k = 0; /* increment k mod 4*/ CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 4

If-else Examples (continued) if (n < maxInput) { scanf("string", &arg1, &arg2, …); n++; printf("string2", arg3, arg4, …); } else { /* note NO semicolon*/ printf("Summary\n", arg5, …); return n; } CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 5

Concatenated If-else Statements if (expr1) statement1 /*do this if expr1 is non-zero*/ else if (expr2) statement2 /*i.e., expr1 == 0, expr2 != 0*/ else if (expr3) statement3 /expr1 and expr2 are zero, expr3 != 0*/ else if (expr4) statement4 /expr1, expr2 , expr3 all zero, expr4 != 0*/ else statement5 /*i.e., all expr are zero*/ CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 6

Concatenated If-else Statements Last else is always attached to last if If it should be attached to any other if, use {} to control the flow of execution. CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 7

Conditionals, Loops, and Other Statements Switch Statement Somewhat like a concatenated if-else Occasionally easier to read Each arm is called a case Evaluate switch expression, select the appropriate case (if any) default case is optional Difference from concatenated if-else Need break statement to exit switch after a case Otherwise, control falls through to next case CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 8

Switch Statement Example int month, daysInMonth, leapYear; … switch (month) { case 0: printf("January\n"); daysInMonth = 31; break; case 1: printf("February\n"); daysInMonth = leapYear ? 29 : 28; break; case 2: printf("March\n"); daysInMonth = 31; break; case 3: printf("April\n"); daysInMonth = 30; break; … } // switch CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements

Switch Statement Example case values must be constants int month, daysInMonth, leapYear; … switch (month) { case 0: printf("January\n"); daysInMonth = 31; break; case 1: printf("February\n"); daysInMonth = leapYear ? 29 : 28; break; case 2: printf("March\n"); daysInMonth = 31; break; case 3: printf("April\n"); daysInMonth = 30; break; … } // switch break statement needed to jump over subsequent cases default case is optional (not shown) CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements

Conditionals, Loops, and Other Statements Questions? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 11

Conditionals, Loops, and Other Statements Iterative Statement while loop for loop do-while loop CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements

Conditionals, Loops, and Other Statements while loops while (expression) statement Evaluate expression If true, execute statement and then repeat Repeat until expression becomes false statement may be executed zero or more times! Often a compound statement CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 13

Conditionals, Loops, and Other Statements while loop example Note initialization of sum and count int sum = 0; int count = 0; int input; while (scanf("%d", &input) != EOF){ sum += input; count++; printf("Input value of %f recorded.\n", input); ... } if (count > 0) printf("Average is %f\n", (double)sum/count); else printf("No inputs recorded\n"); What is this? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 14

while loop examples (continued) A program to count digits, white space characters, and other characters. Includes a while loop with a switch statement inside CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 15

Conditionals, Loops, and Other Statements do-while loop Note: semicolon is required here do statement while (expression); Similar to while loop, but guaranteed to execute statement at least once CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 16

Conditionals, Loops, and Other Statements Breaking out of a Loop When it becomes necessary to terminate a loop prematurely break; /*exits smallest containing switch or loop*/ When it becomes necessary to terminate the current iteration and start the next one continue; /*terminates this iteration only*/ CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 17

Conditionals, Loops, and Other Statements Questions? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 18

Conditionals, Loops, and Other Statements for loop Remember: zero is false, non-zero is true A counting loop for (expr1; expr2; expr3) statement Evaluate expr1 to initialize Evaluate expr2 to test If true, execute statement If not true, exit for loop Evaluate expr3 to prepare for next iteration Repeat expr2 to test CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 19

for loops and while loops The for loop for (expr1; expr2; expr3) statement is exactly equivalent to the following expr1; while (expr2) { statement expr3; } CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 20

The most common kind of for-loop int i; for (i = 0; i < limit; i++) { ...; /* do something with ith entity */ } Loop “body” is typically a compound statement CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 21

The most common kind of for-loop int i; for (i = 0; i < limit; i++) { ...; /* do something with ith entity */ } It is traditional in C that for-loops start counting at zero and test that the counter is less than the upper limit CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 22

The most common kind of for-loop int i; for (i = 0; i < limit; i++) { ...; /* do something with ith entity */ } This loop iterates limit times. Iterations are numbered i = 0, 1, 2, …, limit-1 Reason:– arrays are indexed this way! CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 23

Conditionals, Loops, and Other Statements Nested for-loops int i, j; for (i = 0; i < limit; i++) { ...; /*prepare subgroup i*/ for (j=0; j < limit2; j++) { ...; /* do something with item j of subgroup i*/ } CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 24

An Extension in Modern C Compilers The following construct is legal in C99:– for (int i = 0; i < limit; i++){ expression involving i; ...; printf(“Iteration %d completed.\n”, i); ...; } The loop counter i is declared in for loop Not visible outside of loop! Common practice Good programming style CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 25

Conditionals, Loops, and Other Statements Notes on Loop Style for (int i = 0; i < limit; i++) { ...; /*prepare for subgroup i*/ for (int j=0; j < limit2; j++) { ...; /* do something with item j of subgroup i*/ } /* end of loop j */ } /* end of loop i */ Declare loop variables in for-statements – C99 CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 26

Conditionals, Loops, and Other Statements Notes on Loop Style for (int i = 0; i < limit; i++) { ...; /*prepare for subgroup i*/ for (int j=0; j < limit2; j++) { ...; /* do something with item j of subgroup i*/ } /* end of loop j */ } /* end of loop i */ Include a comment at end of each loop to show which loop it is CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 27

Conditionals, Loops, and Other Statements Questions? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 28

Conditionals, Loops, and Other Statements An Example int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 29

An An Example (continued) This variable is “global” to the loop. I.e., it is remembered from one iteration to the next. int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 30

An Example (continued) int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month At beginning of each iteration, startingDay indicates the day of the week on which that particular month starts. It is the responsibility of the loop to update startingDay for the next month. CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 31