C Flow Control Statement Tutorial Notes Study Material with Examples

C Flow Control Statement Tutorial Notes Study Material with Examples

C Flow Control Statements

Control statement is one of the instructions, statements or group of statement in a programming language which determines the sequence of execution of other instructions or statements.

C provides two styles of flow controls

  1. Branching (deciding what action to take)
  2. Looping (deciding how many times to take a certain action))

 Decision Making and Branching

C has three major decision making instructions as given below If Statement

It takes an expression in parenthesis and a statement or block statements. Expressions will be assumed to be true, if evaluated value are non-zero.


  • Simple if statement Syntax

if (expression)

{

Statements ;

}

  • If else statement

Syntax

if (test

{

block of statements;

}

 else

{

block of statements;

}

  • If else-if ladder statement

Syntax

if (expression)

{

statements;

}

else i f ( expression )

{

 statements;

}

else

   {

statements;

}


NoteC programming language assumes any non zero and non-null values as  true and if it is either zero or null then it is assumed as false value.

Flow Diagram of if-else Statement:

C Flow Control Statement Tutorial Notes Study Material with Examples
C Flow Control Statement Tutorial Notes Study Material with Examples

The switch Statement

The switch statement test the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed.


switch (expression)

{

   case value 1:

    statement 1;

   break;

   case value 2:

   statement 2;

   break;

   .

   .

   .

   default:

   statement;

}


 Note: The break keyword transfers the control out of the switch statement.


The Conditional Operators (?, :)

The ?, : operators are just like an if-else statement except that because it is an operator we can use it within expressions.

? : are a ternary operators in that it takes three values. They are the only ternary

operator in C language.

 

     
if (x < 0) flag = (x < 0) ? 0:1;  
flag = 0;    
Else Condition Value which           will Value          which           will
flag =1; or be        returned,        if be        returned,         if
  expression condition            holds

true.

condition              holds

false.

Loop Control Structure

Loops provide a way to repeat commands and control. This involves repeating some portion of the program either a specified numbers of times until a particular condition is satisfied.

There are three types of loop

while Loop

initialize loop counter;

while (test loop counter using a condition/expression)

{

do this;

and this;

decrement/increment loop counter;

}

for Loop

for    (initialize   counter; test    counter;

increment/decrement counter)

{

          do this;

          and this;

           and this:

}

do while Loop

initialize loop counter;

do

{

this;

and this;

}

while (this condition is true);

The break Statement

The break statement is used to jump out of a loop instantly, without waiting to get back to the conditional test. A break is usually associated with an ‘if’. When break is encountered inside any loop, control automatically passes to the first statement after the loop.


# include  <stdio.h>

void main ( )

{

int num, i;

printf (“Enter a number”);

scanf (“% d”, & num);

i = 2;

while (i <= num— 1)

{

if (num % i = =0)

{

printf (“Not a prime number”);

break;

}

i+ +;

}

if (i = =num)

printf(“Prime number”);

}


Note  :When num% i turns out to be zero(i.e. , num is exactly divisible by i)the message “Not a prime number” is printed and the control breaks out of the while loop.

The continue Statement

The ‘continue’ statement is used to take the control to the beginning of the loop, by passing the statement inside the loop, which have not yet been executed.


       # include <stdio.h>

void main ( )

{

int i, j;

for (i= 1; i < = 2; i + + )

{

for (j = 1; j < = 2; j + + )

{

 if (i= =j)

continue;

printf(“\n %d%d”. i, j);

}

}

}

Output 1 2

21


When the value of i equals to j, the ‘continue’ statement takes the control to the for loop (inner) by passing the rest of the statements pending for executions in the ‘for’ loop.

 goto Statement

C supports an unconditional control statement, goto, to transfer the control from one point to another in a C program. The goto is a branching statement and requires a label.


# include <stdio. h>

 void main ()

{

int i, j, k;

for (i= 1; i < = 3; i+ + )

    {

       for (j= 1: j < = 3; j+ + )

           {

for (k=1;k <= 3; k+ + )

{

if (i = = 2: &&j= = 2 &&, k= = 2)

goto out;

else

printf (“%d%d%d \ n”, i, j, k);

}

     }

              }

              out:

printf ( “out of the loop at last ! ” );


Note  :The usage of the goto keyword should be avoided as, it usually violets the normal flow of execution.


Importance of Turing Machine Tutorial Notes Study Material with Examples

Follow Us on Social Platforms to get Updated : twiter,  facebookGoogle Plus

Learn Turing Machine in Handbook Series:  Click here 


 

Leave a Reply

Your email address will not be published. Required fields are marked *