-->

Friday, 6 April 2018

Branching in c program



Branching is a transfer of control from the current statement to another statement or construct in the program unit. A branch alters the execution sequence. This means that the statement or construct immediately following the branch is usually not executed. Instead, some other statement or construct is executed, and the execution sequence proceeds from that point. The terms branch statement and branch target statement are used to distinguish between the transfer statement and the statement to which the transfer is made.
Simple if statement
An if statement consists of a boolean expression followed by one or more statements.
Syntax:
The syntax of an if statement in C programming language is:
if(boolean_expression){  /* statement(s) will execute if the boolean expression is true */}
If the boolean expression evaluates to true, then the block of code inside the if statement will be executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement(after the closing curly brace) will be executed.
C programming language assumes any non-zero and non-null values as true and if it is either zero or null, then it is assumed as false value.
Example
#include<stdio.h>
main(){
int a=5,b=6;
if(a>b){
printf(“ais greater than b”);
}
Flow chart



if...else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Syntax:
The syntax of an if...else statement in C programming language is:
if(boolean_expression){  /* statement(s) will execute if the boolean expression is true */}else{ /* statement(s) will execute if the boolean expression is false */}
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
Flow Chart



Example
#include<stdio.h>
main(){ int a=5,b=6;
if(a>b){
printf(“A is greater than B”);
else {
printf(“B is greater than A”);
}


No comments:

Post a Comment

Basic structure of c program