For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.

Slides:



Advertisements
Similar presentations
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Spring Semester 2013 Lecture 5
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Computer Science 1620 Loops.
Nested Loops. Problem Print The only printfs you can use are: –printf(“*”); –printf(“\n”); *****
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Some loop programs 1: Read in 10 integers and output their sum
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Introduction to Computer Programming in c
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
do - while  while: Execute the body of the loop at least once
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Iterations Very Useful: Ability to repeat a block of code Example:
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Computer Programming Control Structure
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
Computer Programming -1-
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Control Structures (Repetition structure) Jump Statements
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Arrays, For loop While loop Do while loop
Programming Fundamentals Lecture #6 Program Control
Lec 7.
Loops in C.
Chapter 2.2 Control Structures (Iteration)
Week 6 CPS125.
Computer programming Lecture 3.
PROGRAM FLOWCHART Iteration Statements.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Department of Computer Science
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

For Loop Lecture No 8

Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop.

Three-expression for loops This type of for loop is found in nearly all languages which share a common heritage with the C programming language. It is characterized by a three- parameter loop control expression; consisting of an initializer, a loop-test, and a counting expression. A representative example in C is: This type of for loop is found in nearly all languages which share a common heritage with the C programming language. It is characterized by a three- parameter loop control expression; consisting of an initializer, a loop-test, and a counting expression. A representative example in C is: for (i = 0; i < 10; i++) for (i = 0; i < 10; i++) { /* loop body */ /* loop body */ }

Three-expression for loops The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even ifcontinue is called - and is usually responsible for altering the loop variable. The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even ifcontinue is called - and is usually responsible for altering the loop variable.

Flow Chart of For Loop

Syntax The syntax for a three-expression for loop is nearly identical in all languages that have it, after accounting for different styles of block termination and so on. The syntax for a three-expression for loop is nearly identical in all languages that have it, after accounting for different styles of block termination and so on. In C and C++ similar languages: In C and C++ similar languages: for (counter = 1; counter <= 5; counter++) //statement; for (counter = 1; counter <= 5; counter++) //statement;

For Loop Example 1 int i,j; int i,j; for(i=1; i<=10; i++) for(i=1; i<=10; i++) { printf(“\nA”); printf(“\nA”); } getch() getch() }

For Loop Example 2 Find product of number. The program must perform it at least 10 times. Find product of number. The program must perform it at least 10 times. int i,j,a; int i,j,a; for(i=1; i<=10; i++) for(i=1; i<=10; i++) { printf(“Enter value here = ”); printf(“Enter value here = ”); scanf(%d,&a); scanf(%d,&a); a=a*a; a=a*a; printf(“The product is %d”,a); printf(“The product is %d”,a); } getch() getch() }

Nested For Loops The nested for loop is called the loop inside the loop. e.g The nested for loop is called the loop inside the loop. e.g int i,j,a; int i,j,a; for(i=1; i<=10; i++) for(i=1; i<=10; i++) { printf(“ \n ”); printf(“ \n ”); for(j=1; j<=10; j++) for(j=1; j<=10; j++) printf(“A”); printf(“A”); } getch() getch() }

For Loop Examples 3 Make any Table (Take the Input from User) using (for loop)Print Make any Table (Take the Input from User) using (for loop)Print

#include #include void main() void main() { clrscr(); clrscr(); int a,b,c; int a,b,c; Printf("Enter the Value of Table=“); Printf("Enter the Value of Table=“); Scanf(“%d”,&a); Scanf(“%d”,&a); for (b=1; b<=10; b++) for (b=1; b<=10; b++) { c=a*b; c=a*b; Printf(“\n%d * %d = %d”,a,b,c); Printf(“\n%d * %d = %d”,a,b,c); } getch(); getch(); }

For Loop Example 4 Print Sum of Even and Odd Numbers from (for loop) Print Sum of Even and Odd Numbers from (for loop)

void main() {clrscr(); int a,c,d; c=0;d=0; for (a=10; a<=20; a++) {if(a%2==0){ c=c+a; c=c+a; }else{d=d+a;}} printf("Sum of Even Numbers from 10-20= %d “, c); printf("\nSum of Odd Numbers from 10-20= %d“, d); getch();}

For Loop Example 5 Make increasing stars arrangements as fallow? Make increasing stars arrangements as fallow? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int i,j; int i,j; clrscr(); clrscr(); for(i=1; i<=15; i++) for(i=1; i<=15; i++) { for(j=1; j<=i; j++) for(j=1; j<=i; j++) { printf(" *"); printf(" *"); } printf("\n"); printf("\n"); } getch(); getch(); }

For Loop Example 6 Make increasing stars arrangements as fallow? Make increasing stars arrangements as fallow? * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int i,j; int i,j; clrscr(); clrscr(); for(i=1; i<=10; i++) for(i=1; i<=10; i++) { { for(j=10; j>=i; j--) for(j=10; j>=i; j--) { { printf(" *"); printf(" *"); } } printf("\n"); printf("\n"); } } getch(); getch(); } }

Assignment Questions 1 Make mirrored shape of Example 4. Make mirrored shape of Example 4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Assignment Questions 2 Make mirrored shape of Example 5 Make mirrored shape of Example 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *