Monday, December 18, 2017

What is an flow of control in c programing language

Flow of control


It allows you to repeat compound statements or statements for a certain duration. 
                               Its body is executed at least zero times and continues until the optional condition is found to be false. 
                      If you want to change and initialize values during your execution of your for statement, you may use optional expressions.

       Syntax of the flow control:-

           for ( init – expressionopt ; cond –          expressionopt ; loop – expressionopt ) statement

Can achieved i case the result:-

       If the cond – expression is found to be true or non-zero, then the statement gets executed. If there is a loop – expression, it gets evaluated. This loop – expression gets evaluated after every iteration.
                 Keep in mind that this type does not have any restrictions. The side effects generally take place in a certain order. Then, the process will start once more alone with evaluation of the cond – expression.
                     In case the cond – expression gets skipped, then this cond – expression will be regarded as true. The execution will then continue.
             You have to take note that a for statement that does not have any cond – expression argument only terminates when a return or break statement within a statement body gets executed. The same thing happens when goto gets executed. In case the cond – expression is found to be false or zero, the execution of for statement is terminated and the control is passed on towards the following program statement. You have to keep in mind that the for statement can also be terminated when a return, break, or goto statement in your statement body gets executed.
             In the for loop, the loop – expression is evaluated when there is a continue statement involved. If the break statement inside the for loop is executed, the loop – expression does not get executed or evaluated. The statement for ( ; ; ) is actually a traditional way to come up with the infinite loop.
   
 

Example of flow control chart





      obtain the following output: 
       
The number of spaces is 4 
The number of spaces is 2


1). The while statement 
2). The do statement 
3). Decision- making statement 
4). The If statement 

The while statement-

The While Statement It allows you to repeat the statement until the specified expression turns out to be false. 
          This statement has the following syntax: 
The iteration statement: 
while ( expression ) statement 
       Keep in mind that your expression has to have a pointer or arithmetic type. When everything is ready, your program will be executed through the following steps: 
            The expression gets evaluated first. If it is found to be false, then the body of the white statement does not execute.
            Instead, the control is passed from the while statement towards the next statement in your program. 
                 In case the expression is found to be true, which is represented by a non-zero, the statement body gets executed. The same process that was done before occurs once more.
For example:-

while ( i >= 0 ) 

{ ​

string1 [ i ] = string2 [ i ] ;

 ​i - - ; 

}

The do statement -

The Do Statement It keeps on executing until the condition is found to be false. This statement has the following syntax: 
do statement while ( expression ) ; 
         Keep in mind that this statement is repeatedly executed for as long as the value of the expression stays non-zero. 
             Such expression gets evaluated after every iteration. This is necessary in order for the loop to execute the statement one or more times. 
     With regard to the parentheses found around the expression, they are mandatory. If you do not include them in your statements, you will get an error when you try to run your program. 
           In addition, you have to take note that do is the sole control structure in the C programming language that explicitly ends with a semicolon ( ; ). 
    The other control structures in the C programming language tend to end with a statement. This means that they implicitly feature a closing brace or semicolon. In order for you to understand this concept better, you have to take a look at the following example. 
           In this example, the scalar product of two vectors are calculated with the use of the do statement:

w = 0 ; i = 0 ; do 

{ ​

          w += a [ i ] * b [ i ] ; ​

                i++ ; 


while ( i < n ) ;


Decision- making statement-

When writing your programs in the C language, always remember that you need to specify at least one condition for testing or evaluation of the structures. 
          You also have to include at least one statement for execution in case your condition is found to be true. 
         You may include optional statements in case the condition is found to be false.

 Decision making statement are understanding by flow diagram

    


The If statement-

It consists of a Boolean expression, and then followed by at least one more statement. 
         It has the following syntax: 
    
 if ( Boolean expression ) 
// This statement will be executed if the Boolean expression is found to be true. 
       You have to take note that the expression found within the parentheses can be anything. 
              For instance, if its value is zero, the expression is considered to be false and the statement is ignored. 
           If its value is a non-zero, the expression is considered to be true and the statement is executed.

The If statement are flow chart diagram shown as :-





      

0 comments:

Please comment and follow this site blog inbox