If statement flow dia

C has three major decision making instructions—the if statement, the if-else statement, and the switch statement.

1. The if Statement

C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:
//for single statement
if(condition)
statement;

//for multiple statement
if(condition)
{
block of statement;
}
The more general form is as follows:
//for single statement
if(expression)
statement;

//for multiple statement
if(expression)
{
block of statement;
}
Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in the ifstatement. For example all the following if statements are valid
if (3 + 2 % 5) 
printf("This works");
The expression (3 + 2 % 5) evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf("This works"); gets executed.

2. The if-else Statement

The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements ifthe expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated as
if (expression)
{
block of statement;
}
else
statement;
Note
  • The group of statements after the if upto and not including the else is called an if block. Similarly,the statements after the elseform the else block.
  • Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right.
  • Had there been only one statement to be executed in the if blockand only one statement in the else block we could have dropped the pair of braces.
  • As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above "Multiple Statements within if"must be used.

3. Nested if-elses

If we write an entire if-else construct within either the body of the ifstatement or the body of an else statement. This is called "nesting" of ifs. This is demonstrated as -
if (expression1)
statement;
else
{
if (expression2)
statement;
else
{
block of statement;
}
}

4. The if-else Ladder/else-if Clause

The else-if is the most general way of writing a multi-way decision.
if(expression1) 
statement;
else if(expression2)
statement;
else if(expression3)
statement;
else if(expression4)
{
block of statement;
}
else
statement;
The expressions are evaluated in order; if an expressionis true, the "statement" or "block of statement" associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces. The last elsepart handles the "none of the above" or default case where none of the other conditions is satisfied.

5. Switch Statements or Control Statements

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. The switch statement that allows us to make a decision from the number of choices is called a switch, or more correctlya switch-case-default, since these three keywords go together to make up the switch statement.
switch (expression) 
{
case constant-expression:
statement1;
statement2;
break;
case constant-expression:
statement;
break;
...
default:
statement;
}
  • In switch…case command, each case acts like a simple label. A label determines a point in program which execution must continue from there. Switch statement will choose one of case sections or labels from where the execution of the program will continue. The program will continue execution until it reaches break command.
  • break statements have vital rule in switch structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end of switch block will be executed (while most of the time we just want one casesection to be run).
  • default section will be executed if none of the case sections match switch comparison.

6. switch Versus if-else Ladder

There are some things that you simply cannot do with a switch.
These are:
  • A float expression cannot be tested using a switch
  • Cases can never have variable expressions (for example it is
    wrong to say case a +3 :)
  • Multiple cases cannot use same expressions.

7. Examples

  • Find Leap Year - C Language Programming
/**********************************************************
Statement - Prefect Leap Year
Programmer - Vineet Choudhary
Written For - http://developerinsider.co
**********************************************************/


#include <stdio.h>
#include <conio.h>

void main()
{
int year;
clrscr();

printf("Enter the year : ");
scanf("%d",&year);
int temp=year/100;
if(year%100==0)
{
if(temp%4==0)
{
printf("%d is leap year.",year);
}
else
{
printf("%d is ordinary year.",year);
}
}
else
{
if(year%4==0)
{
printf("%d is leap year.",year);
}
else
{
printf("%d is ordinary year.",year);
}
}

getch();
}

  • ATM money dispatch count while currencies are 1000,500 and 100
/**************************************************************************
Statement - ATM money dispatch count while currencies are 1000,500 and 100
Programmer - Vineet Choudhary
Written For - http://developerinsider.co
***************************************************************************/


#include<stdio.h>
#include<conio.h>

int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;

void main(){

unsigned long withdrawAmount;
unsigned long totalMoney;
int thousand=0,fiveHundred=0,oneHundred=0;
clrscr();

printf("Enter the amount in multiple of 100: ");
scanf("%lu",&withdrawAmount);

if(withdrawAmount %100 != 0){
printf("Invalid amount;");
getch();
return;
}

totalMoney = totalThousand * 1000 + totalFiveFundred* 500 + totalOneHundred*100;

if(withdrawAmount > totalMoney){
printf("Sorry,Insufficient money");
getch();
return;
}

thousand = withdrawAmount / 1000;
if(thousand > totalThousand)
thousand = totalThousand;
withdrawAmount = withdrawAmount - thousand * 1000;

if (withdrawAmount > 0){
fiveHundred = withdrawAmount / 500;
if(fiveHundred > totalFiveFundred)
fiveHundred = totalFiveFundred;
withdrawAmount = withdrawAmount - fiveHundred * 500;
}

if (withdrawAmount > 0)
oneHundred = withdrawAmount / 100;

printf("Total 1000 note: %d\n",thousand);
printf("Total 500 note: %d\n",fiveHundred);
printf("Total 100 note: %d\n",oneHundred);

getch();
}

  • Switch case (Resolve Error in Program) - C Language Programming

This program contains some error due to current output of this program is
 Two
Three
Which is wrong. Correct output is
Two
So, find out the error.

/*******************************************************
Statement - Switch case (Resolve Error)
Programmer - Vineet Choudhary
Written For - http://developerinsider.co
*******************************************************/


#include <stdio.h>
#include <conio.h>

void main()
{
int x = 2;
clrscr();

switch(x) {
case 2:
printf("Two\n");
case 3:
printf("Three\n");
}

getch();
}

/*Correct Output
----------------
Two
*/


<-previous artical: Expression & operator