A Look Back at Arithmetic Operators: the Increment and Decrement

Similar documents
Dept. of CSE, IIT KGP

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

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

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

Control Structure: Loop

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

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

Programming for Engineers Iteration

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Chapter 4 C Program Control

Chapter 4. Flow of Control

Lecture 10. Daily Puzzle

Loops / Repetition Statements

Repetition and Loop Statements Chapter 5

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Introduction. C provides two styles of flow control:

Structured Programming. Dr. Mohamed Khedr Lecture 9

Chapter 3 Structured Program Development

Chapter 4 C Program Control

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

LECTURE 5 Control Structures Part 2

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

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

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

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

Structured Program Development

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Repetition CSC 121 Fall 2014 Howard Rosenthal

Fundamentals of Programming Session 7

Structured Program Development in C

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Iteration. CSE / ENGR 142 Programming I. while loops. Chapter 5. Motivating Loops. Loop to Add 5 Numbers 1996 UW CSE H - 1

Introduction to the Java Basics: Control Flow Statements

Chapter 3 Structured Program Development in C Part II

Repetition Structures II

Control Structures II. Repetition (Loops)

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

Chapter 5: Control Structures

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

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

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Repetition Structures

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Lab 2: Structured Program Development in C

Operators & Expressions

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

3 The L oop Control Structure

Control Statements. Musa M. Ameen Computer Engineering Dept.

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Unit 7. Lesson 7.1. Loop. For Next Statements. Introduction. Loop

REPETITION CONTROL STRUCTURE LOGO

Computer Programming. Decision Making (2) Loops

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

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

LAB 5: REPETITION STRUCTURE(LOOP)

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

General Information About Iteration (Loops)

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN


Looping. Arizona State University 1

CS110D: PROGRAMMING LANGUAGE I

Computer Programming I - Unit 5 Lecture page 1 of 14

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

CMPT 102 Introduction to Scientific Computer Programming

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Repetition, Looping CS101

PROBLEM SOLVING WITH LOOPS. Chapter 7

Operators And Expressions

Loops / Repetition Statements

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Module 4: Decision-making and forming loops

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

LAB 5: REPETITION STRUCTURE(LOOP)

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Assignment: 1. (Unit-1 Flowchart and Algorithm)

ECET 264 C Programming Language with Applications. C Program Control

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Repetition, Looping. While Loop

Prepared by: Shraddha Modi

IT 1033: Fundamentals of Programming Loops

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Introduction to Computers and Programming

Decision Making and Loops

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Transcription:

A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate on a single operand. The increment operator causes its operand to be increased by 1. Example: a++, ++count The decrement operator causes its operand to be decreased by 1. Example: i--, --distance Spring Semester 2016 Programming and Data Structure 28 1

Operator written before the operand (++i, --i)) Called pre-increment operator. Operator will be altered in value before it is utilized for its intended purpose in the program. Operator written after the operand (i++, i--) Called post-increment operator. Operator will be altered in value after it is utilized for its intended purpose in the program. Spring Semester 2016 Programming and Data Structure 29 Examples Initial values :: a = 10; b = 20; x = 50 + ++a; a = 11, x = 61 x = 50 + a++; x = 60, a = 11 x = a++ + --b; b = 19, x = 29, a = 11 x = a++ ++a; Undefined value (implementation dependent) Called side effects:: while calculating some values, something else get changed. Spring Semester 2016 Programming and Data Structure 30 2

Control Structures that Allow Repetition Spring Semester 2016 Programming and Data Structure 31 Types of Repeated Execution Loop: Group of instructions that are executed repeatedly while some condition remains true. How loops are controlled? Counter Controlled Condition Controlled Sentinel Controlled 3

Counter-controlled repetition Definite repetition know how many times loop will execute. Control variable used to count repetitions. Condition-controlled repetition Loop executes as long as some specified condition is true. Sentinel-controlled repetition Indefinite repetition. Used when number of repetitions not known. Sentinel value indicates end of data. Spring Semester 2016 Programming and Data Structure 33 Counter-controlled Repetition Counter-controlled repetition requires name of a control variable (or loop counter). initial value of the control variable. condition that tests for the final value of the control variable (i.e., whether looping should continue). increment (or decrement) by which the control variable is modified each time through the loop. Spring Semester 2016 Programming and Data Structure 34 4

Read 5 integers and display the value of their summation. Counter Controlled Loop counter 1, sum 0 counter < 6 int counter=1, sum=0; true input n while (counter <6 ) { scanf ( %d, &n); sum = sum + n; sum sum + n counter++; counter++ printf ( \nsum is: %d, output sum sum); false int counter, sum=0; for (counter=1;counter<6; counter++) { scanf ( %d, &n); sum = sum + n; printf ( \nsum is: %d, sum); Spring Semester 2016 Programming and Data Structure 36 5

while Statement The while statement is used to carry out looping operations, in which a group of statements is executed ec repeatedly, as long as some condition remains satisfied. while (condition) statement_to_repeat; while (condition) { statement_1;... statement_n; Spring Semester 2016 Programming and Data Structure 37 C true statement(s) false Single-entry / single-exit structure Spring Semester 2016 Programming and Data Structure 38 6

while :: Examples int digit = 0; int weight=100; while (digit <= 9) printf ( %d \n, digit++); while ( weight > 65 ) { printf ("Go, exercise, "); printf ("then come back. \n"); printf ("Enter your weight:"); scanf ("%d", &weight); Spring Semester 2016 Programming and Data Structure 39 Example: Sum of N Natural Numbers START READ N SUM = 0 COUNT = 1 SUM = SUM + COUNT COUNT = COUNT + 1 int main () { int N, count, sum; scanf ( %d, &N) ; sum = 0; count = 1; while (count <= N) { sum = sum + count; count = count + 1; printf ( Sum=%d\n, sum); return 0; NO IS COUNT > N? YES OUTPUT SUM STOP 7

Example: Maximum of inputs printf ( Enter positive numbers, end with -1.0\n ); max = 0.0; scanf( %f, &next); while (next!= -1.0) { if (next > max) max = next; scanf( %f, &next); printf ( The maximum number is %f\n, max) ; Example of Sentinel-controlled loop Inputs: 10 5 100 25 68-1 do-while Statement Similar to while, with the difference that the check for continuation is made at the end of each pass. In while, the check is made at the beginning. Loop body is executed at least once. do statement_to_repeat; while (condition ); do { statement-1; statement-2; statement-n; while (condition ); Spring Semester 2016 Programming and Data Structure 42 8

statement(s) C true false Single-entry / single-exit structure Spring Semester 2016 Programming and Data Structure 43 do-while :: Examples int digit = 0; do printf ( %d \n, digit++); while (digit <= 9); int weight; do { printf ("Go, exercise, "); printf ("then come back. \n"); printf ("Enter your weight: "); scanf ("%d", &weight); while (weight > 65 ) ; Spring Semester 2016 Programming and Data Structure 44 9

for Statement The for statement is the most commonly used looping structure in C. General syntax: for (expression1; expression2; expression3) statement-to-repeat; for (expression1; expression2; expression3) { statement_1; : statement_n; Spring Semester 2016 Programming and Data Structure 45 How it works? expression1 is used to initialize some variable (called index) that controls the looping action. expression2 represents a condition that must be true for the loop to continue. expression3 is used to alter the value of the index initially assigned by expression1. int digit; int digit; for (digit=0; digit<=9;digit++) printf ( %d \n, digit); for (digit=9;digit>=0;digit--) printf ( %d \n, digit); Spring Semester 2016 Programming and Data Structure 46 10

expression1 expression2 true statement(s) t t( false Single-entry / single-exit structure expression3 Spring Semester 2016 Programming and Data Structure 47 for :: Examples int fact = 1, i, N; int sum = 0, N, count; scanf ( %d, &N); for (i=1; i<=n; i++) fact = fact * i; printf ( %d \n, fact); Compute factorial scanf ( %d, &N); for (i=1; i<=n, i++) sum = sum + i * i; printf ( %d \n, sum); Sum of squares of N natural numbers Spring Semester 2016 Programming and Data Structure 48 11

2-D Figure Print * * * * * #define ROWS 3 * * * * * * * * * * #define COLS 5... for (row=1; row<=rows; row++) { for (col=1; col<=cols; col++) { printf( * ); printf( \n ); Another 2-D Figure Print * ** * * * * * * * * * * * * * #define ROWS 5... int row, col; for (row=1; row<=rows; row++) { for (col=1; col<=row; col++) { printf( * ); printf( \n ); 12