Control Statements in C Programming | C Programming

Control Statement

Control statements enable us to specify the flow of program control i.e., the order in which the instructions in a program must be executed. They make it possible to make decisions, perform tasks repeatedly, or jump from one section of code to another.

The different forms of if statements with their syntax and semantic diagram are: -

If statements: - 

The if statement evaluates the test expression inside the parenthesis. If the test expression is evaluated to true (non-zero), statements inside the body it is executed. If the test expression is evaluated to false (0), statements inside the body of if are skipped from execution.

The Syntax and Semantics diagram are:

if (test expression)
{
    (statements)
}

if statement

If else statement: 

The if-else statement executes some code if the test expression is true (nonzero) and some other code if the expression is false (0). If the tested expression is true, codes inside the body of the if statement is executed and, codes inside the body of the else are skipped. And when it is false codes inside the body of the else are executed and codes inside the body of the if statement is skipped.

The syntax and semantic diagram are: -

if (test expression)
{
    // codes inside the body of if
}
else
{
    // codes inside the body of else
}


if-else statement

Nested if.... else statement (if ...... else if ....... else statement): -

The if .... else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The nested if ...... else statements allow you to check for multiple test expressions and execute different codes for more than two conditions. The syntax is

if (condition 1)
    statement 1;
else if (condition 2)
    statement 2;
else if (condition 3)
    statement 3;
else if (condition n)
    statement n;
else
    default statement;
statement x;


Nested if statements: -

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 then condition 2 is tested. The second is if the condition is nested in the first. Second, if the condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement. The if statement may itself contain another if statement known as a nested if statement.
Syntax:

if (condition1)
    if (condition2)
        statement - 1;
    else
        statement - 2;
else
    statement - 3;

Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C Program To Check The String Is Valid Identifier Or Not | C Programming