|
A while loop is the most straightforward looping structure. The basic format of while loop is as follows: while (condition) { statements; } It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop. After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body. In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. It is different in do while loop which we will see shortly. Following program illustrates a while loop: #include<stdio.h> #include<conio.h> int main() { int num=1; //initializing the variable while(num<=10) //while loop with condition { printf("%d\n",num); num++; //incrementing operation } return 0; } Output: 1 2 3 4 5 6 7 8 9 10 The above program illustrates the use of while loop. In the above program, we have printed series of numbers from 1 to 10 using a while loop.
\n is used for formatting purposes which means the value will be printed on a new line. |
A do-while loop is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop. The basic format of while loop is as follows: do { statements } while (expression); As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false. This type of operation can be achieved by using a do-while loop. In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop. Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed. The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;) The following program illustrates the working of a do-while loop: We are going to print a table of number 2 using do while loop. #include<stdio.h> #include<conio.h> int main() { int num=1; //initializing the variable do //do-while loop { printf("%d\n",2*num); num++; //incrementing operation }while(num<=10); return 0; } Output: 2 4 6 8 10 12 14 16 18 20 In the above example, we have printed multiplication table of 2 using a do-while loop. Let's see how the program was able to print the series.
|
A for loop is a more efficient loop structure in 'C' programming. The general structure of for loop is as follows: for (initial value; condition; incrementation or decrementation ) { statements; }
Following program illustrates the use of a simple for loop: #include<stdio.h> int main() { int number; for(number=1;number<=10;number++) //for loop to print 1-10 numbers { printf("%d\n",number); //to print the number } return 0; } Output: 1 2 3 4 5 6 7 8 9 10 The above program prints the number series from 1-10 using for loop.
|
The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. The break statement in C can be used in the following two scenarios:
Syntax:
Flowchart of break in c Example
Output 0 1 2 3 4 5 came outside of loop i = 5 Example of C break statement with switch case C break statement with the nested loop In such case, it breaks only the inner loop, but not outer loop.
Output 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3 As you can see the output on the console, 2 3 is not printed because there is a break statement after printing i==2 and j==2. But 3 1, 3 2 and 3 3 are printed because the break statement is used to break the inner loop only. break statement with while loop Consider the following example to use break statement inside while loop.
Output 0 1 2 3 4 5 6 7 8 9 came out of while loop break statement with do-while loop Consider the following example to use the break statement with a do-while loop.
Output 2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10 2 X 6 = 12 2 X 7 = 14 2 X 8 = 16 2 X 9 = 18 2 X 10 = 20 do you want to continue with the table of 3 , enter any non-zero value to continue.1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 3 X 4 = 12 3 X 5 = 15 3 X 6 = 18 3 X 7 = 21 3 X 8 = 24 3 X 9 = 27 3 X 10 = 30 do you want to continue with the table of 4 , enter any non-zero value to continue.0 |
The continue statement in C language is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition. Syntax:
Continue statement example 1
Output infinite loop Continue statement example 2
Output 1 2 3 4 6 7 8 9 10 As you can see, 5 is not printed on the console because loop is continued at i==5. C continue statement with inner loop In such case, C continue statement continues only inner loop, but not outer loop.
Output 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3 As you can see, 2 2 is not printed on the console because inner loop is continued at i==2 and j==2. |
In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages. Advantage of functions in C There are the following advantages of C functions.
Function Aspects There are three aspects of a C function.
The syntax of creating function in c language is given below:
Types of Functions There are two types of functions in C programming:
|
#include<stdio.h> int even_odd(int); void main() { int n,flag=0; printf("\nGoing to check whether a number is even or odd"); printf("\nEnter the number: "); scanf("%d",&n); flag = even_odd(n); if(flag == 0) { printf("\nThe number is odd"); } else { printf("\nThe number is even"); } } int even_odd(int n) { if(n%2 == 0) { return 1; } else { return 0; } } Output Going to check whether a number is even or odd Enter the number: 100 The number is even |
#include<stdio.h> void average(int, int, int, int, int); void main() { int a,b,c,d,e; printf("\nGoing to calculate the average of five numbers:"); printf("\nEnter five numbers:"); scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); average(a,b,c,d,e); } void average(int a, int b, int c, int d, int e) { float avg; avg = (a+b+c+d+e)/5; printf("The average of given five numbers : %f",avg); } Output Going to calculate the average of five numbers: Enter five numbers:10 The average of given five numbers : 30.000000 |
Call by value in C
Let's try to understand the concept of call by value in c language by the example given below:
OutputBefore function call x=100 Before adding value inside function num=100 After adding value inside function num=200 After function call x=100 Call by Value Example: Swapping the values of the two variables
OutputBefore swapping the values in main a = 10, b = 20 After swapping values in function a = 20, b = 10 After swapping values in main a = 10, b = 20 Call by reference in C
Consider the following example for the call by reference.
OutputBefore function call x=100 Before adding value inside function num=100 After adding value inside function num=200 After function call x=200 Call by reference Example: Swapping the values of the two variables
OutputBefore swapping the values in main a = 10, b = 20 After swapping values in function a = 20, b = 10 After swapping values in main a = 20, b = 10 |