basic c programs for beginners
Control structures in C

The control structures determine the order in which the various instructions in a C program are to be executed. In other words, they determine the flow of control in a C program. The different control structures are as follows:

  1. Sequential control structure
  2. Conditional branching structure
  3. Unconditional branching structure
  4. Loop structure

Sequential control structure

This type of control structure ensures that the instructions are executed in the same order in which they appear in a C program.

Conditional branching structure

This type of control structure allows us to make the decision as to which instruction is to be executed next. The decision-making structures are as follows:

If structure

Syntax:

if (condition)
{
statements;
}

Here, if the given condition is true then the statements are given within the brace will be executed. There is no need to write the pair of brace is there is only one statement to be executed.

e.g.

if (i>5)
printf(“The value of i is greater than 5”);

If-else structure

Syntax:

if (condition)
{
statements;
}
else
{
statements;
}

The if statement itself executes a single or a group of statements when the given condition is true, it does nothing when the condition is false. The purpose of the else statement is to execute the set of statements written after it even when the condition is false. There is no need to write the pair of brace if there is only one statement to be executed.

e.g.

if(i>5)
printf(“The value of i is greater than 5”);
else
printf(“The value of i is less than or equal to 5”);

Nested if structure

If we write an entire if or if-else structure either within the body of an if statement or the body of an else statement then this is called nested if structure.

Syntax:

if(condition)
{
statements;
if(condition)
}

e.g.

if(j>5)
if(j>10)
{
printf(“The value of i is greater than 5 and”);
printf(“The value of j is greater than 10.”);
}

Switch-case structure

Syntax:

switch(expression)
{
case value1: statements;
break;
case value2: statements;
break;
…………………………………….
…………………………………….
default: statements;
break
}

It is also known as a multi-way branching statement. It consists of expression in “switch” and the constant values in “case” statements. This structure can have any number of case statements based on the values of the given expression. If the expression in the “switch” statement matches any of the constant values in the “case” statement, then the statement, the statement after the “case” statement will be executed. But, if any of the constant values do not match with the given expression then the statement written in the “default” section will be executed. A “break” statement is necessary at the end of each “case” and also in the “default” section. In this structure, the “default” section is totally optional.

e.g.

switch (n) /*here n is a variable*/
{
case 0: printf(“Value of n is 0”);
break;
case 1: printf(“Value of n is 1”);
break;
………………………………
………………………………
case 9: printf(“Value of n is 9”);
break;
default: printf(“Value of n is not between 0-9”);
break;
};

Unconditional branching statements

Generally, it should not be supported as it makes the user confuse about the program because it moves the control to another location. These statements should be used only within the control statements otherwise the user may not get a perfect requirement. There are three types of unconditional branching statements and they are,

goto

syntax:

goto<label-name>;
……………………..
<label>
……………………..

It moves the control to the required statement based on the label name. same label name with “goto” statement should not be used twice. The label name can be used before or after the “goto” statement.

e.g.

if(j>5)
goto lb1;
lb1;
printf(“Value of j is greater than 5”);

continue

syntax:

continue

The block of statement will be executed continuously without breaking the loop in a loop structure.

e.g.

k=0;
while(k<5)
{
printf(“This is an example”);
printf(“of continue statement!”);
continue;
k++;
}

break

syntax:

break;

it is used to move the control to the outside of a control structure.

e.g.

k=0;
while(k<5)
{
printf(“This is an example”);
printf(“of a break statement!”)
break;
k++;
}

Loop control structure

The versatility of a computer lies in its ability to perform a set of instructions repeatedly. This involves some portion of the program executes either a specified number of times or until a particular condition is satisfied. This type of repetitive operation is done through a loop control structure. The three methods are as follows:

while loop structure:

syntax:

while(condition)
{
statements;
}

Here all the block statements will be executed as long as the given condition is true. Once the condition becomes false the control exits from the “while” structure. We can also use the nested “while” loop if necessary.

e.g.

x=0;
while(x<5)
{
printf(“This is an example”);
printf(“of while loop structure!”);
x++;
}

do-while loop structure

syntax:

do
{
statements;
}

There is a minor difference between the working style of “while” and “do while” loop. This difference is the place where the condition is tested. The “while” tests the given condition before executing any of the statements within the loop, whereas “do while” tests the given condition after executing the statements within the loop at least for once. Here all the block statements will be executed at least once even if the given condition is false. We can also use the nested “do while” loop if necessary.

e.g.

z=5;
do
{
printf(“This is an example”);
printf(“of do-while loop structure!”);
z++;
}

for loop structure

syntax:

for(initialize;condition;increment/decrement)
{
statements;
}

There are three parts in a “for” loop,

  1. initialization part, where we can initialize any variable, like a=0 or b=5, etc.
  2. the condition part, where it is tested that the given condition is true or false. As long as the given condition remains true, all the statements within the loop will be executed.
  3. Increment/decrement part, where we can increase or decrease the value of a variable of our choice.

e.g.

for(j=0;j<10;j++)
{
printf(“This is an example”);
printf(“of for loop structure”);
}

The do while loop and a 100 Hello Worlds

#include<stdio.h>
main()
{
int count;
count=0;
do
{
++count;
Printf(“Hello World!\n”);
}
While(count<100);
}

This program demonstrates the for loop

#include<stdio.h>
main()
{
int fahr;
for(fahr=0;fahr<=300;fahr=fahr+20)
printf(“%4d%6.1f\n”, fahr, (5.0/9.0)*(fahr-32));
}

This program demonstrates the while loop

#include<stdio.h>
Main()
{
Int lower, upper, step;
Float fahr, Celsius;
Lower=0;
Upper=300;
Step=20;
Fahr=lower;
While(fahr<=upper)
{
Celsius=(5.0/9.0)*(fahr-32.0);
Printf(“%d.0f%6.1f\n”, fahr, celsius);
Fahr=fahr+step;
}
}

This program demonstrates the if statement

#include<stdio.h>
main()
{
int a, b;
do
{
printf(“\nEnter first number:”);
scanf(“%d”, &a);
printf(“\nEnter second number:”);
scanf(“%d”, &b);
if(a<b)
printf(“\n\nfirst number is less than second\n\n”);
if(b<a)
printf(“second number is less than first number\n\n”);
}
while(a<999);
}

This program demonstrates the if-block

#include<stdio.h>
main()
{
int a, b;
do {
printf(“\nEnter first number:”);
scanf(“%d”, &a);
printf(“\nEnter second number:”);
scanf(“%d”, &b);
if (a<b){
printf(“\n\nFirst number is less than second\n”);
printf(“Their difference is: %d\n”, b-a);
printf(“\n”);
}
printf(“\n”);
}while(a<999);
}

The program demonstrates the Use of the if else block

#include<stdio.h>
main()
{
int num1, num2;
printf(“\nEnter the first number”);
scanf(“%d”, &num1);
printf(“\nEnter the second number”);
scanf(“%d”, &num2);
if(num2==0)
printf(“\n\nCannot divideby zero\n\n”);
else
printf(“\n\nAnswer is %d\n\n”, num1/num2);
}

A simple program to show the use of continue statement

#include<stdio.h>
main()
{
int x;
for (x=0; x<=100; x++){
if (x%2) continue;
printf(“%d\n”, x);
}

This program jumps out of an infinite loop using the break statement

#include<stdio.h>
main()
{
int t;
for ( ; ; ){
scanf(“%d”, &t);
if (t==10) break;
}
printf(“End of an infinite loop….\n”);
}

This program demonstrates the switch statement

#include<stdio.h>
main()
{
int i;
printf(“Enter a number between 1 and 4”);
scanf(“%d”, &i);
switch(i)
{
case 1;
printf(“One”);
break;
case 2;
printf(“Two”);
break;
case 3;
printf(“three”);
break;
case 4;
printf(“Four”);
break;
default;
printf(“Unrecognized number”);
} /*End of switch*/
}