Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Introduction to C Programming
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Introduction to Computer Programming in c
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Controlling Program Flow with Looping Structures
Controlling Program Flow with Decision Structures.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 – C Program Control
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
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.
Lecture 7: Repeating a Known Number of Times
Python: Control Structures
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 5: Loops and Files.
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Control Structures - Repetition
Chapter 4 Control structures and Loops
Arrays, For loop While loop Do while loop
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 7 Additional Control Structures
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Computing Fundamentals
Computer programming Lecture 3.
JavaScript: Control Statements II
Loops.
Repetition Statements (Loops) - 2
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.
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
Controlling Program Flow
REPETITION Why Repetition?
Chapter 8 JavaScript: Control Statements, Part 2
Looping and Repetition
Presentation transcript:

Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei Email: qinpeizhao@tongji.edu.cn http://sse.tongji.edu.cn/zhaoqinpei/Courses/

Loops Repeat a statement, or a block of statements Repeat a statement or a block of statements until a particular condition is fulfilled for, while, and do-while loops The increment and decrement operators Plays a simple Simon game Beiginning C / Qinpei Zhao 2018/9/19

Endless loop Beiginning C / Qinpei Zhao 2018/9/19

How loop works Loop: executes a series of statements repeatedly a given number of times or until a particular condition is fulfilled. Game: guessing numbers(lec4_1_GuessNumberGame.c) Beginning C / Qinpei Zhao 2018/9/19

The increment and decrement operator increment operator (++) 自增 Increase the value stored in the integer variable that they apply to by 1. ++number; decrement operator (--) 自减 Decrease the value stored in the integer variable that they apply to by 1. -- number; the variables do change ! If number = 2; then number ++; will modify the number to 3; number = number + 1; number = number - 1; Beginning C / Qinpei Zhao 2018/9/19

The for loop display the numbers from 1 to 10 Beginning C / Qinpei Zhao 2018/9/19

Executes once, when the loop starts. The for loop (cont.) Executes once, when the loop starts. Beginning C / Qinpei Zhao 2018/9/19

The for loop (cont.) The expression is evaluated at the beginning of each loop cycle. If it is true, the loop continues, otherwise, the loop ends. Beginning C / Qinpei Zhao 2018/9/19

The expression is executed at the end of every loop cycle. The for loop (cont.) The expression is executed at the end of every loop cycle. Beginning C / Qinpei Zhao 2018/9/19

Logical expression (true or false) The for loop (cont.) Logical expression (true or false) Within parentheses; Each expression is separated by semicolon; You can omit any of the control expressions, but semicolon should be there. Beginning C / Qinpei Zhao 2018/9/19

The for loop display the numbers from 1 to 10 Beginning C / Qinpei Zhao 2018/9/19

Drawing a box (lec4_2_drawbox.c) Beginning C / Qinpei Zhao 2018/9/19

The increment operator ++ What is the number of total? 8 Beginning C / Qinpei Zhao 2018/9/19

The prefix and postfix forms of ++ Prefix ++i: the incrementing occurs before its value has been used Postfix i++: the incrementing occurs after its value has been used reduce confusion by using parentheses What is the number of total? 7 Beginning C / Qinpei Zhao 2018/9/19

The flexible for loop How about this? How about ++i? Beginning C / Qinpei Zhao 2018/9/19

A for loop with No parameters Is the grammar correct? What will happen? Beginning C / Qinpei Zhao 2018/9/19

The break statement in the loop Beginning C / Qinpei Zhao 2018/9/19

A minimal for loop (lec4_4_indefinite.c) Beginning C / Qinpei Zhao 2018/9/19

Generating pseudo-random integers Pseudo-random: truly random numbers can arise only in natural processes and can’t be generated algorithmically. Standard function with seed as a paramter Default seed <stdlib.h> [0, RAND_MAX] Beginning C / Qinpei Zhao 2018/9/19

More for loop control options Too much of this can make your code hard to understand. Do not rely on equality of floating numbers Beginning C / Qinpei Zhao 2018/9/19

Equality of floating numbers A = = B Relative error Absolute error

The while loop Beginning C / Qinpei Zhao 2018/9/19

Using the while loop Beginning C / Qinpei Zhao 2018/9/19

Nested loops Beginning C / Qinpei Zhao 2018/9/19

goto statement 10*20*30 is a statement following the nested loops. Beginning C / Qinpei Zhao 2018/9/19

do-while while v.s. do-while? run the statement at least once Beginning C / Qinpei Zhao 2018/9/19

The continue statement You don’t want to end a loop, but you want to skip the current iteration and continue with the next. continue is a keyword Beginning C / Qinpei Zhao 2018/9/19

Designing a program game of Simple Simon Simple Simon is a memory-test game. The computer displays a sequence of digits on the screen for a short period of time. You then have to memorize them, and when the digits disappear from the screen, you must enter exactly the same sequence of digits. Each time you succeed, you can repeat the process to get a longer list of digits for you to try. The objective is to continue the process for as long as possible. game of Simple Simon lec4_problem.c Beginning C / Qinpei Zhao 2018/9/19

Summary three different loops you can use to repeatedly execute a block of statements. The for loop: use for counting loops where the value of a control variable is incremented or decremented by a given amount on each iteration until some final value is reached. The while loop: the loop continues as long as a given condition is true. The do-while loop, works like the while loop except that the loop condition is checked at the end of the loop block. Consequently the loop block is always executed at least once. flow chart understand operator precedence (use parentheses) comment, indentation Beginning C / Qinpei Zhao 2018/9/19