Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

Chapter 3 - Structured Program Development
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Structured Program Development in C
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Fundamentals of C and C++ Programming Control Structures and Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Control Statements Spring Semester 2013Programming and Data Structure1.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
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.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
C Lecture Notes 1 Structured Program Development.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 4 – C Program Control
Lecture 7: Repeating a Known Number of Times
JavaScript: Control Statements I
Chapter 4 - Program Control
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
JavaScript: Control Statements I
Programming Fundamentals Lecture #6 Program Control
Lecture 2: Logical Problems with Choices
- Additional C Statements
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Lecture 10: Reviews

Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default Selection structures Used to choose among alternative courses of action Three types: if, if … else, and switch Repetition structures Used to repeat an action when some condition remains true. Three types: while, do … while and for

Sequence Structures Normally, statements are executed one after the other in the order written

Selection Structures - if if statement if (condition) { statement(s); } Only performs an action if the condition is true. Example: Flowchart if ( grade >= 60 ) { printf( “Passed\n” ); } if ( grade >= 60 ) { printf( “Passed\n” ); printf( “Congratulations!\n” ); } if ( grade >= 60 ) printf( “Passed\n” ); printf( “Congratulations!\n” );

Selection Structures - if…else If…else statement if (condition) { statement(s); } else { statement(s); } Specifies an action to be performed both when the condition is true and when it is false. Flowchart

Selection Structures - if…else Nested if…else statements Test for multiple cases by placing if…else selection statements inside if…else selection statement Once condition is met, rest of statements skipped if ( score >= 90 ) { printf( "The grade is 'A'.\n" ); } else if ( score >= 80 ) { printf( "The grade is 'B'.\n" ); } else if ( score >= 70 ) { printf( "The grade is 'C'.\n" ); } else if ( score >= 60 ) { printf( "The grade is 'D'.\n" ); } else { printf( "The grade is 'F'.\n" ); } >= 90:A 80 ~ 89:B 70 ~ 79:C 60 ~ 69:D < 60:F

Selection Structures - if…else if…else statements in sequence if ( score >= 90 ) { printf( "The grade is 'A'.\n" ); } else if ( score >= 80 ) { printf( "The grade is 'B'.\n" ); } if ( score >= 70 ) { printf( "The grade is 'C'.\n" ); } else if ( score >= 60 ) { printf( "The grade is 'D'.\n" ); } else { printf( "The grade is 'F'.\n" ); }

Selection Structures - switch switch statement Useful when a variable or expression is tested for all the values it can assume and different actions are taken. Format Series of case labels and an optional default case switch ( controlling expression ) { case value1: actions case value2: actions default: actions } The switch statement can be used only for testing a constant integral expression. switch (grade) { case >= 90: … case >= 80: … ….. } default case occurs if none of the case s are matched

Selection Structures - switch switch statement with break statements switch ( controlling expression ) { case value1: actions; break; case value2: actions; break; default: actions; break; } It causes program control to continue with the first statement after the switch statement.

Selection Structures - switch switch statement with break statements Example switch ( n ) { 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”); break; }

Selection Structures - switch switch statement with break statements Example switch ( n ) { case 1: printf( “The number is 1\n”); 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”); break; }

Repetition Structures Two types of repetition structures Counter-controlled repetition Definite repetition: know how many times loop will execute Control variable used to count repetitions Sentinel-controlled repetition Indefinite repetition Used when number of repetitions not known Sentinel value indicates "end of data entry” Loop ends when user inputs the sentinel value

Repetition Structures Common error: Infinite loops are caused when the loop-continuation condition in a while, for or do...while statement never becomes false. To prevent: a) Make sure there is not a semicolon immediately after the header of a while statement. b) In a counter-controlled loop, make sure the control variable is incremented (or decremented) in the loop. c) In a sentinel-controlled loop, make sure the sentinel value is eventually input. j = 1; while (j <= 10); { printf(“%d\n”, j); j++; } j = 1; while (j <= 10) { printf(“%d\n”, j); } j = 1; while (j <= 10) { printf(“%d\n”, --j); j++; } j = 1; while (j <= 10) { printf(“%d\n”, j); j--; }

Repetition Structures - while while loop repeated until condition becomes false Counter-controlled repetition Sentinel-controlled repetition Format while ( condition ) { statement(s); } Flowchart condition while body

Repetition Structures - while Counter-controlled while loop Loop repeated until counter reaches a certain value Count the number of iterations j = 1; while (j <= 10) { printf(“Good Luck\n”); j++; } j = 0; while (j < 10) { printf(“Good Luck\n”); j++; } j = 0; while (j <= 10) { printf(“Good Luck\n”); j++; } j = 1; while (j <= 10) { printf(“Good Luck\n”); j += 2; } j = -1; while (j <= 10) { printf(“Good Luck\n”); j += 3; }

Repetition Structures - while Sentinel-controlled while loop Loop repeated until user inputs the sentinel value Sentinel value chosen so it cannot be confused with a regular input Examples Simple calculator ATM menu interface Prime number

Repetition Structures - do…while Similar to the while statement Condition for repetition only tested after the body of the loop is performed. All actions are performed at least once. Format do { statement(s); } while ( condition ); Flowcharting the do…while repetition statement

Repetition Structures - do…while Examples counter = 1; do { printf( “%d\n”, counter); counter ++; } while (counter <= 10); counter = 1; while (counter <= 10) { printf( “%d\n”, counter); counter ++; } counter = 12; do { printf( “%d\n”, counter); counter ++; } while (counter <= 10); counter = 12; while (counter <= 10) { printf( “%d\n”, counter); counter ++; }

Repetition Structures - for Can loop a known number of times Format when using for loops for ( initialization; loopContinuationTest; increment ) { statement(s); } initialization; while ( loopContinuationTest ) { statement(s); increment; } for (j = 1; j <= 10; j++) { printf(“%d\n”, j); } for (j = 10; j > 0; j--) { printf(“%d\n”, j); } for (j = 10; j > 0; j -= 2) { printf(“%d\n”, j); } incrementdecrement Increasing/decreasing other than 1

Nested Control Loops Nested while loops i = 1; while (i < 5) { j = 1; while (j < 5) { printf(“%d * %d = %d\n”, i, j, i*j); j++; } i++; } Nested for loops for (i = 1; i < 5; i++) { for (j = 1; j < 5; j++) { printf(“%d * %d = %d\n”, i, j, i*j); } i j

Nested Control Loops Nested while loops i = 1; while (i < 5) { j = 1; while ( j <= i ) { printf(“%d * %d = %d\n”, i, j, i*j); j++; } i++; } Nested for loops for (i = 1; i < 5; i++) { for (j = 1; j <= i; j++) { printf(“%d * %d = %d\n”, i, j, i*j); } i j j <= i

Nested Control Loops Nested while loops i = 1; while (i < 5) { j = 1; while ( j < 5 ) { printf(“%d * %d = %d\n”, i, j, i*j); j++; } i += 2; } Nested for loops for (i = 1; i < 5; i += 2) { for (j = 1; j < 5; j++) { printf(“%d * %d = %d\n”, i, j, i*j); } i j

Operators Arithmetic operators +, -, *, /, % Assignment operator = Arithmetic assignment operators +=, -=, *=, /=, %= Increment / decrement operators ++, -- Relational operators, >= Equality operators ==, != Logical operators &&, ||, !

== and = Swapping both operators does not ordinarily cause syntax errors. int payCode = 0; payCode = 4; if (payCode == 4) printf(“You get a bonus!\n”); int payCode = 0; payCode == 4; if (payCode == 4) printf(“You get a bonus!\n”); int payCode = 0; payCode = 3; if (payCode == 4) printf(“You get a bonus!\n”); int payCode = 0; payCode = 3; if (payCode = 4) printf(“You get a bonus!\n”);

Pre-increment and Post-increment Pre-increment/Pre-decrement Operator is used before the variable (++c or --c) Variable is changed before the expression it is in is evaluated Post-increment/Post-decrement Operator is used after the variable (c++ or c--) Expression executes before the variable is changed. c = 5; printf(“%d\n”, ++c); printf(“%d\n”, c); c = 5; printf(“%d\n”, c++); printf(“%d\n”, c); c = 5; ++c; printf(“%d\n”, c); c = 5; c++; printf(“%d\n”, c);

Logical Operators Logical AND - && Logical OR - || Logical NOT - ! x y x y

Logical Operators Examples int F = 0, T = 1; int x, y; x = !240; x = !0; x = (F && 1) || (T || F); y = (T || !F) && (!F && T); x = !(T && F || 1) if (grade >= 80 && grade <90) printf(“B\n”); while ((x > 0) || (y > 10)) { x--; y--; }

scanf and printf scanf() Used to obtain a value (values) from the user using standard input (usually keyboard). Examples int num, j; float x; scanf( “%d”, &num); scanf( “%d%d”, &num, &j); scanf( “%f”, &x); scanf( “%d%d%f”, &num, &j, &x); scanf( “%d”, num); scanf( “%d%d”, num);

scanf and printf printf() int num = 3, j = 2; float x = 3.0; printf( “Good Luck!\n”); /* prints a string */ printf( “num = %d\n”, num); /* prints the value of a variable */ printf( “%d + %d = %d\n”, num, j, num+j); /* prints the value of an expression */ printf( “%c\n”, ‘b’); /* prints the character ‘b’ */ j = ‘a’; printf( “%c\n”, j); /* prints the character ‘a’ */ printf( “x = %f\n”, x); /* prints a floating-point number */ printf( “x = %.2f\n”, x ); /* prints result with 2 digits after decimal point */