Skip to main content

Posts

Showing posts from May, 2021

Infinite Loops in C

An infinite loop is a looping construct that does not terminate and executes the loop forever. It is also called an indefinite loop or an endless loop . It either produces a continuous output or no output. When to use an infinite loop An infinite loop is useful for those applications that accept the user input and generate the output continuously until the user exits from the application manually.  In the following situations, this type of loop can be used: All the operating systems run in an infinite loop as it does not exist after performing some task. It comes out of an infinite loop only when the user manually shuts down the system. All the servers run in an infinite loop as the server responds to all the client requests. It comes out of an indefinite loop only when the administrator shuts down the server manually. All the games also run in an infinite loop. The game will accept the user requests until the user exits from the game. We can create an infinite loop through various lo

Prgoram transfer control(Jump) statements, break, continue ,goto and exit function

break Statement The break statement is used with in switch statement to transfer program control out of switch statement after executing a set  of statements mentioned in a particular case. break statement can also be used in looping statements to transfer program control out of the loop. This will help to terminate a loop when a particular condition is met. Example: This program will check whether the given number contain digit 0.It is noted  that when the number contain first 0, we can stop doing the process and print that the number contains 0.There is no point in continuing the loop and hence computation time can be saved. #include <stdio.h> main()  {     int n,digit;     printf("Enter an integer: ");     scanf("%d", &n);     while (n != 0) {         digit= n % 10;           n = n/10;         if (digit==0) {                printf("number contains 0\n");                       break;                       }           }      } o/p Enter an in

Looping (iteration )Statements while,do while and for ,sample programs using loops and programs to try

Loops are used in programming to repeat a specific block of statements until some end condition is met.    Types of Loops in C Depending upon the position of a control statement in a program, looping in C is classified into two types:   1. Entry controlled loop 2. Exit controlled loop In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop. In an exit controlled loop , a condition is checked after executing the body of a loop. It is also called as a post-checking loop. The control conditions must be well defined and specified otherwise the loop will execute an infinite number of times. The loop that does not stop executing and processes the statements number of times is called as an infinite loop . An infinite loop is also called as an " Endless loop ."   There are three looping statements in C programming: while loop ( entry controlled) do...while loop ( exit controlled) for loop ( entry control

switch statement in C,sample programs and programs to try

There is one potential problem with the if-else statement which is the complexity of the program increases whenever the number of alternative path increases. If you use multiple if-else constructs in the program, a program might become difficult to read and comprehend. Sometimes it may even confuse the developer who himself wrote the program. The solution to this problem is the switch statement. A switch statement is used for multiple way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type. Syntax: switch (expression) { case value1:                   code segment1;                   break; case value2:                  code segment2;                  break; case valueN:                  code segmentN;                  break; default:                 default code segment; } The value of this expression is either generated duri

Structure of a C program

Lets understand the  basic structure of a C program. A C program is divided into different sections. There are six main sections to a basic c program. The six sections are, Documentation Include/Link/Preprocessor     section Definition Global Declarations ( Functions and variables) Main function Subprogram functions     Now let us learn about each of this layer in detail. Figure: Basic Structure Of C Program Sample Program The C program here will find the area of a circle using a user-defined function and a global variable PI holding the value of pi /* File Name: areaofcircle.c Author: Abhijith date: 06/05/2021   description: a program to calculate area of circle user enters the radius */ #include<stdio.h>   //link section #define PI   3.14;//definition section float area(float r);//global declaration /****************************/ int main()//main function { float r; printf(" Enter the radius:\n"); scanf("%