By C. Shing ITEC Dept Radford University

Slides:



Advertisements
Similar presentations
Chapter 4 Function By C. Shing ITEC Dept Radford University.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.
Chapter 3 Flow Control By C. Shing ITEC Dept Radford University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CISC105 – General Computer Science Class 4 – 06/14/2006.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 13 Introduction to C++ Language
Java Fundamentals 4.
Chapter 4 – C Program Control
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
ECE Application Programming
Lecture 7: Repeating a Known Number of Times
Input/output.
7. BASIC TYPES.
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Lecture 13 & 14.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Control Structures II
Looping.
Arrays, For loop While loop Do while loop
- Additional C Statements
Chapter 4 - Program Control
Structured Program Development in C
Computer programming Lecture 3.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Chapter 4 - Program Control
Flow of Control.
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
EECE.2160 ECE Application Programming
ICS103: Programming in C 4: Selection Structures
ICS103: Programming in C 5: Repetition and Loop Statements
EECE.2160 ECE Application Programming
Presentation transcript:

By C. Shing ITEC Dept Radford University Chapter 3 Flow Control By C. Shing ITEC Dept Radford University

Objectives Understand how to use assignment statement Understand how to use conditional statement Understand how to use loop statement

Assignment Statement Identifier assignment_operator expression; Represents identifier = identifier assignment_operator (expression); After evaluating the right hand side of =, it stores result to the left hand side identifier (i.e. memory) and change to the data type of left hand side

Assignment Statement – assignment operator =, +=,-=, *=, /=, %= Example: x *= 3 Means x=x*3

Assignment Statement (Cont.) Identifier = expression; Both sides of data type should match. If not, then the right hand side of type will be widened automatically to the left hand side data type. Otherwise, warning message will be given for narrowing data type.

Assignment Statement (Cont.) Type conversion: Automatic conversion rule Manual conversion: use casting

Assignment Statement (Cont.) Automatic conversion rule: The widest data type in an expression determines the data type of the expression unsigned type is wider than the corresponding type the rest from widest: double, float, long, int, short (or char) Finally, the expression data type will be widened to the data type of the identifier

Assignment Statement (Cont.) Examples: unsigned u; long g; int i; unsigned long ul; ul = u-3*g +i; // right hand side result type // long is converted to unsigned long // before stored in ul

Assignment Statement (Cont.) Examples: unsigned u; float f; int i; double db; db = u/3-i+f; // right hand side result type // float is converted to double before // stored to db

Assignment Statement (Cont.) Casting: Identifier = (left hand side type) (expression)

Assignment Statement (Cont.) Examples: unsigned u; long g; int i; unsigned long ul; i = (int) (u-3*g +4); // right hand side result type // is long

Conditional Statement - if if, if .. else, ?:, switch if form: if (condition) { … }

Conditional Statement – if (Cont.) Note: condition is evaluated using short-circuit If condition consists of condition1 and condition2 connected by a logic operator: Condition1 && condition2 if condition1 is false, then it will not evaluate condition2 Condition1 || condition2 if condition1 is true,

Conditional Statement – if (Cont.) Example: int x; if (x = 5) { printf(“Always TRUE is printed!\n”); }

Conditional Statement – if … else if … else form: if (condition) { … } else

Conditional Statement – if … else (Cont.) Example: if (x<y) { min = x; } else min = y;

Conditional Statement – ?: form: identifier = (condition)?true part:false part

Conditional Statement – ?: (Cont.) Example: (Same example as previous one) min = (x<y) ? x : y;

Conditional Statement – switch switch form: values (char/int) cannot be range as in Visual Basic switch (expression) { case value1: … break; // quit the current (switch) statement case value2: case value3: break; // do this part when value2 or value3

Conditional Statement – switch (Cont.) switch form: (Cont.) case …: … break; default: // everything else }

Conditional Statement – switch (Cont.) Example: switch (character) { case ‘a’: case ‘A’: a_count++; break; … default: printf(“%c is not a letter!\n”, character); }

Loop Statement while do … while for Steps of writing a loop: Identify the loop body: repetition part Identify condition that can make the repetition of loop body either true of false; Make sure the first time, condition is true Initialize loop condition before loop Update loop condition at end of loop body

Loop Statement - while while form: repeat loop body when condition is true – pre-condition while (condition part) { … } Note: condition part can be more than one statement, separated by a comma Example: while (scanf("%c", &character), character !='\n'); What task does the statement above perform?

Loop Statement - while Condition can be Example: int count=1; Counter control (definite loop): use counter Example: int count=1; // the following prints out 5 times only while (count <= 5) { ++count; printf(“%d\n”, count); }

Loop Statement - while Condition can be (Cont.) Sentinel control (indefinite loop): use pseudo data (sentinel) for condition Example: sentinel data is any negative score int score; scanf(“%d”, &score); while (score >= 0) { printf(“%d\n”, score); } Yard-Meter Conversion

Loop Statement - while Condition can be (Cont.) Example: Skip 1st Line More than 1 statement Example: Skip 1st Line

Loop Statement – while (Cont.) Example: Hard –to-find Error: int x; // the following is an infinite loop while (x = 5) { printf(“Always TRUE is printed!\n”); }

Loop Statement – while (Cont.) Example: (Use break to get out of loop) int x, count=0; // the following prints out TRUE 5 times only while (x = 5) { ++count; printf(“Always TRUE is printed!\n”); if (count == 5) break; }

Loop Statement – do while do while form: repeat loop body when condition is true – post-condition do { … } while (condition);

Loop Statement – do while (Cont.) Example: int x=0; do { ++x; printf(“Always TRUE is printed!\n”); } while (x != 5)

Loop Statement - for for form: repeat loop body when condition is true – pre-condition Most general form: can represent while and do while loops

Loop Statement – for (Cont.) for (initialization, exprPart1, …; exprPart2, …,condition; exprPart3, …,last statement in loop body) { … }

Loop Statement – for (Cont.) Example: int i; // loop index must be defined // outside for loop for (i=0;i<5;++i) { printf(“TRUE is printed!\n”); // print 5 times }

Loop Statement – for (Cont.) Example: (Same as the previous one) int i=0; // print 5 times for (;i<5; printf(“TRUE is printed!\n”), ++i);

Loop Statement – for (Cont.) Example: (Use continue statement to skip current loop) int x, count=0; // the following prints out TRUE 5 times only while (count <=10) { ++count; if (count %2 == 1) // skip odd number times of print continue; printf(“TRUE is printed!\n”); }

Loop Statement – for (Cont.) Represent while loop: for (;condition;) { … }

Loop Statement – for (Cont.) How to check end of file: If read a character: (Must deal with \n) Use scanf(“%c”, &c)>0 to read in a character to check any character being read (if reach to end of file, scanf returns -1) Example 1: a.out < checkEOF.c If read an integer: Use scanf(“%d”, &number)>0 to read

Loop Statement – for (Cont.) Represent do while loop: for (;loop body statements, condition;);

Keyboard Input Format Function - scanf Form: scanf(“pure_format”, variable_address); Format: similar to those used in printf, however, no character or strings included %i: for decimal, octal (data begins with 0) or hexadecimal (data begins with 0x) If use %s format, it reads all until it reaches the first white space &variable represents the address of the memory (or variable) Example: char c; int i; double db; char s[80]; // remember s is address: array of 80 characters scanf(“%c%d%lf%s”, &c, &i, &db, s); // sample data:a 100 -1.23 This is a sample data // c=‘a’, i=100, db=-1.23, s=“This”

Class Example Example 1 Example 2 : print character ‘2’ in front of each line Read Character : read a character after reading a number For loop

Assignment Assignment #1

Practice Given int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a +=b+c a= b *=c=d+5 b=

Practice - Answer Given int a=1,b=2,c=3,d=4; Find the values of the following table: Expression Value ________ _____ a +=b+c a= 6 b *=c=d+5 b= 18

References Herbert Schildt: C: The Complete Reference, 4th ed, Osborne Publishing Deitel & Deitel: C How to Program, 4th ed., Chapter 3, 4 & 9, Prentice Hall