Conditional statements and looping in c are used to change the flow of control of the program.
Flow of control means that the way in which the program executes.
Conditional statements in c:
- If statement : if statement is the simple conditional statement in c.if statement contains condition along with this.if followed with the condition , if this conditional is true then the statements followed by if statement is executed.
Syntax : if(condition){
// block of statements
}
- If_else statement : if _else is also the conditional statement in c which most commonly used.if followed by condition if this condition is true then the block of statements followed by if is executed. otherwise the statement in else block is executed.
Syntax : if(condition){
// block of statements
}else
{
// block of statements
}
- switch statement : this switch statement which contains multiple branch statements.Its checks with each value. each value is called as the case.if this case true then statement in this case is executed.
Syntax :
switch(expression){
case value:
// statements.
break;
case value:
// statements.
break;
case default:
//statements
break;
}
we write break after each case because we do not need to execute rest of the cases.
Looping statements in c:
- while loop : This loop checks the condition first if its true then the block of statements followed by the while is executed else not.
syntax :
while(condition){
//statements
}
- do _ while loop : This loop checks the condition at the end of the loop.It executes the block first after that check the condition.The do _while loop executes at least once.
syntax :
do{
//statements
} while(condition);
- for loop : This is most commonly used looping in c.the first statement in loop treated as statement, second treated as the condition if its true then loop executes , and third treated as the the increament or decrements which used for the iterating loop .
Syntax : for(exp1;exp2;exp3;){
//statements
}
- Break statement : This is usually used to break the flow of control. or to terminate loop and program flow goes to next statement after loop.
- Continue statement : This is slightly similar to break.It restart loop with next value.The code below continue skipped and move to next iteration.