Article

Chapter-6(Decision-Making Statements)


1.Conditional Statements:-

a)If Statements:-

The if statement is used to execute a block of statements if a specified condition is true.

Syntax:-

if(condition)

{

Statement 1;

}

Statement 2;


If the condition is true, then statement 1 is executed; otherwise, statement 2 is executed.

b) If-else Statements:-

It allows you to execute different blocks of code based on whether a specified condition is true or false.

Syntax:-

if(condition)

{

Statement 1;

}

else

{

Statement 2;

}

If the condition is true, then the if statement (Statement 1) is executed; otherwise, the else statement (Statement 2) is executed.

c) Multiple-If Statement:

syntax:-

if(condition)

{

Statement1;

}

else if(condition)

{

Statement2;

}

else

{

Statement3;

}

multipleif statements to check multiple conditions sequentially. Eachif statement is evaluated independently, and the corresponding block of code is executed if the condition is true.

d) Nested If Statement:

A nested if statement is an if statement that appears within the body of another if or else statement. This allows for more complex conditions and decision-making structures in a program. Here is the syntax and an example of a nested if statement in C

if (condition1) 

{


//--Statement 1 is executed if condition1 is true

    Statement1;

    if (condition2) 

   {

        //-- Statement2 is executed if both condition1 and condition2 are true

      Statement2;

    }

    

    //-- More code you can execute outside of if block

   }

 else 

{

    //-- Statement3 is executed if condition1 is false

     Statement3;

    

    if (condition3) 

  {

//--Statement 4 is executed if condition3 is true

         Statement4;

    } 

else

 {

        //-- Statement5 to be executed if both condition1 and Condition 3 are false.

        Statement5;

    }

    

    // More code you can execute within the outer else block

}

// Code  you can execute outside the if-else blocks


e) Conditional/Teenary Expression:-

syntax:-

Expression1?Expression2:Expression3;

  • a) Expression1: A boolean expression that is evaluated
  • b) Expression2: The value or expression2 to be returned if Expression1 is true.
  • c) Expression3: The value or expression3 to be returned if Expression1 is false.

In this example, the ternary expression (number >= 0) ? "positive" : "negative" checks if the number is greater than or equal to 0. If true, the expression evaluates to "positive"; otherwise, it evaluates to "negative".


2. Unconditional Statements:


a)Break Statement:-


It is used to terminate the loop.

The break statement is used to terminate the execution of the innermost loop (for, while, or do-while).


syntax:-


for (initialization; condition; alteration) {

    // Statement to be executed in each iteration

    if (condition_to_out of loop) {

        break; // Exit the loop prematurely

    }

}


b)Continue Statement:-

It is used to skip the number of terms.

In C programming, the continue statement is used to skip the rest of the code inside a loop for the current iteration and move on to the next iteration of the loop. It is typically used within loops (for, while, or do-while) to jump to the next iteration prematurely.


Syntax:-


for (initialization; condition; alteration)

{

    // block of code before the continue statement


    if (condition_to_skip)

{

        continue; // Skip the rest of the block of code in the current alteration and move to the next alteration

    }


    // code after the continue statement

}


when (condition_to_skip) is true  then it skip current alteration and move to next alteration.


c) goto Statement:-

It is used to jump the control from one statement to another.


Example:-

int i;

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

{

if(i==5)

{

goto label1;

}

printf("%d\n",i);

}

printf("out of loop");

label1:

printf("Hello");


Result:-

2

3

4

Hello


Conclusion:-

label: It is a user-defined identifier followed by a colon (:). The label is a target for thegoto statement, indicating the position in the code where control should be transferred.

Here we use label1 in the example instead of label.

3. Repetitive statement:-

Repetitive statements, also known as loops, are used to execute a block of code repeatedly as long as a certain condition is true. There are three main types of loops in the C language: for, while, and do-while. Each type serves different purposes but shares the common objective of repetitive execution.


a). For Loop:

The for loop is typically used when the number of iterations is known beforehand.


Steps to work for loop:

1. Intialization 

2.Condition

3. Body of loop

4. Alteration 


Syntax:-

for (initialization; condition; Alteration)

{

Body of the loop;

}


Example:-

int i;

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

{

printf("%d",i);

}


Result:-

1

2

3

4

5


b)While Loop:-

The while loop is used when the number of iterations is not known beforehand, and the loop continues as long as a specified condition is true.


syntax:-

While(condition)

{

Body of loop;

}


Example:-

int i=1;

while(i<=5)

{

printf("%d\n",i);

i++;

}


Result:-

1

2

3

4

5


c)do-while loop:-

The do-while loop is similar to the while loop, but it ensures that the loop body is executed at least once before the condition is checked.


Syntax:-

do

{

Body of loop;

}

while(condition);



Example:-

int i=1;

do

{

printf("%d\n",i);

i++;

}

while(i<=5);



Result:-

1

2

3

4

5