Control Structure

 if condition


            Normally program is executed in the sequential order, and when we want to change the normal execution of the program then we need to use control statements or we can say control statements are used to change the normal execution of the program.

            Control statements are mainly divided into three types:-
            1. Condition control statements
            2. Looping or iterative statements
            3. Branching statements

            1. Condition control statements:-
            These are used when we want to execute the statement or back of statement according to some condition.  

Control Stucture in c        
            

Conditional execution can be controlled by:-
            a. If
            b. If else
            c. If else if
            d. Nested if
            e. Switch case
            f. Conditional operator or ternary operator
            If statement
            It is the basic condition control statement. When we have single condition then we mostly use it.
The if statement allows a programmer to test the value of conditional expression and to select or reject the execution of the statement or block of statements depending on this value.
            Example of program of if statement:-
            #include< stdio.h >
            #include< conio.h >
            void main()
            {
                 int a,b;
                 clrscr();
                 printf("enter the value of a");
                  scanf("%d",&a);
                 printf("enter the value of b");
                 scanf("%d",&b);
                   if(a>b)
                 {
                      printf("a is big");
                 }
                   if(a < b)
                  {
                        printf("b is big");
                  }
                  if(a==b)
                  {
                        printf("a is equal to b");
                  }
                  getch();
            }
            output:-
            enter the value of a 13
            enter the value of b 9
            a is big


            enter the value of a 9
            enter the value of b 13
            b is big


            enter the value of a 13
            enter the value of b 13
            a is equal to b



if Else If Ladder

     If we have more than two condition then we use if else is statement. In this we can create multiple blocks of statements and statements will execute according to the condition.

#include<stdio.h>

#include<conio.h>

void main()

{

           int a,b;

          clrscr();  

          printf(“enter the value of a”); 

          scanf(“%d”,&a);        

          printf(“enter the value of b”);        

         scanf(“%d”,&b);    

        if(a>b)      

     {             

          printf(“a is big”);     

      }

     else if(b>a)  

    {              

        printf(“b is big”);        

    }         

  else  {            

       printf(“a is equal  to b”);       

   }

   getch();

}

In this example if a is greater than b first block is executed and if b>a then second block will execute  else if none of the above conditions are true then else part will execute . If-Else-If is faster then multiple if block because in this if first block is execute then all the other blocks are ignored by compiler.










Nested If


We can write if-else statement  within the scope of another if or else block, it is known s nested if.

#include<stdio.h>

#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the value of a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
printf(“enter the value ofc”);
scanf(“  %d”,&c);

if(a>b)
{             
if(a>c)
{
printf(“a is big”);
}
}
if(b>a)
{
if(b>c)
{
printf(“b is big”);
}
}
If(c>a)
{
if(c>b)
{
                                printf(“c is big”);
}
}
getch();

}




Another Example of nested If




#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the value of a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
printf(“enter the value ofc”);
scanf(“  %d”,&c);

if(a>b)
{             
if(a>c)
{
printf(“a is big”);
}
else
{
                printf(“c is big”);
}
}
else
{
if(b>c)
{
printf(“b is big”);
}
else
{
                printf(“c is big”);
}

}
getch();
}
      




Switch statement

When we have multiple if statement then we can use switch case statement. The variable used in switch in known as switch variable. The variable in the switch statement can be of int or char type only. The switch statement can also contain the default statement that will executed if none of the case is executed, default is optional

#include<stdio.h>
#include<conio.h>
void main()
{
                char c;
                clrscr();
printf(“enter the value of c”);
scanf(“%c”,&c);
if(c==’r’)
{
printf(“colour is red”);
}
else if(c==’b’)
{
printf(“colour is blue”);
}
else if(c==’g’)
{
printf(“colour is green”);
}
else
{
printf(“colour is white”);
}
getch();
}

Program using swich case statement :-
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf(“enter the value of c”);
scanf(“%c”,&c);
switch(c)
{
case ‘r’:
printf(“colour is red”);
break;
case ‘b’:
printf(“colour is blue”);
break;
case ‘g’:
printf(“colour is green”);
break;
default:
printf(“colour is red”);


}
getch();
}


Another switch program  :-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char ch;
clrscr();
printf(“enter two number”):
scanf(“%d%d”,&a,&b);
fflush(stdin);
scanf(“%c”,&ch);
switch(ch)
{
case ‘a’:
c=a+b;
printf(“\naddition=%d”,c);
break;
case ‘s’:
c=a-b;
printf(“\nsubtraction=%d”,c);
break;
case ‘d’:
c=a/b;
printf(“\ndivsionion=%d”,c);
break;
default:
printf(“\nWrong key pressed”);




}
getch();


}

NESTED SWITCH :


#include<stdio.h>
#include<conio.h>
void main()
{
char c;
int i;
clrscr();
printf("enter any char");
fflush(stdin);
scanf("%c",&c);
switch(c)
{
case 'a':
printf("enter any num");
scanf("%d",&i);
switch(i)
{
case 1:
printf("one");
break;
case 2:
printf("two");
break;
default:
printf("wrong");
break;
}
break;
case 'b':
printf("enter any num");
scanf("%d",&i);
switch(i)
{
case 3:
printf("three");
break;
case 4:
printf("four");
break;
default:
printf("wrong");
break;
}
break;
default:
printf("wrong char enter");
break;
}
getch();
}



OUTPUT  :-  enter any char a
enter any num 1
  one


enter any char b
enter any num 6
wrong


enter any char d
wrong char enter







# C Example Simple Program print Hello World  without using semicolon(;) in c/c++ language 

        using if:-

        #include<stdio.h>
        #include<conio.h>
        void main()
        {
                if(printf("hello world"))
                {}
                
        }
        
        using switch:-

        #include<stdio.h>
        #include<conio.h>
        void main()
        {
                switch(printf("hello world"))
                {}
                
        }
using while:-

        #include<stdio.h>
        #include<conio.h>
        void main()
        {
                while(!printf("hello world"))
                {}
                
        }

C program run without main() function that time we will use #define start main() directive in our program.


        #include<stdio.h>
        #include<conio.h>
        #define start main
          start()
        {
                printf("welcome in c language");
                getch();
        }

3 comments: