Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Similar documents
Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

GAS Tutorial - 6. Expression

Day21 A. Young W. Lim Wed. Young W. Lim Day21 A Wed 1 / 13

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Chapter 4 C Program Control

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Lecture 7 Tao Wang 1

Pointers (2) Applications

GAS Tutorial - 4. Sections & Relocation

A Look Back at Arithmetic Operators: the Increment and Decrement

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Introduction. C provides two styles of flow control:

Programming for Engineers Iteration

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Link 7.A Static Linking

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

Accessibility (1A) Young Won Lim 8/22/13

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Programming Language A

Chapter 3 Structured Program Development

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

HyperGeometric Distribution

Computers Programming Course 7. Iulian Năstac

Decision Making and Loops

Dept. of CSE, IIT KGP

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Lecture 3. More About C

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

ECET 264 C Programming Language with Applications. C Program Control

Fundamentals of Programming Session 9

Logic Haskell Exercises

Lecture 6. Statements

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

REPETITION CONTROL STRUCTURE LOGO

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Structured Program Development

Control structures in C. Going beyond sequential

Link 7. Dynamic Linking

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

2. HW/SW Co-design. Young W. Lim Thr. Young W. Lim 2. HW/SW Co-design Thr 1 / 21

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

GAS Tutorial - 7. Directives (2)

COMP 208 Computers in Engineering

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Chapter 4 C Program Control

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Problem Solving and 'C' Programming

Control Structures II. Repetition (Loops)

Chapter 5: Control Structures

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Programming for Electrical and Computer Engineers. Loops

C++ Programming: From Problem Analysis to Program Design, Third Edition

Flow Control. CSC215 Lecture

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 13 Control Structures

do, while and for Constructs

Introduction. Pretest and Posttest Loops. Basic Loop Structures ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Structured Programming. Dr. Mohamed Khedr Lecture 9

Example 2. Young Won Lim 11/24/17

Class (1A) Young Won Lim 9/8/14

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Scope. Scope. Region of a program in which a defined object is visible. Defined Objects. Two types of regions. Variables Functions

Assoc. Prof. Dr. Tansu FİLİK

NWEN 241 Systems Programming Exercises (Set 1)

Chapter 13 Control Structures

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Expression and Operator

Operators And Expressions

204111: Computer and Programming

Binary Search Tree (3A) Young Won Lim 6/2/18

CIS 3260 Intro to Programming in C#

n Group of statements that are executed repeatedly while some condition remains true

Lecture 10. Daily Puzzle

Example 3. Young Won Lim 11/22/17

Transcription:

Day06 A Young W. Lim 2017-09-20 Wed Young W. Lim Day06 A 2017-09-20 Wed 1 / 26

Outline 1 Based on 2 C Program Control Overview for, while, do... while break and continue Relational and Logical Operators Assignment Operators Increment / Decrement Operators Young W. Lim Day06 A 2017-09-20 Wed 2 / 26

Based on "C How to Program", Paul Deitel and Harvey Deitel I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Day06 A 2017-09-20 Wed 3 / 26

Repetition Essentials loop : a group of instructions which are executed repeatedly while the loop continuation condition is true counter controlled repetition : definite repetition the number of repetition is known in advance counter variable sentinel based repetition : indefinite repetition the end of data Young W. Lim Day06 A 2017-09-20 Wed 4 / 26

Counter Controlled Repetition counter variable (loop counter) the initial value the increment / decrement the test condition Young W. Lim Day06 A 2017-09-20 Wed 5 / 26

for Repetition Statement 1 the control variable is initialized 2 the loop continuation condition is checked 3 if the condition is met, the loop body is executed 4 the control variable is incremented / decremented 5 the loop continuation condition is checked 6 repeat 3~5 until the loop continuation condition fails for (init; cond; update) statement; Young W. Lim Day06 A 2017-09-20 Wed 6 / 26

for Repetition Statement Examples (1) 1 for (i=1; i<=100; ++i) 2 for (i=100; i>=1; --i) 3 for (i=1; i<100; i+=2) 4 for (i=99; i>=1; i-=2) 5 for (i=1; i<=1000; i*=2) 6 for (i=1000; i>=1; i/=2) for (init; cond; update) statement; Young W. Lim Day06 A 2017-09-20 Wed 7 / 26

for Repetition Statement Examples (2) #include <stdio.h> int main(void) { int i=0; for (i=1; i<=1024; i*=2) printf("i=%d \n", i); printf("----------------------\n"); for (i=1024; i>=1; i/=2) printf("i=%d \n", i); printf("----------------------\n"); for (i=0; i<=10; i++) printf("1<<%2d= %d \n", i, 1<<i); printf("----------------------\n"); for (i=10; i>=0; i--) printf("1<<%2d= %d \n", i, 1<<i); printf("----------------------\n"); Young W. Lim Day06 A 2017-09-20 Wed 8 / 26

for and while Repetition Statements init : initialize the loop control variable cond : the loop continuation condition update : increments / decrements the loop control variable for (init; cond; update) statement; init; while (cond) { statement; update; Young W. Lim Day06 A 2017-09-20 Wed 9 / 26

The optional three statements in for init expression is omitted init; for ( ; cond; update) statement; cond expression is omitted for (init; ; update) statement; for (init; 1 ; update) statement; update expression is omitted for (init; cond; _) { statement; update; infinite loop for ( ; ; ) {... if (...) break;... Young W. Lim Day06 A 2017-09-20 Wed 10 / 26

The comma operator in for statement for (exp1, exp2; exp3, exp4; exp5, exp6) statments the comma operator: evaluates the expressions from left to right the value of the entire expression : the value of the rightmost expression condition is checked for exp4, after exp3 is executed Young W. Lim Day06 A 2017-09-20 Wed 11 / 26

The comma operator examples (1) for (S=0, i=1; i<11; ++i, S+=i) ; not working when S=0, S+=2 instead of S+=1 for (S=0, i=1; i<11; S+=i, ++i) ; the final i value is 11 S is incremented by 10, before incrementing i to 11 after for loop, S contains the correct value 55 for (S=0, i=1; i<11; S+= i++) ; combine two statements S+=i, ++i into one statement S is incremented by old i, before incrementing i Young W. Lim Day06 A 2017-09-20 Wed 12 / 26

The comma operator examples (2) #include <stdio.h> int main(void) { int i=0, S=0; S=0; for (i=1; i<11; ++i) { S += i; printf("i=%d S=%d \n", i, S); printf("----------------------\n"); for (S=0, i=1; i<11; ++i, S+=i) ; printf("----------------------\n"); for (S=0, i=1; i<11; S+=i, ++i) ; printf("----------------------\n"); for (S=0, i=1; i<11; S+=i++) ; printf("i=%d S=%d (X) \n", i, S); printf("i=%d S=%d \n", i, S); printf("i=%d S=%d \n", i, S); printf("----------------------\n"); Young W. Lim Day06 A 2017-09-20 Wed 13 / 26

do... while Repetition Statement if the initial loop continuation condition is false do... while executes the loop body at least once for and while never execute the loop body init2; do { statement; update; while (cond2); init; while (cond) { statement; update; for (init; cond; update) statement; Young W. Lim Day06 A 2017-09-20 Wed 14 / 26

break and continue statements break in for, while, do... while, and switch statements immediate exits from the body continue in for, while, do... while statements skips the remaining statement after continue in the body continue the next iteration of the loop while and do... while the loop continuation condition is checked immediately for the update expression is executed and then the loop continuation condition is executed Young W. Lim Day06 A 2017-09-20 Wed 15 / 26

break example #include <stdio.h> int main(void) { int i; int S; S=0; for (i=0; i<10 ; ++i) { if (i==5) break; S += (i+1); printf("i=%d S=%d \n", i,s); Young W. Lim Day06 A 2017-09-20 Wed 16 / 26

continue example #include <stdio.h> int main(void) { int i; int S; S=0; for (i=0; i<10 ; ++i) { printf("i%%2 = %d --- ", i%2); if ((i%2) == 1) { printf("\n"); continue; S += (i+1); printf("i=%d S=%d \n", i,s); #include <stdio.h> int main(void) { int i; int S; S=0; for (i=1; i<10 ; i=i+2) { printf("i=%d old S=%d --- ", i,s); S += (i+1); printf("i=%d S=%d \n", i,s); Young W. Lim Day06 A 2017-09-20 Wed 17 / 26

True and False Values the result of the following operators : 0 for false, 1 for _true relational operators (>, <, >=, <=) equality operators (==,!=) logical operators (&&,,!) accepts nonzero value as a true Young W. Lim Day06 A 2017-09-20 Wed 18 / 26

The results of relational operators int main(void) { int a =10; float x =3.14; printf(" a = %d \n", a); printf("(a > 1) = %d \n", (a>1) ); printf("(a < 1) = %d \n", (a<1) ); printf(" x = %f \n", x); printf("(x > 1) = %d \n", (x>1) ); printf("(x < 1) = %d \n", (x<1) ); int main(void) { if (0) printf("0 is true \n"); else printf("0 is false \n"); if (1) printf("1 is true \n"); else printf("1 is false \n"); if (-999) printf("-999 is true \n"); else printf("-999 is false \n"); if (3.14999) printf("3.14999 is true \n"); else printf("3.14999 is false \n") Young W. Lim Day06 A 2017-09-20 Wed 19 / 26

Logical Operators to form a complex conditions logical AND : && (true when all operands are true) logical OR : (true when any operand is true) logical NOT :! logical AND true when all operands are true false when any operand is false logical OR true when any operand is true false when all operands are false Young W. Lim Day06 A 2017-09-20 Wed 20 / 26

Evaluation of relational operators && is higher precedence over A && B if A is true, then B is evaluated if A is false, then B needs not be evaluated (false) A B if A is true, then B needs not be evaluated (true) if A is false, then B is evaluated Young W. Lim Day06 A 2017-09-20 Wed 21 / 26

Assignment Operators first compute then assign a += b a + b a = a + b a -= b a - b a = a - b a *= b a * b a = a * b a /= b a / b a = a / b a %= b a % b a = a % b Young W. Lim Day06 A 2017-09-20 Wed 22 / 26

Assignment Operator Examples (1) #include <stdio.h> int main(void) { int m = 100; int n = 20; printf("m=100, n=20 \n"); m = m + n; printf("m = m + n = %d \n", m); m = m - n; printf("m = m - n = %d \n", m); m = m * n; printf("m = m * n = %d \n", m); m = m / n; printf("m = m / n = %d \n", m); m += n; printf("m += n = %d \n", m); m -= n; printf("m -= n = %d \n", m); m *= n; printf("m *= n = %d \n", m); m /= n; printf("m /= n = %d \n", m); Young W. Lim Day06 A 2017-09-20 Wed 23 / 26

Assignment Operator Examples (2) printf("m = m + n --> %d \n", m = m + n); printf("m = m - n --> %d \n", m = m - n); printf("m = m * n --> %d \n", m = m * n); printf("m = m / n --> %d \n", m = m / n); printf("m += n --> %d \n", m += n); printf("m -= n --> %d \n", m -= n); printf("m *= n --> %d \n", m *= n); printf("m /= n --> %d \n", m /= n); m = n = 999; printf("m=%d, n=%d \n", m, n ); Young W. Lim Day06 A 2017-09-20 Wed 24 / 26

Increment / Decrement Operators first update then access b = ++a; a = a + 1; b = a b = a; a = a 1; b = a first access then update b = a++; b = a; a = a + 1; b = a ; b = a; a = a 1; Young W. Lim Day06 A 2017-09-20 Wed 25 / 26

Pre-increment / Post-increment Examples #include <stdio.h> int main(void) { int i; i = 100; printf("++i= %d \n", ++i); // pre-increment printf("i = %d \n", i); i = 100; printf("i++= %d \n", i++); // post-increment printf("i = %d \n", i); i = 100; printf("--i= %d \n", --i); // pre-decrement printf("i = %d \n", i); i = 100; printf("i--= %d \n", i--); // post-decrement printf("i = %d \n", i); Young W. Lim Day06 A 2017-09-20 Wed 26 / 26