Control Statements in C++

Control Statements in C++
             Program that we tried so far are executed in an orderly manner i.e. the statements are executed one after another without getting repeated or ignored. Certain tasks require execution of some statements ignoring the rest. These can be accomplish by using control statements.


Conditional control statements

These statements are executed when one or more conditions is/are satisfied. C++ supports many such statements. These statements are listed below with a brief description.

if:- The statement following the if statement is executed when the condition given is true.
                                if(condition)
                                                s1;
                                s2;
                When the condition is true statement s1 and then s2 is executed. If the condition is false only s2 is executed. If we want to execute more than 1 statements when condition is true then we should write all those statements within braces {} after if.
        if(condition)
                                {
                                                s1;
                                                s2;
                                }
        s3;
when the condition is true statement s1, s2 and then s3 is executed. If the condition is false only s3 is executed.



if-else:- Also known as either or. This statement is used to select one statement and ignore the other statements.
                                if(condition)
                                                s1;
                                else
            s2;
                when the condition is true statement s1 is executed. If the condition is false s2 is executed. Thus one of the statements, either s1 or s2 is always executed.



if-else if-else:- It is a branching statement which can choose and execute on of the statements available depending upon the condition.
                                if(condition1)
                                                s1;
                                else if(condition2)
            s2;
                                else
                                                s3;
                when the condition1 is true statement s1 is executed and rest are ignored. When condition1 is false condition2 is verified. If condition2 is true statement s2 is executed and other statements are ignored and so on. Thus only one statement is executed from the top, depending upon the condition. The statement following else is executed when all the conditions are false. However else clause is optional.


while:- It is a repeated structure statement which repeats a statement given as long as the condition is true.
                                while(condition)
                                                statement1;
                statement1 is executed till the condition is true.



do while:- Like while statement it repeats a statement given as long as the condition is satisfied unlike in while statement the condition is checked at the end of the structure.
                                do
                                {
                                                statement1;
                                }
                                while(condition);
statement1 is executed till the condition is true.



for:- It is a repeated structure which can repeat a statement as long as the given condition is satisfied.
                                for(statement1; condition; statement2)
                                                statement3;
                where statement1 initialises control variable, condition is used to check whether statement written after loop can be repeated or not , statement2 is used to modify the control variable. Statement3 is a simple statement (having only one statement) or compound statement (a set of statements written within braces {} ) which can be repeated as long as condition is true or satisfied.

Comments

Popular Posts