Control Statements

Control Statements

-Decision Making-Branching
    -If Statement
    -Switch Statement
-Looping
    -For Loop
    -While Loop
    -Do ... While Loop
-Break and Continue Statements

Decision Making - Branching
 -If Statement (Simple If Statement)
 -If else Statement
 -Nested If Statement
 -Ladder If else Statement

 If Statement (Simple If Statement)
 If statement is a basic control flow structure of C programming language. 
 If statement is used when a unit of code need to be executed by a condition true or false. 
 If the condition is true, the code in if block will execute otherwise it does nothing. 

 The if statement syntax is simple as follows:
Syntax:
if(condition)
{
   /* unit of code to be executed */
}

Example:
if(a= =b)
{
        printf("A is equal to B");
}



If Else Statement
If we want to use several conditions we can use if-else-if statement. Here are common syntax of if-else-if statement:

Syntax:
if (condition) 
{       
          Program statement 1; 
}else 
{
        Program statement 2; 
}

Example:
if (x > y)
{
          printf("x is greater than y");
}
else 
{
          printf("x is less than y");
}

The if else is actually just on extension of the general format of if statement. 
If the result of the condition is true, then program statement 1 is executed, 
otherwise program statement 2 will be executed. 
In any case either program statement 1 is executed or program statement 2 is 
executed but not both

Nested If Statement
The if statement may itself contain another if statement is known as nested if 
statement. 
Syntax: 
if (condition)
{
        if(condition)
       {
               statement 1;
       }
       else
       {
               statement 2;
       }
}else
{
        statement 3; 
}



The if statement may be nested as deeply as you need to nest it.
One block of code will only be executed if two conditions are true. 
Condition 1 is tested first and than condition 2 is tested. 
The second if condition is nested in the first. 
The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement.

The else if Ladder
When a series of many conditions have to be checked we may use the ladder else if statement which takes the following general form.

Syntax:           
          if (condition1) 
      Statement – 1; 
}
else if (condition2)
{
        Statement2; 
}else if (condition3)
{
        Statement3; 
}
else if (condition) 
{
        Statement n; 
}else 
{
        Default statement; 
}


This construct is known as if else construct or ladder. 
The conditions are evaluated from the top of the ladder to downwards. 
As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement x (skipping the rest of the ladder. When all the condition becomes false, the final else 
containing the default statement will be executed. 


C Switch Statement
The switch statement allows you to select from multiple choices based on a set of fixed 
values for a given expression. 

Here is the common switch statement syntax:
Syntax: 
switch(expression)
{
        case value1: 
                              /* execute unit of code 1 */
                              break;
        case value2: 
                             /* execute unit of code 2 */
                             break;
                   ...
        default: 
                     /* execute default action */
                     break;
}



Switch case statement is option of if else statement.
The value of variable is passed to switch(variable) to compare with each case, if variable value matches the case variable then execution of that case occurs.
If all the cases are wrong then default case is true like the else part of if else statement.The default case is optional.
The break; statement at the end of each case is used to break out the case.If break is not used then all the case will be executed.

In switch statement case may only be integer or character

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
        int ch;
        clrscr();
        printf("Insert from 1 to 3:");
        scanf("%d",&ch);
        switch(ch)
        {
                 case 1:
                           printf("You have entered 1");
                           break;
                 case 2:
                           printf("You have entered 2");
                           break;
                  case 3:
                           printf("you have entered 3");
                           break;
                  default:
                           printf("Number is out of 1 to 3");
                           break;

        }
        getch();
}



Looping
-For Loop (Entry Controlled)
-While Loop (Entry Controlled)
-Do While Loop (Exit Controlled)

Looping
Loops are useful to execute certain block of  code multiple times.
Loop will execute until the particular condition occurs.
It is useful when we want to perform repetitive task in program.

Consider if we want to print the numbers 1 to 100 then loop is useful
Instead of printing numbers 100 times we can execute the loop for it

For Loop
Syntax:
for(Initialize; condition;increment)
{
      //block of code
}



There are three parts which is separated by semi-colons in control block of the for loop.
Initialization indicates the starting value of loop
Condition is to manage the loop, loop will execute until the condition is false
Increment is used to increment the loop counter.
We can also use for loop in nested structure.

Here is an example of using for loop statement to print an integer five times


Example:

void main()
{
    // using for loop statement
     int i = 0;
     for(i = 0; i<5;i++)
     {
         printf("%d\n",i);
     }
}
And the output is
1
2
3
4
5

In above example 
i=0 is initialize with value 0
condition is i<max, means loop will run till the value of i became 5
and i++ shows the increment of 1, means i=i+1



While Loop

While loop executes till the expression is true. When expression is false, execution jump out from the while block.
In while loop test expression is checked first then code is executed.
Loop continues till the expression becomes false.

Syntax:

while (expression) 
{
  // statements
}

Example:
#include <stdio.h>
#include<conio.h> 
void main()
{
    // using while loop statement
     int i = 1;         //Initial value to start the loop

    while(i<=5)           //test condition
     {
         printf("%d\n",i);
     i++;                         //increment value
    }
   getch();
}
output :
1
2
3
4
5



Do While:
Do While loop is same as while loop but in do while loop 
Code inside the body of do is executed and Condition is tested at the end of the block.
If condition is true code inside the do body execute again and this process continues till the condition becomes false.
Semicolon (;) is used at the end of while(); in do while loop

Syntax:

do 
{
  // statements

}while (expression);

Example:

#include <stdio.h>
#include<conio.h> 
void main()
{
    // using while loop statement
     int i = 1;         //Initial value to start the loop

    do           
     {
         printf("%d\n",i);
     i++;                         //increment value
    }while(i<=5);  //test condition
   getch();
}
And the output is

1
2
3
4
5


Break and Continue

Break
Break statement is used to terminate the loop(for,while,do). We can use break with if and switch also.
Syntax:
break;
Example:
         
for(i=1;i<10;i++)
{
        printf("%d\n",i);
        if(i==5)
             break;
}
output:
1
2
3
4
5

above loop will break when the value of i become 5



Continue
Continue statement is used to skip values inside the loop.
Syntax:
continue:
Example:
          for(i=1;i<5;i++)
{
     
        if(i==3)
             continue;
        printf("%d\n",i);
}
output:
1
2
4
5



Nested Loops in C

C programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.

Syntax:

The syntax for a nested for loop statement in C is as follows:
for ( init; condition; increment )
{
   for ( init; condition; increment )
   {
      statement(s);
   }
   statement(s);
}
The syntax for a nested while loop statement in C programming language is as follows:
while(condition)
{
   while(condition)
   {
      statement(s);
   }
   statement(s);
}
The syntax for a nested do...while loop statement in C programming language is as follows:
do
{
   statement(s);
   do
   {
      statement(s);
   }while( condition );
 
}while( condition );
A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa.

Example:
The following program uses a nested for loop to find the prime numbers from 2 to 100:
#include <stdio.h>
 
void main ()
{
   /* local variable definition */
   int i, j;
   
   for(i=2; i<100; i++)
   {
      for(j=2; j <= (i/j); j++)
         {
                  if(!(i%j)) 
                  {
                           break; // if factor found, not prime
                  }
                  if(j > (i/j)) 
                  {
                           printf("%d is prime\n", i);
                  }
          }
   }
}
When the above code is compiled and executed, it produces the following result:

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime

0 comments:

Post a Comment