C – If Statement

C – If Statement 2017-07-19T15:48:45+00:00

If Statement :

  • If statement is a conditional branching statement.
  • In conditional branching statement a condition is evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:
if (expression)
statement;
  • If the expression is evaluated and found to be true, the single statement following the “if” is executed. If false, the following statement is skipped. Here a compound statement composed of several statements bounded by braces can replace the single statement.

C – IF STATEMENT EXAMPLE PROGRAM :

Output :

Enter a number

7

Given number is positive

 

if else statement :

  • This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. The general form of the if-else statement is
if(expression)

statement1

else

statement2

Here also expression in parentheses must evaluate to (a boolean) true or false. Typically you’re testing something to see if it’s true, and then running a code block(one or more statements) if it is true, and another block of code if it isn’t. The statement1 or statement2 can be either simple or compound statement.

Output :

Enter a number

5

Given number is not Zero

You can set up an if-else statement to test for multiple conditions. The following example uses two conditions so that if the first test fails, we want to perform a second test before deciding what to do:

This brings up the other if-else construct, the  if, else if, else. This construct is useful where two or more alternatives are available for selection.

The syntax is

if(condition)

statement 1;

else if (condition)

statement 2;

…………………

…………………

else if(condition)

statement n-1;

else

statemens n ;

The various conditions are evaluated one by one starting from top to bottom, on reaching a condition evaluating to true the statement group associated with it are executed and skip other statements. If none of expression is evaluate to true, then the statement or group of statement associated with the final else is executed.

C – IF  ELSE EXAMPLE PROGRAM :

Output :

Enter a number

96

Given number is Positive

Prev Next