C- Control Structures

A control structure defines the execution order of the given program statements.
There are three types of control structures.

  • Sequence (Linear)
  • Selection
  • Repetition (Iteration)

Selection Structure

In C language we can use the If and Switch statements to represent the selection structure.

If statement also come in two ways.

1. if(condition){
     statement;
   }
   else{
    statement;
   }

2. if(condition){
     statement;
   }
   else if{
    statement;
   }
   else{
    statement;
   }

This second way is called as nested if statement.
and also there can be only a if condition in a program.

ex: 
1.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int no1,no2;
    printf("enter two numbers");
    scanf("%d %d",&no1,&no2);
    if(no1>no2){
        printf("highest is %d \n",no1);
    }
    else{
        printf("highest is %d \n",no2);
    }
    return 0;
}

2.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int no;
    printf("enter a numbers");
    scanf("%d ",&no);
    if(no>0){
        printf("number is positive \n");
    }
    else if(no<0){
        printf("number is negative \n");
    }
    else
        printf("number is zero");
    return 0;
}

In the second example above i haven't used curly brackets in the else statement. Curly brackets are not essential if any condition clause having only one statement within it. Curly brackets are used to identify the statements executing under a condition.

In the switch statement ,the condition can have integer or character values only.

ex:
1.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char v;
    printf("enter a simple letter");
    scanf("%c ",&v);
    switch(v)
    {
    case'a':
        printf("vowel");
        break;
    case'e':
        printf("vowel");
        break;
    case'i':
        printf("vowel");
        break;
    case'o':
        printf("vowel");
        break;
    case'u':
        printf("vowel");
        break;
    default:
        printf("%c is not a vowel",v);
    }
    return 0;
}

2.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m;
    printf("enter the month number");
    scanf("%d ",&m);
    switch(m)
    {
    case 1:
        printf("january");
        break;
    case 2:
        printf("february");
        break;
    case 3:
        printf("march");
        break;
    case 4:
        printf("april");
        break;
    case 5:
        printf("may");
        break;
    default:
        printf("invalid month number");
    }
    return 0;
}

In switch case "break" key word should be used after each each case statement.Otherwise the program will keep running until it finds a break statement.
Also in case statement , if you are entering a character value u should give that value inside single quotation marks. If the value is a integer no need of using quotations.

Repetition Structure

In iteration structure the given set of statements will repeatedly execute until the given condition remains true.
The iteration structure can be implemented by using following three types of loops.
  • While
  • Do while
  • For
1.While loop

While(condition)
{
  statements;
}

ex:
int x=10;
while(x<=100)
{
printf("%d",x);
x=x+10;
}

2. Do while loop

This also works similar as while loop ,but in here the statements inside the loop is executing one time before checking the condition. Because the condition is at the end of the program. Therefore this type is not used much in programming.

do
{
statement;
}while(condition);

ex:
int x=1;
do{
printf("%d",x);
x++;
}while(x<=100);

3. For loop

for(expression1;expression2;expression3){
action;
}

ex:
for(x=1;x<=10;x++){
printf("%d",x);
}






Comments

Popular posts from this blog

Programming with Java

C - Functions