Arrays, For loop While loop Do while loop

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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,
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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”
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Problem Solving and Control Statements: Part 2
CSE1320 Loop Dr. Sajib Datta
Loop Structures.
Chapter 4 - Program Control
Week 4 – Repetition Structures / Loops
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
JavaScript: Control Statements.
Looping.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Looping and Repetition
Outline Altering flow of control Boolean expressions
Declaration, assignment & accessing
Chapter 4 - Program Control
Loops in C.
3 Control Statements:.
Computing Fundamentals
2.6 The if/else Selection Structure
CMPE212 – Reminders The other four assignments are now posted.
Chapter 4 - Program Control
Introduction to Programming
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Looping and Repetition
Presentation transcript:

Arrays, For loop While loop Do while loop c Arrays, For loop While loop Do while loop

Arrays: Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. Means it is collection of same type data and accessed using a common name.

Arrays: Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

Arrays: In array, elements are stored at continuous memory locations. A specific element in an array is accessed by an index.

Arrays: (declaration) Arrays delectation same as normal variable declaration. Here we specify the array size along with declaration. type arrayName [ arraySize ]; The array Size must be an integer constant greater than zero and type can be any valid C data type.

Arrays: (declaration) Example   float mark[5]; // floating array int a[10]; // integer array char b[10]; // character array i.e. string

Arrays: Initialize an array in C programming: It's possible to initialize an array during declaration. For example, int mark[5] = {19, 10, 8, 17, 9}; Another method to initialize array during declaration: int mark[] = {19, 10, 8, 17, 9};

Arrays: index Elements of an array are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name.   Array indices start at zero in C, and go to one less than the size of the array. For example, a five element array will have indices zero through four.

Loops: A loop is used for executing a block of statements repeatedly until a given condition returns false.

Loop Type & Description Loops: C programming language provides the following types of loops to handle looping requirements. Loop Type & Description 1 while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 2 for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. 3 do...while loop It is more like a while statement, except that it tests the condition at the end of the loop body.

For loop: for (initializationStatement; testExpression; updateStatement) { // codes }

For loop: The initialization statement is executed only once. Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated.

For loop: This process repeats until the test expression is false. The for loop is commonly used when the number of iterations is known. #include <stdio.h> int main() { int i; for (i=1; i<=3; i++) printf("%d\n", i); } return 0;

For loop: different ways to declare for loop 1) Here instead of num++, I’m using num=num+1 which is same as num++. for (num=10; num<20; num=num+1) 2) Initialization part can be skipped from loop as shown below, the counter variable is declared before the loop. int num=10; for (;num<20;num++) Note: Even though we can skip initialization part but semicolon (;) before condition is must, without which you will get compilation error.

For loop: different ways to declare for loop 3) Like initialization, you can also skip the increment part as we did below. In this case semicolon (;) is must after condition logic. In this case the increment or decrement part is done inside the loop. for (num=10; num<20; ) { //Statements num++; }

For loop: different ways to declare for loop 4) This is also possible. The counter variable is initialized before the loop and incremented inside the loop. int num=10; for (;num<20;) { //Statements num++; }

For loop: different ways to declare for loop 5) As mentioned above, the counter variable can be decremented as well. In the below example the variable gets decremented each time the loop runs until the condition num>10 returns false. for(num=20; num>10; num--)

Multiple initialization inside for Loop in C We can have multiple initialization in the for loop as shown below. for (i=1,j=1;i<10 && j<10; i++, j++) It is initializing two variables. Note: both are separated by comma (,). It has two test conditions joined together using AND (&&) logical operator. Note: You cannot use multiple test conditions separated by comma, you must use logical operator such as && or || to join conditions. It has two variables in increment part. Note: Should be separated by comma.

Example of for loop with multiple test conditions #include <stdio.h> int main() { int i,j; for (i=1,j=1 ; i<3 || j<5; i++,j++) printf("%d, %d\n",i ,j); } return 0;

While loop: A while statement is a repeating if statement. Like an If statement, if the test condition is true: the statements get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again. This cycle repeats until the test condition evaluates to false.

While loop (Syntax ): while (condition test) { //Statements to be executed repeatedly // Increment (++) or Decrement (--) Operation }

While loop (Example): #include <stdio.h> int main() { int count=1; while (count <= 4) printf("%d ", count); count++; } return 0;

do...while loop do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. Here content of the loop are always executed at least once. In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop.

Assignments Write a sample program on array concept, just reading and displaying integer and character data [2M]. Write a program for finding sum of n number using for, while and do while loops [5M] Write a program to print table suing all loop concepts [5M]. Write a program to find factorial of a given number using loops concept [3M]. Write a program on array concept using loops for reading and displaying array data [5M].