Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays, For loop While loop Do while loop

Similar presentations


Presentation on theme: "Arrays, For loop While loop Do while loop"— Presentation transcript:

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

2 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.

3 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.

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

5 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.

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

7 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};

8 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.

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

10 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.

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

12 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.

13 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;

14 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.

15 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++; }

16 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++; }

17 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--)

18 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.

19 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;

20 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.

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

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

23 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.

24 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].


Download ppt "Arrays, For loop While loop Do while loop"

Similar presentations


Ads by Google