Prepared by: Shraddha Modi

Similar documents
Unit 3 Decision making, Looping and Arrays

DECISION CONTROL AND LOOPING STATEMENTS

DELHI PUBLIC SCHOOL TAPI

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

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

Decision Making and Loops

SELECTION STATEMENTS:

REPETITION CONTROL STRUCTURE LOGO

Computers Programming Course 7. Iulian Năstac

OER on Loops in C Programming

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

Module 4: Decision-making and forming loops

Government Polytechnic Muzaffarpur.

UIC. C Programming Primer. Bharathidasan University

Flow Control. CSC215 Lecture

Loops / Repetition Statements

Introduction. C provides two styles of flow control:

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

Subject: PIC Chapter 2.

Fundamentals of Computer Programming Using C

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

Why Is Repetition Needed?

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

A control structure refers to the way in which the Programmer specifies the order of executing the statements

Looping statement While loop

AMCAT Automata Coding Sample Questions And Answers

Loops / Repetition Statements

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

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

Subject: Fundamental of Computer Programming 2068

Principles of Computer Science

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

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

Programming Fundamentals

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

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

Prepared by: Shraddha Modi

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Repetition and Loop Statements Chapter 5

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

Dept. of CSE, IIT KGP

Lecture 10. Daily Puzzle

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

For Loop. Variations on Format & Specific Examples

Java Control Statements

C Programming Class I


CSE 130 Introduction to Programming in C Control Flow Revisited

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

Chapter 4: Control structures. Repetition

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

Chapter 5: Control Structures

CHAPTER : 9 FLOW OF CONTROL

Computer Programming Unit 3

Chapter 4: Control structures

CHAPTER 9 FLOW OF CONTROL

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

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

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

COMPUTER PROGRAMMING LOOPS

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

'C' Programming Language

Introduction to C/C++ Lecture 3 - Program Flow Control

A Look Back at Arithmetic Operators: the Increment and Decrement

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

UNIT 3 FUNCTIONS AND ARRAYS

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

Computer Programming & Problem Solving ( CPPS )

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

6 COMPUTER PROGRAMMING

Programming Using C UNIT-1

C++ PROGRAMMING SKILLS Part 2 Programming Structures

C: How to Program. Week /Mar/05

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

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

UNIT IV INTRODUCTION TO C

Functions. Arash Rafiey. September 26, 2017

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

Q 1. Attempt any TEN of the following:

Tutorial No. 2 - Solution (Overview of C)

OVERVIEW OF C. Reads a string until enter key is pressed Outputs a string

Structured Programming. Dr. Mohamed Khedr Lecture 9

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

UNIT - V STRUCTURES AND UNIONS

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

Chapter 4. Flow of Control

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

CSE 230 Intermediate Programming in C and C++ Functions

ENGR 1181 MATLAB 09: For Loops 2

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

LAB 5: REPETITION STRUCTURE(LOOP)

Class 9. Choose the correct answer:

Fundamentals of Programming Session 9

Repetition Structures II

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

3 The L oop Control Structure

Chapter 5: Control Structures II (Repetition)

DECISION MAKING STATEMENTS

Transcription:

Prepared by: Shraddha Modi

Introduction In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consist of two segments Body of the loop Control statement Two types of control structure: Entry-controlled loop Exit-controlled loop

For loop Entry-controlled loop Used when the number of iterations is predetermined. Syntax : for (initialization; test condition; increment/decrement) block of code next statement;

INITIALIZATION CONDITION FALSE True LOOP_BODY INCREMENT/ DECREMENT NEXT STATEMENT 4

Example: Output : void main() int i, n=5; for (i=0;i<n; i=i+1) printf ( The numbers are %d \n",i); The numbers are 0 The numbers are 1 The numbers are 2 The numbers are 3 The numbers are 4 5

Nested loop If a for loop is defined in another for loop, it is called nested for loop. The outer loop is executed first of all. The general format of nested for loop is: for (initialization; test condition; increment/decrement) for (initialization; test condition; increment/decrement) block of code block of code next statement;

Write a program to display the stars as shown below * * * * * * * * * * * * * * * * * * * * * * * * * void main( ) int x, i, j ; * * * * * printf( How many lines stars (*) should be printed? : ); scanf( %d, &x); for(i=1; i<=x; i++) for (j=1; j < =i; j++) printf( * ); printf( \n );

While Loop Entry-controlled loop Syntax : while(condition 1) body of the loop next statement; CONDITION True Body of Loop False NEXT STATEMENT

Example: void main() Output : int i = 0; while (i<5) printf(" the value of i is %d\n", i); i=i+1 ; the value of i is 0 the value of i is 1 the value of i is 2 the value of i is 3 the value of i is 4

Program to calculate factorial of a given number using while loop. void main ( ) int n, fact =1; clrscr( ) ; printf( \n Enter the Number: ); scanf( %d, &n); while(n>=1) fact = fact*n; n - - ; printf( \n factorial of given number is %d, fact); getch( );

Do-While Loop It tests the condition at the bottom of the loop after executing the body of the loop. we can be assured that the body of the loop is executed at least once. It is mainly used in menu like programs where all the available choices are printed at least once. Syntax : do statement; while(condition); Statement CONDITION False True

Example: void main() Output : int i,n = 5; i = 0; do printf("the numbers are %d \n",i); i=i +1; while( i<n) ; the numbers are 0 the numbers are 1 the numbers are 2 the numbers are 3 the numbers are 4

Program to calculate factorial of a given number using do while loop. void main ( ) int n, fact =1; clrscr( ) ; printf( \n Enter the Number: ); scanf( %d, &n); do fact = fact * n; n- -; while (n >= 1); printf( \n factorial of given number is %d, fact); getch( );

for loop while statement do-while statement Condition is evaluated at the beginning of the loop. Condition is evaluated at the beginning of the loop. Condition is evaluated at the end of the loop. A variable value is initialized at the beginning of the loop. Increment/Decrement is given at the beginning of the loop. The statement block will not be executed when the value of the condition is false. A variable value is initialized at the beginning or before the loop. Increment/Decrement is given inside the loop. The statement block will not be executed when the value of the condition is false. A variable value is initialized before the loop or assigned inside the loop. Increment/Decrement is given inside the loop. The statement block will executed at least once the value of the condition is false.

Example: for(i=1;i<=10;i++) s = s + i; p = p * i; Example: i =1; while(i<=10) s = s + i; p = p * i; i++; Example: i =1; do s = s + i; p = p * i; i++; while(i<=10);

Continue statement The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statement is encountered, instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. The continue statement can be included within a while, a do-while, a for statement. It is simply written as continue. The continue statement tells the compiler Skip the following Statements and continue with the next Iteration. In while and do loops continue causes the control to go directly to the test condition and then to continue the iteration process. In the case of for loop, the updation section of the loop is executed before test-condition, is evaluated.

(1) while (Test condition) - - - - - - - - if ( - - - - - - -) continue; -------------------- -------------------- (2) do - - - - - - - - if ( - - - - - - -) continue; -------------------- -------------------- while (Test condition); (3) for(initialization; test condition; increment) - - - - - - - - if ( - - - - - - -) continue; -------------------- --------------------

No. break statement continue statement 1. A break statement is used to terminate the execution of a statement block. The keyword is break; A continue statement is used to transfer the control to the beginning of a statement block. The keyword is continue; 2. When a break statement is executed in a loop, a repetition of the loop will be terminated. When a continue statement is executed in a loop, the execution is transferred to the beginning of the loop.

Example: #include<stdio.h> void main() int i = 1, j = 0; clrscr(); while(i<=5) i=i+1; if(i== 2) break; j=j+1; printf("value of i=%d and j=%d",i,j); getch(); OUTPUT: value of i=2 and j=0 Example: #include<stdio.h> void main() int i = 1, j = 0; clrscr(); while(i<=5) i=i+1; if(i== 2) continue; j=j+1; printf("value of i=%d and j=%d",i,j); getch(); OUTPUT: value of i=6 and j=4