Cunit II
Cunit II
1)For statement:-A for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.
Syntax
The syntax of a for loop in C programming language is –
Example
#include <stdio.h>
int main ( )
{
int a;
for( a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d\n", a);
}
return 0;
}
When the above code is compiled and executed, it produces the following result –
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
2 Looping Statements and Arrays
2)While statement:-A while loop in C programming repeatedly executes a target statement as long as a
given condition is true.
Syntax:- The syntax of a while loop in C programming language is –
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any nonzero value. The loop iterates while the condition is true.When the
condition becomes false, the program control passes to the line immediately following the loop.
Flow Diagram
Example:
#include <stdio.h>
int main ()
{ int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}
When the above code is compiled and executed, it produces the following result –
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
3 Looping Statements and Arrays
3)Do-While statement:-Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming checks its condition at the bottom of the loop.A do...while loop is
similar to a while loop, except the fact that it is guaranteed to execute at least one time.
Syntax:The syntax of a do...while loop in C programming language is –
do {
statement(s);
} while( condition );
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes
again. This process repeats until the given condition becomes false.
Flow Diagram
Example:
#include <stdio.h>
int main ()
{
int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
When the above code is compiled and executed, it produces the following result –
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
4 Looping Statements and Arrays
4)Break statement:-The break statement in C programming has the following two usages −
When a break statement is encountered inside a loop, the loop is immediately terminated and the
program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement (covered in the next chapter).
5)Continue Statement:-The continue statement in C programming works somewhat like the break
statement. Instead of forcing termination, it 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;
Array:-An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
Declaring Arrays:-To declare an array in C, a programmer specifies the type of the elements and
the number of elements required by an array as follows –
This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type. For example, to declare a 10-element array called
balance of type double, use this statement –
double balance[10];
Accessing Array Elements:-An element is accessed by indexing the array name. This is done by placing
the index of the element within square brackets after the name of the array. For example –
The above statement will take the 10th element from the array and assign the value to salary variable.
5 Looping Statements and Arrays
Manipulation of Array:-An element is accessed by indexing the array name. This is done by
placing the index of the element within square brackets after the name of the array.
The above statement will take the 10th element from the array and assign the value to salary
variable. The following example Shows how to use all the three above mentioned concepts viz.
declaration, assignment, and accessing arrays −
Example:-
#include <stdio.h>
int main ( )
{
int n[ 10 ];
int i,j;
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100;
}
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Program:
#include <stdio.h>
void arr(int x[2 ][3]);
int main ()
{
int a[2][3] = {{1,2,3}, {4,5,6}};
int b[2][3] = {1, 2, 3, 4, 5};
int c[2][3] = {{1, 2}, {4}};
printf("values in array a by row:\n");
arr(a);
printf("values in array b by row:\n");
arr(b);
printf("values in array c by row:\n");
arr(c);
return 0;
}
void arr(int x[ 2][3])
{
int i; //row counter
int j; //column counter
for (i = 0; i <= 1; ++i)
{
for (j = 0; j<= 2; ++j)
{
printf("%d", x[i][j]);
} //end of inner for
printf("\n");
} //end of outer for
}
Output:
Values in array a by row :
1 2 3
4 5 6
Values in array b by row :
1 2 3
4 5 0
Values in array c by row :
1 2 0
4 0 0
The most popular and commonly used multi-dimensional array is two-dimensional array. The 2-
D arrays are used to store data in the form of a table. We also use 2-D arrays to create
mathematical matrices.