Home > Programming In C > C-Flow Control Statements

C-Flow Control Statements

C-Flow Control Statements – C- Provides Two Styles Of Flow Control.1) Branching 2) Looping, Branching Is Deciding What Actions To Take And Looping Is Deciding How Many Times To Take A Certain Action.

Branching

Branching Is So-Called Because The Program Choose To Follow One Branch Or Another.

(i) If Statement 

This Is Th And An Statement e Most Simple Form Of The Branching Statements.

It Takes An Expression In Parenthesis And An Statement Or Block Of Statements. If The Expression Is True, Then The Statement Or Block Of Statements Gets Executed, Otherwise, These Statements Are Skipped.

Note :- Expression Will Be Assumed To Be True If Its Evaluated Values Is Non-zero.

If Statements Take The Following Form :

if (expression)

statement;

or

if(expression)

{

block of statements);

}

or

if(expression)

{

block of statements;

}

else

{

block of statements

}

or

if (expression)

{

block of statements;

}

else if (expression)

}

block of statements;

}

else

}

block of statements;

}

 

(ii) Conditional (?:) Operator 

The ? : operator is just like an if … else statement except that because it is an operator you can use it within expressions.

? : is a ternary operator in that it takes three values, that is the only ternary operator C has.

? takes the following form :

if condition is true ? then x return value : otherwise y value;

(iii) Switch Statement

The Switch Statement Is Must Like A nested if … else statement. Its Mostly A Matter Of Preference Within You Use, Switch Statement Can Be Slightly More Efficient And Easier To Read.

switch (expression)

{

case constant 1 :

statement 1 ;

break;

case constant 2 :

statement 2;

break;

: :

case constant n :

statement n;

break;

default :

statement;

}

If A Condition Is Met In Switch Case Then Execution Does Not Continue On Into The Next Case Thats Why We Are Using Break Keyword. It Stops The Execution Of Current Control And Take Out Of The Control From Control Information To The Next Line Of The Program.

 

Looping

Loops Provide A Way To Repeat Commands And Control How Many Times They Are Repeated. C Provides A Number Of Looping Way.

(i) While Loop

The Most Basic Loop In C Is The while loop. A while Statement Is Like A Repeating if statement.

Like An If Statement, If The Test Condition Is True : The Statements Gets Executed. The Difference Is That After The Statements Have Been Executed, The Test Condition Is Checked Again. If It Is Still True The Statements Get Executed Again. This Cycle Repeats Until The Test Condition Evaluates To False.

while (expression)

{

single statement;

or

block of statements;

}

(ii) For Loop

For Loop Is Similar To While, Its Just Written Differently. For Statements Are Often Used To Process Lists Such A Range Of Numbers.

for (expression 1; expression 2; expression 3)

{

single statement;

or

block of statements;

}

In The Above syntax :

Expression 1 – Initialize Variables

Expression 2 – Conditional Expression As Long As This Condition Is True, Loop Will Keep Executing.

Expression 3 – Expression 3 Is The Modifier Which May Simple Increment Of A Variable.

(iii) Do…While Loop

Do…while is just like a while loop except that the text condition is checked at the end of the loop ranther than the start. This Has The Effect That The Content Of The Loop Are Always Executed Atleast Once.

do

{

single statement;

or

block of statements;

}

while (expression);

 

 

 

Leave a Reply

Secured By miniOrange