Control structure in C-Decision Making Structures
Control structure in C-Decision Making Structures
Introduction
• In real life problems are not simple and linear. Some
of the actions are to be performed on fulfilment of
certain conditions. Some of the actions are to be
repeated for certain number of times or till a
condition is satisfied.
• ‘C’ language provides different control structures for
decision making- like if statement, if…else statement,
else-if ladder, switch statement, goto statement and
break statement.
if statement
• If statement checks a condition. If the condition is
true then the statements are executed, otherwise
they are not executed.
• The syntax of if statement is:
if(condition)
statement(s);
• If there are more than one statements to be executed
then they are put within { } symbols.
if else statement
• In certain cases, we want to execute one set of
statements, if condition is satisfied, and other set of
statements if condition is false. In that case we can use
if… else statement.
• The syntax is :
if(condition)
statement(s)1;
else
statement(s)2;
• If condition is TRUE, then statement(s)1 are executed,
otherwise statement(s)2 are executed.
Program – if else
/* Write a program to find larger of two numbers using if statement */
#include<stdio.h>
void main()
{
float num1,num2;
printf(“Enter one number\n”);
scanf(“%f”,&num1);
printf(“Enter second number\n”);
scanf(“%f”,&num2);
if (num1 > num2)
printf(“%f is larger\n”,num1);
else
printf(“%f is larger\n”,num2);
}
Nested if else
• As the name suggest, if is used inside if statement.
The nesting can be upto any level.
Program – nested if else
/* Write a program to find maximum of three numbers */
#include<stdio.h>
void main()
{
int num1,num2,num3;
int max;
printf(“Enter three numbers\n”);
scanf(“%d%d%d”,&num1,&num2,&num3);
if (num1 > num2)
{
if (num1 > num3)
max = num1; /* num1 largest */
else
max = num3; /* num3 largest */
}
else
{
if (num2 > num3)
max = num2; /* num2 largest */
else
max = num3; /* num3 largest */
}
printf(“maximum of %d, %d and %d is = %d\n”,num1,num2,num3,max);
}
Else if ladder
• The two level choice provided by if else statement is not sufficient when we have multiple choices.
‘C’ provides multiple if statement for that. The syntax is:
• if (condition1)
statement(s)1;
else if (condition2)
statement(s)2;
else if (condition3)
statement(s)3;
.
.
.
else if(conditionN)
statement(s)N;
else
default_statement(s);
Program - Week day number to name
/* Write a program which asks day number and prints corresponding day name i.e if input is 5, it will print the fifth day of week which is
Thursday. Sunday being the first day. */
#include<stdio.h>
main()
{
int day;
printf(“Enter day number between (1-7)\n”);
scanf(“%d”,&day);
if(day==1)
printf(“Sunday\n”);
/* equality checked with == (= symbol twice) and not single = */
else if(day==2)
printf(“Monday\n”);
else if(day==3)
printf(“Tuesday\n”);
else if(day==4)
printf(“Wednesday\n”);
else if(day==5)
printf(“Thursday\n”);
else if(day==6)
printf(“Friday\n”);
else if(day==7)
printf(“Saturday\n”);
else
printf(“Wrong input\n”);
}
Program – find character type digit,
uppercase, lowercase etc
/* Write a program to check the category of given character. Digit, Upper Case, Lower
Case or other symbol */
#include<stdio.h>
void main()
{
char ch;
printf(“Enter one character\n”);
scanf(“%c”,&ch);
if (( ch >= ‘0’) && (ch <= ‘9’))
printf(“The character %c is digit\n”,ch);
else if (( ch >= ‘a’) && (ch <= ‘z’))
printf(“The character %c is Lower Case\n”,ch);
else if (( ch >= ‘A’) && (ch <= ‘Z’))
printf(“The character %c is Upper Case\n”,ch);
else
printf(“The character %c is other symbol\n”,ch);
}
switch statement
• Multiple if statement is difficult to manage if there are many choices to handle. ‘C’ provides a
better way for handling multiple choices by switch statement. The syntax is :
• switch (variablename or expression)
{
case value1 : statement(s)1;
break;
case value2 : statement(s)2;
break;
.
.
.
case valueN : statement(s)N;
break;
default : default_statement(s);
}
switch statement (cont)
• Writing the default value and its corresponding
default_statement(s) is optional.
• The value of expression or variable written after the
switch statement must be either character or integer
value.
• In multiple if…else statement, we have many
conditions, while in switch statement, we have only
one expression.
Program week day number to name using switch
statement
/* Write a program which asks day number and prints corresponding day name. */
#include<stdio.h>
void main()
{
int day;
printf(“Enter day number between (1-7)\n”);
scanf(“%d”,&day);
switch(day) /* day variable has only integer value*/
{
case 1:
printf(“Sunday\n”);
break; /* break statement here*/
case 2:
printf(“Monday\n”);
break; /* break statement here*/
case 3:
printf(“Tuesday\n”);
break; /* break statement here*/
Program week day number to name using switch
statement (cont)
case 4:
printf(“Wednesday\n”);
break; /* break statement here*/
case 5:
printf(“Thursday\n”);
break; /* break statement here*/
case 6:
printf(“Friday\n”);
break; /* break statement here*/
case 7:
printf(“Saturday\n”);
break; /* break statement here*/
default:
/* case where value is outside range 1 to 7 */
printf(“Wrong input\n”);
}
}
Program – month number to number of days
#include<stdio.h>
void main()
{
int month;
printf(“Give month number between (1-12)\n”);
scanf(“%d”,&month);
switch(month)
{
/* number of days in month 1,3,5,7,8,10 and
12=31 so same case for all */
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf(“Number of days in month
number %d is = 31\n”);
break;
case 4: /* number of days in month 4,6,9 and 11=30
so same case for all */
Program – month number to number of days (cont)
case 6: case 9: case 11:
printf(“Number of days in month
number %d is = 30\n”);
break;
case 2:
printf(“Number of days in month
number %d is = 28 or 29\n”);
break;
default:
printf(“Wrong input\n”);
break;
}
}
break statement
• It causes an exit from switch body.
• If we do not write break statement after case
statement, then next switch statements are executed
thus giving wrong answer.
default statement
• If any of the case statements in switch do not match,
then the statements written in default part are
executed.
• This statement is optional.
• Normally, it is written in switch statement after all
cases are over.