echo '' ;
C Programming Language

C – Decision Making Branching Looping

This article will discuss “Decision Making Branching Looping in C program”.

Decision Making: This involves using conditions to make choices in code. For example, using an “if” statement to check if a certain condition is true or false, and then executing different blocks of code accordingly.

Branching: Branching is an extension of decision making. It involves creating multiple paths or branches in your code based on different conditions. This often includes using “else if” statements.

Looping: Looping allows you to repeat a certain block of code multiple times. There are different types of loops, but two common ones are “for” and “while” loops.

Control Statements (Two types)

C program is executing sequentially line by line from the first statement to the last statement. But we can change this sequential execution in c, by using control statements. Types: Unconditional statements (goto statement) and Conditional statements (Two types)

Unconditional statements (goto statement)

  • It will change the execution order without any checking process
  • Syntax : goto label_name_position; label_name_position : ArunEworld;
  • Rules
    1. the label should be a valid identifier.
    2. the label should end with a colon :
    3. Does not require to declare of a label.

Conditional statements (Two types)

  • It will change the execution order with the checking process.
  • Types: Decision-making statements(five types) and Looping statements (Three types)
  • Note: return statement or return keyword cannot be used with conditional operators(ternary).

Decision-Making statements (five types)

  • It will change the execution order with the checking process.
  • types
    1. Simple if Statements
    2. if-else statements
    3. else …if ladder statements
    4. nested if statements
    5. switch statements

Simple if Statements

  • It will change the execution order from one place to another place (or) skip the execution of a group of statements (or) Execute the group of statements by checking one condition.
  • Syntax : if(Boolean_expression) goto label;(or)if(Boolean_expression) statement; (or)
    if(Boolean_expression)
    {
        statement_1;
        statement_2; 
    }
  • Note: statement(s) will execute if the Boolean expression is true.
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open{ and } close braces, if use group of statements
    • examples
      • if (a<b) big =1;
      • if (a=1) goto ArunEworld;
      • if (a<b)
        {
            s=a+b; 
            d =a-b;
            m = a+b;
        ]
  • Example Programs
    • Evaluate_mark_of_students_using_if_statement.c
    • Evaluvate_sale_tax_using_if_statement.c

if- else statements

  • Execute the group of statement if test condition is true, else test condition is false it will execute the another group of statements
  • Syntax : if(Boolean_expression) statement_1; else statement_2; (or)
    if(Boolean_expression)
    {
        statement_1;
        statement_2;
    }
    else
    {
        statement_1;
        statement_2;
    }
    
    
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples
      • if (a<b) big =b; else big =a;
      • if (a=1) goto ArunEworld; else goto Arun;
      • if (a<b) { s=a+b; d =a-b; } else m = a+b;
  • Example Programs
    • check_given_marks_fail_or_pass_using_if_else_statement.c
    • Biggest_number_using_if_statement.c

else …if ladder statements

  • else..if is can use to checking more than one conditions, Execute the group of statement if test condition_1 is true, else..if test condition_2 is true it will execute the another group of statements, else above two test condition is false execute another group of statements
  • Syntax :
    if(Test_condition_1) statement_1; 
    elseif(Test_Condition_2) statement_2;
    else statement_3;

(or)

  • if(Test_condition_1) {    statement_1;  statement_2; } 
    elseif(Test_condition_2) { statement_1;  statement_2; } 
    else {  statement_1;  statement_2; }
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open{ and } close braces, if use group of statements
    • examples
      if (a<b) big =b; elseif(b>a) big = a;  else printf("a and b are same vale");
      • if (a>b) goto ArunEworld; elseif(b>a) goto Arun; else goto Eworld;
      • if (a<b) { s=a+b;d =a-b; } eslesif(b>a){ d =a-b; b= a-b;} else {m = a+b; a=a+b;}
  • Example Programs
    • Display_Day_using_if_elseif_statement.c

nested if statements

  • Syntax :
    if( boolean_expression)
    {
        /* Executes when the boolean expression 1 is true */
        if(boolean_expression 2)
        {
             /* Executes when the boolean expression 2 is true */
        }
    }
  • Examples : Biggest_of_three_numbers_using_nested-if.c

 

switch statements

group of if..else statements collection is called a switch statement.

Syntax :

switch(expression)
{
    case constant-expression  :
    statement(s);
    break; /* optional */
    
    case constant-expression  :
    statement(s);
    break; /* optional */

    /* you can have any number of case statements */

    default : /* Optional */

    statement(s);
}

Rule

  1. The expression must have an integral or enumerated type.
  2. The constant expression must be the same data type as the variable in the switch.

Try your self Example: Display_Day_using_switch_statement.c

nested switch

Syntax :

switch(ch1)
{
    case 'A':
    printf("This A is part of outer switch" );
    switch(ch2)
    {   
        case 'A':
        printf("This A is part of inner switch" );
        break;

        case 'B': /* case code */
        break;
    }
    case 'B': /* case code */
    break;
}


Looping control statements (Three types)

While Loop

  • Syntax: while(condition) //The condition may be any expression, and true is any nonzero
  • value. The loop iterates while the condition is true.
{
    statement(s); //statement(s) may be a single statement or a block of statements.
}

Do..while Loop

Syntax :

do
{
     statement(s);
}while(condition);

Rules: the condition is true while statement break the do loops

nested while loops

Syntax :

while(condition)
{
   while(condition)
   {
       statement(s);
   }
   statement(s);
}

for Loop

Syntax :

for( init; condition; increment )     //
{
      statement(s);
}

Rules:

  1. init – The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  2. condition – Next, the condition is evaluated. If it is true, the body of the loop is executed.If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop.
  3. increment – After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

nested for loops

Syntax :

for ( init; condition; increment )
{
    for ( init; condition; increment ) 
    {
         statement(s);
    }
    statement(s);
}

Infinite Loop

A loop running continuously for an indefinite number of times is called an infinite loop.

example:

int main()
{
    for(;;)
    {
         //code to be executed
         printf("This loop will run forever.\n"); 
    }
    return 0;
}

While Loop in Infinite

while(1)
{
    //code to be executed
}

Infinite Do-While Loop:

do
{
   //code to be executed
}while(1);

Infinite For Loop:

Syntax:

for(;;)
{
	printf("ArunEwrold\t");
}

Example code:

#include<stdio.h>
int main()
{
   int n;
   for(n = 7; n!=0; n--)
     printf("n = %d", n--);
   getchar();
   return 0;
}

Output: Above C program goes in an infinite loop because n is never zero when loop condition (n != 0) is checked


break

Break can appear only within the looping control and switch statement. The purpose of the break is to bring the control out of the used blocks.

Syntax : break;

if you use break statement in unwanted place then error will be generated.

#include <stdio.h>
int a =1;

int main()
{
if(a==1)
{
// printf() displays the string inside quotation
printf(“Hello, World!”);
break;
}
return 0;
}

/****************** Result & Error *********************/
//Error : #13 a break statement may only be used within a loop or switch


continue

  • Syntax : continue;

Goto Statement

Excersize

JCOKE = 3
JCOKE = JCOKE + 1
GO TO (5, 8, 9, 11, 15, 16 18, 20) JCOKE.

After the execution of the above statement, the control is transferred to statement number
A. 8
B. 11
C. 16
D. 20

  • Answer: Option B (Explanation: It is a computed GO TO statement. Since J COKE = 3 + 1 = 4, the control is transferred to statement 11).

Next

C - Basic
C - Operator
C - Decision making, Branching, Looping
C - Functions
C - Storage Class
C - Extern
C - Array
C - Pointer
C - Memory Allocation
C - Structure
C - Union
C - Structure and Union
C - Macros
C – Preprocessor and Macros
C - File Management
C - Coding Standard
C - Compilers
C - Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading