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:
The more general form is as follows:
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 validif (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
Note
- The group of statements after the
if
upto and not including theelse
is called anif block
. Similarly,the statements after theelse
form theelse block
. - Notice that the
else
is written exactly below theif
. The statements in theif block
and those in theelse block
have been indented to the right. - Had there been only one statement to be executed in the
if block
and only one statement in theelse block
we could have dropped the pair of braces. - As with the
if
statement, the default scope ofelse
is also the statement immediately after theelse
. 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 if
statement or the body of an else
statement. This is called "nesting" of ifs
. This is demonstrated as -4. The if-else Ladder/else-if Clause
The
else-if
is the most general way of writing a multi-way decision.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
else
part 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.- In
switch…case
command, eachcase
acts like a simple label. A label determines a point in program which execution must continue from there. Switch statement will choose one ofcase
sections or labels from where the execution of the program will continue. The program will continue execution until it reachesbreak
command. break
statements have vital rule inswitch
structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end ofswitch
block will be executed (while most of the time we just want onecase
section 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
These are:
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();
}
/**********************************************************
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();
}
/**************************************************************************
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
Which is wrong. Correct output is
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
*/
0 Comments