control flow statements(theory)

 Control Flow Statements are those statements used to control the flow of execution of a program to achieve desired output.

There are four types of control statements

1. If statement

2. If - Else statement

3.Else -If statement

4. switch statement

Note: these if statements are good to go with some basic programs but in huge programs they are not acceptable because in if statement control work like car if two more turns occur it will take the one and never go through others even if they are right.

if there are 10 conditions, and first condition is true then all other conditioins remain untouched, so we need loops for that 

If statement : This statement is used to check true condition, it means if the condition is true only then the given set of instruction will be executed.

it have drawback that it will execute instruction only in case if condition is true.

Syntax:-

            if(condition)

                {

                    Statement

                }

If-Else statement: This statement is used to check both true and false conditions means that it will execute set of instruction either the condition is true or false.

it have drawback the we can use only one condition in this.

Syntax:

            if(condition)

                {

                    Statement

                }

               else

                {

                    Statement

                }

Else-if statement: This statement is used to check multiple conditions in a single program. means that in single else-if condition we can use multiple conditions and generate desired output.

but it have drawback that it can not use symbols in conditions.

Syntax:

            if(condition-1)

                {

                    Statement

                }

                else if(condition-n)

                {

                    Statement

                }

               else

                {

                    Statement

                }

Switch statement: (Same like else-if statemet with one additional property that is using symbols as case values) This statement is used to check cases instead of conditions, switch statement matches the pattern of the variable values and according to the matter matched in the case output is displayed

Syntax:

            switch(variable)

                {

                    case 1:

                       statement;

                       break;

                    case 2:

                       statement;

                       break;

                    case n;

                       statement:

                     break;

                    default

                       statement;

                 }

No comments:

Post a Comment