COMPUTER PROGRAMMING LOOPS

Similar documents
COMPUTER PROGRAMMING QUICK GUIDE COMPUTER PROGRAMMING OVERVIEW

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Preview from Notesale.co.uk Page 2 of 79

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

8. Control statements

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

CP122 CS I. Iteration

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Java Bytecode (binary file)

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Introduction to the Java Basics: Control Flow Statements

DELHI PUBLIC SCHOOL TAPI

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

PROGRAMMING FUNDAMENTALS

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Prepared by: Shraddha Modi

Introduction. C provides two styles of flow control:

Subject: PIC Chapter 2.

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

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

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Programming Basics. Digital Urban Visualization. People as Flows. ia

Following is the general form of a typical decision making structure found in most of the programming languages:

Unit 3 Decision making, Looping and Arrays

SDKs - Eclipse. SENG 403, Tutorial 2

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CMPT 125: Lecture 4 Conditionals and Loops

COMP 110 Project 1 Programming Project Warm-Up Exercise

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CS 177 Recitation. Week 1 Intro to Java

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

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

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Programming with Java

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

YOLOP Language Reference Manual

Pace University. Fundamental Concepts of CS121 1

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Controls Structure for Repetition

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Computer Science II Lecture 1 Introduction and Background

PYTHON MOCK TEST PYTHON MOCK TEST III

Introduction to Java & Fundamental Data Types

CS111: PROGRAMMING LANGUAGE II

Simple Java Reference

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

Problem Solving and 'C' Programming

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

ENGR 1181 MATLAB 09: For Loops 2

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Preview from Notesale.co.uk Page 3 of 36

Java Basic Programming Constructs

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Working with JavaScript

CSEN 202 Introduction to Computer Programming

CS 251 Intermediate Programming Java Basics

Instructor: Eng.Omar Al-Nahal

Computer Science is...

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Software and Programming 1

CS1150 Principles of Computer Science Loops (Part II)

Object-oriented programming in...

GUAVA - RANGE CLASS. Range represents an interval or a sequence. It is used to get a set of numbers/ strings lying in a particular range.

Getting started with Java

An overview of Java, Data types and variables

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Welcome to Python 3. Some history

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Example: Monte Carlo Simulation 1

Full file at

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

C and C++ I. Spring 2014 Carola Wenk

TESTING AND DEBUGGING

Key Differences Between Python and Java

Condi(onals and Loops

204111: Computer and Programming

CS111: PROGRAMMING LANGUAGE II

Question: Total Points: Score:

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Java+- Language Reference Manual

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Strings, Strings and characters, String class methods. JAVA Standard Edition

DECISION CONTROL AND LOOPING STATEMENTS

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Decision Making and Loops

Starting chapter 5. l First open file, and say purpose read or write inputfile = open('mydata.txt', 'r') outputfile = open('myresults.

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

C-Language Tutorial. Apple Macintosh system software was formerly written in Pascal, but is now almost always written in C.

Programming in the Small II: Control

AP CS Unit 3: Control Structures Notes

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Introduction to Java

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Transcription:

COMPUTER PROGRAMMING LOOPS http://www.tutorialspoint.com/computer_programming/computer_programming_loops.htm Copyright tutorialspoint.com Let's consider a situation when you want to write five times. Here is a simple C program to the same: printf( "\n"); printf( "\n"); printf( "\n"); printf( "\n"); printf( "\n"); It was simple, but again let's consider another situation when you want to write thousand times, what you will in such situation? Are we going to write printf statement thousand times? No, not at all. Almost all the programming languages provide a concept called loop, which helps in executing one or more statements up to desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly. Let's write above C program with the help of a while loop and later we will discuss how es this loop work: while ( i < 5 ) printf( "\n"); Above program makes use of while loop, which is being used to execute a set of programming statements enclosed within... Here, computer first checks whether given condition, i.e., variable "a" is less than 5 or not and if it finds condition is true then the loop body is entered to execute given statements. Here, we have the following two statements in the loop body:

First statement is printf function, which prints Hello World! Second statement is i = i + 1, which is used to increase the value of variable i After executing all the statements given in the loop body, computer goes back to whilei < 5 and given condition, i < 5, is checked again, and the loop is executed again if condition is true. This process repeats till the given condition remains true which means variable "a" has a value less than 5. To conclude, a loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages: This tutorial has been designed to present programming's basic concepts to non-programmers, so let's discuss about two important loops available in C programming language. Once you are clear about these two loops, then you can pickup C programming tutorial or a reference book and check what are other loops available in C and how they work. The while Loop A while loop available in C Programming language has following syntax: while ( condition ) /*...while loop body...*/ Above code can be represented in the form of a flow diagram as shown below:

There are following important points to note about a while loop: A while loop starts with a keyword while followed by a condition enclosed in. Further to while statement you will have body of the loop enclosed in curly braces... A while loop body can have one or more lines of source code to be executed repeatedly. If while loop body has just one line, then its optional to use curly braces... A while loop keeps executing its body till given condition is true. As soon as condition becomes fast, while loop comes out and continue executing from immediate next statement after while loop body. A condition is usually a relational statement, which is evaluated to either true or false values. A value equal to zero is treated as false and any non-zero value works like a true for the condition. The...while Loop If you have noted while loop, it checks given condition before it executes given statements of the code. C programming provides another form of loop, which is called...while loop and allows to execute a loop body before checking given condition. This has following syntax: /*...while loop body...*/ while ( condition ); Above code can be represented in the form of a flow diagram as shown below:

If you will write above example using...while loop, then Hello, World will produce the same result: printf( "\n"); while ( i < 5 ); The break statement When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. The syntax for a break statement in C is as follows: break; A break statement can be represented in the form of a flow diagram as shown below: Following is a variant of the above program, but it will come out after printing Hello World! only three times:

printf( "\n"); if( i == 3 ) break; while ( i < 5 ); The continue statement The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. The syntax for a continue statement in C is as follows: continue; A continue statement can be represented in the form of a flow diagram as shown below: Following is a variant of the above program but it will skip printing when variable has a value equal to 3: if( i == 3 ) continue; printf( "\n");

while ( i < 5 ); Loops in Java Following is the equivalent program written in Java programming language. Java programming language also provides while and...while loops. Following program will be used to print Hello, World! five times as we did in case of C Programming: You can try to execute following program to see the output, which must be identical to the result generated by the above example. public class DemoJava public static void main(string []args) while ( i < 5 ) System.out.println(""); The break and continue statements in Java programming work very similar way, what they work in C programming. Loops in Python Following is the equivalent program written in Python. Python also provides while and...while loops. Following program will be used to print five times as we did in case of C Programming. Here you must note that Python programming es not make use of curly braces for loop body, instead it simply identifies the body of the loop using indentation of the statements. You can try to execute following program to see the output. To show the difference I used one more print statement, which will be executed when loop will be over. i = 0 while (i < 5): print "" i = i + 1 print "Loop ends" Loop ends

The break and continue statements in Python programming work very similar way as they work in C programming. Loading [MathJax]/jax/output/HTML-CSS/jax.js