OER on Loops in C Programming

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

Prepared by: Shraddha Modi

Flow Control. CSC215 Lecture

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

DELHI PUBLIC SCHOOL TAPI

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Introduction. C provides two styles of flow control:

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

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

Module 4: Decision-making and forming loops

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

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

REPETITION CONTROL STRUCTURE LOGO

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

Computer Programming: C++

Chapter 5: Control Structures

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

Computers Programming Course 7. Iulian Năstac

Control structures in C. Going beyond sequential

Chapter 3 Structured Program Development

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

Loops / Repetition Statements

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Chapter 4. Flow of Control

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

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

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

Information Science 1

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

Information Science 1

do, while and for Constructs

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 4: Control structures. Repetition

Chapter 4 C Program Control

DECISION CONTROL AND LOOPING STATEMENTS

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

BIL101E: Introduction to Computers and Information systems Lecture 8

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

Chapter 4: Control structures

M.CS201 Programming language

Why Is Repetition Needed?

LECTURE 5 Control Structures Part 2

Chapter 3. Iteration

Structured Program Development

Computer Science III WEEK4 Dr.Elgin KILIÇ

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

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

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

CS 103 Lecture 3 Slides

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

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

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

University of Kelaniya Sri Lanka

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

3 The L oop Control Structure

Lecture 6. Statements

- If you want to repeat the same blocks of code over and over you have two choices. Copy

Unit 3 Decision making, Looping and Arrays

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Computer Programming: C++


Dept. of CSE, IIT KGP

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

Loops / Repetition Statements

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

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

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

IT 1033: Fundamentals of Programming Loops

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

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

C Functions. 5.2 Program Modules in C

Repetition Structures

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

Decision Making -Branching. Class Incharge: S. Sasirekha

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

ENGR 1181 MATLAB 09: For Loops 2

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

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

More About WHILE Loops

Structured Programming. Dr. Mohamed Khedr Lecture 9

Structured Program Development in C

EL2310 Scientific Programming

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 2: Functions and Control Structures

Government Polytechnic Muzaffarpur.

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

Introduction to C Programming

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Loops. CSE 114, Computer Science 1 Stony Brook University

Reminder the scope of a variable is the part of the program where that variable is visible Will this compile?

Fundamentals of Computer Programming Using C

EK131 E5 Introduction to Engineering

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:

OER on Loops in C Programming Prepared by GROUP ID 537 B. Bala Nagendra Prasad (bbalanagendraprasad@gmail.com C. Naga Swaroopa (swarupabaalu@gmail.com) L. Hari Krishna (lhkmaths@gmail.com)

1 Table of Contents Objectives p. no. 2 Introduction to Loops p. no 3 While Loop p. no. 4 Example p. no. 5 For Loop p. no. 6 Example p. no. 8 Do..While Loop p. no. 9 Example p. no. 10 Assignment p. no. 11

2 Objectives: This section brings together the various looping mechanisms available to the C programmer with the program control constructs we met in the last section. It is one of the great discoveries of programming that you can write any program using just simple while loops and if statements. You don't need any other control statements at all. Of course it might be nice to include some other types of control statement to make life easy - for example, you don't need the for loop, but it is good to have! So as long as you understand the if and the while loop in one form or another you can write any program you want to. If you think that a loop and an if statement are not much to build programs then you are missing an important point. It's not just the statements you have, but the way you can put them together. You can include an if statement within a loop, loops within loops are also OK, as are loops in ifs, and ifs in ifs and so on. This putting one control statement inside another is called nesting and it is really what allows you to make a program as complicated as you like.

3 You may encounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages

4 While Loop A while loop in C programming repeatedly executes a target statement as long as a given condition is true. Syntax The syntax of a while loop in C programming language is while(condition) { statement(s); Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop. Flow Diagram Here, the key point to note is that a while loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

5 Example #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; return 0; When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19

6 For Loop A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax The syntax of a for loop in C programming language is for ( init; condition; increment ) { statement(s); Here is the flow of control in a 'for' loop The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop. After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

7 Flow Diagram

8 Example #include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ){ printf("value of a: %d\n", a); return 0; When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19

9 Do.While loop Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time. Syntax The syntax of a do...while loop in C programming language is do { statement(s); while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false. Flow Diagram

10 Example #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { printf("value of a: %d\n", a); a = a + 1; while( a < 20 ); return 0; When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19

11 Assignment of the following examples, write equivalent code using a while loop instead. (a) int factorial ( int n) int i, ret =1; for (i =2; i <= n; i++) { ret = i; return ret ; (b) #include <stdlib.h> double rand double () { / generate random number in [0,1) / double ret =(double )rand (); return ret /(RANDMAX+1); int sample geometric rv(double p) { double q;int n= 0;do { q = rand double (); n++; while (q >= p);return n;