Chapter 8 The Loops By: Mr. Baha Hanene.

Slides:



Advertisements
Similar presentations
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Advertisements

CS150 Introduction to Computer Science 1
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
By: Mr. Baha Hanene Chapter 4. LEARNING OUTCOMES This chapter will cover learning outcome no. 2 i.e. Use basic data-types and input / output in C programs.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Chapter 5 Repetition or loop structure. What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Count and add list of numbers From user input and from file.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
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;
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The ‘while’ loop ‘round and ‘round we go.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Repetition statements
ECE Application Programming
REPETITION CONTROL STRUCTURE
ECE Application Programming
while Repetition Structure
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Formatted Input/Output
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Repetition-Counter control Loop
Chapter 2.2 Control Structures (Iteration)
Formatted Input/Output
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Control Structures Lecture 7.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Looping.
The while Looping Structure
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Lec 7.
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
UMBC CMSC 104 – Section 01, Fall 2016
Alternate Version of STARTING OUT WITH C++ 4th Edition
Functions, Part 1 of 3 Topics Using Predefined Functions
CPS125 Week
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Lec 6 Loop Statements Introduction to Computer Programming
The while Looping Structure
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
EECE.2160 ECE Application Programming
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Getting Started With Coding
Presentation transcript:

Chapter 8 The Loops By: Mr. Baha Hanene

Learning Outcomes In this chapter we will cover learning outcome no. 4 Use the program control structures of Sequences, Selections and Repetitions as well as simple functions to develop C programs to meet specified tasks. L04

THE WHILE LOOP Initialization (counter) THE FORMAT Initialization (counter) while (<testing> or <condition>) { body for the loop (statements) increment for the counter } EXAMPLE count = 0; while (count < = 10) { printf (“ \n %i ”, count); count++; or count=count+1; }

THE OUTPUT

THE FOR LOOP THE FORMAT for (<initialization>; <testing>; <increment>) { body for the loop (statements) } EXAMPLE for (count = 0; count < = 10; count++) { printf (“ \n %i ”, count); }

THE OUTPUT

THE DO-WHILE LOOP Initialization (counter) do { THE FORMAT Initialization (counter) do { body for the loop (statements) increment for the counter } while (<testing> or <condition>); EXAMPLE count = 0; do { printf (“ \n %i ”, count); count++; or count=count+1; } while (count<=10);

THE OUTPUT

Comparison (While Loop Vs. For Loop) If we want to display the numbers from 0 to 10 then both loop statements will be as below. int count = 0; while ( count < = 10 ) { printf (“ \n %i ”, count); count++; } Initialization Increment int count; for (count = 0; count < = 10; count++) { printf (“ \n %i ”, count); }

A Sample Program Q: Write a program to display numbers from zero to ten. #include <stdio.h> void main() { int num; for (num = 0; num < = 10; num++) printf (“ \n %d ”, num); }

THE OUTPUT

Q: Change the previous program to prompt the user to enter the last number and then display the numbers from 0 to the last number. #include <stdio.h> void main() { int num, last ; printf(“\n Enter The Last Number = ”); scanf(“%d”, & last); for (num = 0; num < = last; num++) { printf (“ \n %d ”, num); }

THE OUTPUT

Q: Again change the previous program to prompt the user to enter the first & the last number and then display the numbers accordingly. #include <stdio.h> void main() { int num, first, last ; printf(“\n Enter The First Number = ”); scanf(“%d”, & first); printf(“\n Enter The Last Number = ”); scanf(“%d”, & last) for (num = first; num < = last; num++) { printf (“ \n %d ”, num); }

THE OUTPUT

Q: Write a program to get three numbers from the user, add them together & print their total & average.

THE OUTPUT

Q: Write a program to get three numbers from the user, add them together & print their total & average.

THE OUTPUT

GUESS THE OUTPUT 1) for (a=0; a=5; a++) 3) for (a=1; a<=5; a++) Which code from the following prints “Hello” 5 times? 1) for (a=0; a=5; a++) printf(“\n Hello”); 3) for (a=1; a<=5; a++) 2) for (a=0; a==5; a++) 4) for (a=1; a==5; a+=2) Answer : 3

What is the output for the following? # include <stdio.h> void main() { int i, y, z=0; for (i=5; i<10; i++) { y = ++z; printf(“%d ”,y); } Output : 1 2 3 4 5

Q: Write the program to read the salary of ten employees, add them and then display the total & average of all the salaries.

THE OUTPUT

Q: Write a program to display odd numbers between a range, whereas the range must be input by the user.

THE OUTPUT

To change the odd number program into even number, the following places we need to change….

Q: Write a program to display the odd or even numbers with the count of displayed numbers.

THE OUTPUT

Q: What is the output for the following program? j k l 1 2 3 2 4 6 For Loop Start i = 0 4 6 8 i = 2 6 8 10 i = 4 8 10 12 i = 6 10 12 14 i = 8 12 14 16