Decision Making and Loops

Similar documents
Prepared by: Shraddha Modi

Loops / Repetition Statements

DECISION CONTROL AND LOOPING STATEMENTS

Unit 3 Decision making, Looping and Arrays

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

AMCAT Automata Coding Sample Questions And Answers

Control Structure: Loop

DECISION MAKING STATEMENTS

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Decision Making -Branching. Class Incharge: S. Sasirekha

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Computers Programming Course 7. Iulian Năstac

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Chapter 5: Control Structures

Dept. of CSE, IIT KGP

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

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

Java Control Statements

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

COMP 208 Computers in Engineering

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Government Polytechnic Muzaffarpur.

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

Repetition and Loop Statements Chapter 5

Questions Bank. 14) State any four advantages of using flow-chart

CS111: PROGRAMMING LANGUAGE II

Lecture 10. Daily Puzzle

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

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

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


ECET 264 C Programming Language with Applications. C Program Control

Comments. Comments: /* This is a comment */

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Principles of Computer Science

Programming for Experimental Research. Flow Control

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Structured Programming. Dr. Mohamed Khedr Lecture 9

Fundamental of Programming (C)

Fundamentals of Programming

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

Introduction. C provides two styles of flow control:

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

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

REPETITION CONTROL STRUCTURE LOGO

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

1 Unit 8 'for' Loops

Subject: PIC Chapter 2.

Computer Programming

A Look Back at Arithmetic Operators: the Increment and Decrement

MTH 307/417/515 Final Exam Solutions

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

CS16 Exam #1 7/17/ Minutes 100 Points total

Lecture 6. Statements

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

Repetition Structures II

Advanced Computer Programming

C 101. Davide Vernizzi. April 29, 2011

Module 4: Decision-making and forming loops

Introduction to the Java Basics: Control Flow Statements

C programming basics T3-1 -

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

M1-R4: Programing and Problem Solving using C (JAN 2019)

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

Crude Video Game Simulator Algorithm

6 COMPUTER PROGRAMMING

Loops / Repetition Statements

Introduction to Programming

Overview. CS 201 Repetition. Endfile-controlled loops. What is the Output? Nested Loops. Loops. Debzani Deb

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

EK131 E5 Introduction to Engineering

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

Why Is Repetition Needed?

Introduction to C Programming

Lecture 2: C Programming Basic

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

More examples for Control statements

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

Conditional Statement

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

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

Introduction to Programming

Control Structure: Selection

ECE 122. Engineering Problem Solving with Java

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Repetition Structures

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

Introduction to Computing Lecture 09: Functions (Part II)

More on control structures

do, while and for Constructs

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Transcription:

Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for loop -nested for loops 85-132 Decision Making and Loops 8-1 if-else-if Control Structure /* trivial example of if-else-if control structure */ #include <stdio.h> void main(void) int i; printf( input an integer value, 0, 1, or 10:\n ); scanf( %d,&i); if(i == 0) printf( you picked zero ); else if(i == 1) printf( you picked one ); else if(i == 10) printf( you picked 10 ); else printf( you did not pick any of the cases\n ); printf( try again ); 85-132 Decision Making and Loops 8-2 85-132: Introduction to C-Programming 1

switch Control Structure #include <stdio.h> // I removed white space to save space void main(void) int i; printf( input an integer value, 0, 1 or 10: \n ); scanf( %d,&i); switch(i) case 0: printf( you picked 0 ); break; case 1: printf( you picked 1 ); break; case 10: printf( you picked 10 ); break; default: printf( you did not pick any of the cases\n ); printf( try again ); 85-132 Decision Making and Loops 8-3 while Loop loop is an iterative control structure which involves repeated execution of one or more statements syntax for while loop while (expression) false true example i=1; while (i<=5) printf ( Loop number %d\n,i); i=i+1; /* alternatively */ i=1; while (i <= 5) printf( Loop number %d\n,i); i++; //same as i=i+1 Loop number 2 Loop number 3 Loop number 4 Loop number 5 85-132 Decision Making and Loops 8-4 85-132: Introduction to C-Programming 2

variable =0 while Loop additional syntax of while loop while (variable) variable 0 example i=3; while (i) // while(i) same as while (i!=0) printf ( Loop number %d\n,i); i--; Loop number 3 Loop number 2 85-132 Decision Making and Loops 8-5 while Loop if the while loop s test expression is false initially, the loop is never executed if the while loop s test expression never becomes false, an infinite loop is created and the program will stop example i=3; while (i) printf ( Loop number %d\n,i); i=i++; /* prevention of an infinite loop*/ //put a counter in the loop i=3; icount=0; while (i && icount<51) printf( Loop number %d\n,i); i++; icount++; 85-132 Decision Making and Loops 8-6 85-132: Introduction to C-Programming 3

do-while Loop do-while loops are almost identical to while loops except they are always executed at least once even if the while test expression is false syntax of do-while loop do while (expression); NOTE that the while test expression occurs at the end of the block and if it is false the block executes only once /* example of do-while loop*/ i=1; do printf( Loop number %d\n,i); i++; while(i>5); 85-132 Decision Making and Loops 8-7 for Loop for loops are used when you know how many times a loop needs to be executed syntax of for loop for (initialization; test expression; increment expression) /* example for loop */ for(i=1; i<=100; i++) printf( loop number %d\n,i); Loop number 2 Loop number 99 00 85-132 Decision Making and Loops 8-8 85-132: Introduction to C-Programming 4

for Loop Changing the increment expression /* example for loop */ /* example for loop */ for(i=1; i<=100; i+=2) printf( loop number %d\n,i); for(i=100; i>=0; i-=5) printf( loop number %d\n,i); Loop number 3 Loop number 5 Loop number 97 Loop number 99 00 Loop number 95 Loop number 90 Loop number 5 Loop number 0 85-132 Decision Making and Loops 8-9 Nested for Loop /* example of a nested for loop */ #include <stdio.h> void main (void) int i,j; 3X for(i=1; i<=3; i++) for(j=1; j<=4; j++) 4X printf( i = %d, j = %d\n,i,j); i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 1, j = 4 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 2, j = 4 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 i = 3, j = 4 85-132 Decision Making and Loops 8-10 85-132: Introduction to C-Programming 5

Your Tasks Read Sections 4.6 to 4.11 (Tan and D Orazio, 1999) Review this lecture Prepare for assignment 5 Make sure you review and keep up if you are not up-to-date and do not make an all out effort to catch up as of today, all I can wish you is good luck and we ll see you in the Appeals Committee 85-132 Decision Making and Loops 8-11 85-132: Introduction to C-Programming 6