Branching and Looping Statements in C | Decision Making | Control Statements

Branching and Looping in C Language

We can control the flow of a program using branching and looping statements in any language. Branching statements give us code which is optionally executable, depending on the outcome of certain tests or you can say certain cases which we can define. Looping statements are used to repetition of a section of code a number of times or until a condition has been fulfilled.

Branching and Looping in C Language | Tech-Mirror
Branching and Looping in C Language | Tech-Mirror

Branching Statements

1) if Statement

It executes its block only if the condition is true

  • Syntax

TheĀ if statement tests an expression contained in the parentheses() that follow the if keyword. If the expression evaluates to Boolean true, then the statement will be executed. As far as C is concerned, zero is false and anything non-zero is true.

2) if else Statement

if…else executes if block if condition is true; otherwise it will execute the else block

  • Syntax

The else statement optional… if “nothing” is to be done when the condition is false, then the else statement should not be used! For example, we only congratulate our good students, we do not chastise our bad students so in this case we do not write in else part.

3) else if Statement

It checks the condition of both if and else if block and executes the respective block; otherwise it will execute the else block.

  • Syntax

For else if statement,we often break our problems into several sub paths, that mean it provides an “elseif” control statement. For example, if we have 5 dollars for go to the dollar theater, “else if” we have 10 dollars for go to the regular theater, “else if” you have 100 dollars, go to a Broadway play, else if you have 1000000 dollars, buy a theater etc..

4) Switch Statement

The switch block consists of several cases which includes a default case too. Each case must has a break statement to jump out of switch block on its execution. The cases are matched and then executed provided the condition for cases in switch statement.

  • Syntax


Looping Statements

1) while Statement

It will execute its block only if the condition is true and continues to loop. It executes the block until the condition fails.

  • Syntax

2) do-while Statement

This will executes its statements and checks the condition. It continues looping if condition is true; else it aborts.

  • Syntax

3) for Statement

It executes its block until the condition fails. In it there are initialization, condition and iteration includes in syntax.

  • Syntax


Jumping Statements

1) goto Statement

It defines a block with a label; on goto execution the block will executed respectively.

  • Syntax

2) continue Statement :
The continue statement is used to execute the current block or region sequentially.

3) break Statement :
The break Statement is used to jump out from the current block after its execution.

You do code in any language there is similar syntax for branching and looping statements, with the help of branching and looping you can do a lot.

For More C Variables

 

Leave a Reply

Your email address will not be published. Required fields are marked *