Objective
Learn about control structures
Discover how to use the selection control
structures if, if...else, and switch in a
program
1/14/15
Exercise
int a = 10, b, c; a is initialized to 10.
c = b = a;
a value is assigned to c and b(i.e.
b -= a--;
10)
c -= --a;
Written as b = b a then a is
decremented (b = 10-10, a=9)
a -= --a a--;
Exercise
int k=3,l=4,m,n;
Assigned value of k is 3 and l is 4
m = ++k + l--;
1st k value is incremented (4)then m = k + l
(m=4+4) finally l value is decremented (3)
n = k++ + --l;
1st l value is decremented (2)then n = k + l
(n=4+2) finally k value is incremented (5)
Output: m= 8,n=6 ,k=5 ,l=2
Exercise
void main()
{
int x = 10, y = 5, p,q;
p = x > 9;
q = x > 3 && y != 3;
printf(\n p =%d q=%d,p,q);
}
Output: p = 1,q = 1
Exercise
void main()
{
int a = 5, b = 2, c = 7, d = 3, e = 13,f;
f = a * b c % d + e;
printf(The value of F is %d,f);
}
Output: F = 22
Exercise
void main()
{
int a = 2, b = 3;
char ch = 'C';
printf("%c \t %c \n",ch,++ch);
printf("%d\t%d\n",a,a++);
printf("%d\t%d\t%d\n",b,b,++b);
printf("%d\t%d\n",a,!(a>5));
}
Output: D D
3 2
4 4 4
3 1
Exercise
/*Find the given no. is even or odd using conditional/ternary operator.*/
#include<stdio.h>
void main ()
{
int num;
printf ("Enter the value : ");
scanf ("%d", &num);
num%2==0?printf("Even"):printf("Odd");
}
Output:
Enter the value : 4
Even
Control Structures in C
Control structures control the flow of execution in a program
or function.
There are three kinds of execution flow:
Sequence:
the execution of the program is sequential.
Selection/branching:
A control structure which chooses alternative to execute.
Repetition/iterative:
A control structure which repeats a group of statements.
Step 1: Start
Step 2: Read the value of temperature in
centigrade. Store the value in C
Step 3: Calculate the temperature
F = 32+ (9*C/5)
Step 4: Display value of F
Step 5: Stop
Selection/Branching Statement
We can alter the flow of control(the order in which
statements are executed)using selection
structures.
Selection structures can be translated into C using
if statements.
Four forms of selection structures will be looked at:
One-Way
Two-Way
Nested
Switch
expression.
if is a reserved word
statement must be a single C statement.
Cont
Flowchart
Algorithm
Find the given number is positive.
Step 1: Start
Step 2: Read the number. Store the value in
num.
Step 3: if num greater than zero then
Display Number is positive
Step 4: Stop
Flowchart
Example
/*Find the given number is positive.*/
#include<stdio.h>
void main ()
{
int num;
printf ("Enter the value : ");
scanf ("%d", &num);
if(num>0)
printf (Number is positive");
}
Output:
Enter the value: 5
Number is positive
Cont
Flowchart
Example
Find the given number is positive/negative.
Algorithm:
Step 1: Start
Step 2: Read the number. Store the value in num.
Step 3: if num greater than zero then
Display Number is positive
else
display Number is negative
Step 4: Stop
Example
Flowchart
Example
/*Find the given number is positive. Or negative*/
#include<stdio.h>
void main ()
{
int num;
printf ("Enter the value : ");
scanf ("%d", &num);
if(num>0)
printf (Number is positive");
else
printf(Number is negative);
}
Output:
Enter the value: 5
Number is positive
Leap Year
http://www.timeanddate.com/date/leapyear.html
Why do we need Leap Years?
Leap Years are needed to keep our calendar in alignment with the Earth's revolutions
around the sun. It takes the Earth approximately 365.242199 days or 365 days, 5
hours, 48 minutes, and 46 seconds (a tropical year) to circle once around the Sun.
However, the Gregorian calendar has only 365 days in a year, so if we didn't add a
day on February 29 nearly every 4 years, we would lose almost six hours off our
calendar every year. After only 100 years, our calendar would be off by approximately
24 days!
Example
To find out whether a given year is a leap year/not.
Year is said to be a leap year if it is
Divisible by 4 and not divisible by 100
(year % 4 == 0 && year % 100 != 0). A
Divisible by 400 . B
year % 400 == 0
A OR B
Exercise
Write a program to find the given number
is even or odd.
Write a program to find the largest of the
given two numbers.
Write a program to determine a students
final grade and indicate whether it is
passing or failing. The final grade is
calculated
as the average
of four marks.
Average
Grade
>=50
Pass
<50
Fail
}
else
printf(Drive carefully);
Cont
Flowchart
Exercise
Determine the interestRate depending on
the value of the balance.
Balance Amount
InterestRate
0.07
0.05
0.03
Otherwise
#include<stdio.h>
void main ()
{
float balance,interestRate;
printf ("Enter the balance amount : ");
scanf ("%f", &balance);
if(balance > 50000.00)
interestRate = 0.07;
else
if(balance >= 25000.00)
interestRate = 0.05;
else
if(balance >= 1000.00)
interestRate = 0.03;
else
interestRate = 0.00;
printf("Amount = %f Interest Rate = %f",balance,interestRate);
}
Exercise
Find the biggest of three numbers.
#include<stdio.h>
void main ()
{
int num1,num2,num3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &num1,&num2,&num3);
if( num1 >= num2)
{
if(num1 >= num3)
printf(" % d is biggest ",num1);
else
printf(" %d is biggest ", num3);
}
else
{
if( num2 >= num3)
printf("%d is biggest", num2);
else
printf("%d is biggest", num3);
}
}
else-if Ladder
If there are multiple alternative, then use the else-if ladder
syntax:
Syntax:
if (condition1)
statement1
else if (condition2)
statement2
else if (conditionn)
statementn
else
statemente
Exercise
Find the given number is positive ,negative or zero.
#include<stdio.h>
void main ()
{
int num;
printf ("Enter the value : ");
scanf ("%d", &num);
if(num>0)
printf (Number is positive");
else if(num < 0)
printf(Number is negative);
else
printf(Number is zero.);
}
Output:
Enter the value: 5
Number is positive
Exercise
Determine the interestRate depending on
the value of the balance.
Balance Amount
InterestRate
0.07
0.05
0.03
Otherwise
#include<stdio.h>
void main ()
{
float balance,interestRate;
printf ("Enter the balance amount : ");
scanf ("%f", &balance);
if(balance > 50000.00)
interestRate = 0.07;
else if(balance >= 25000.00)
interestRate = 0.05;
else if(balance >= 1000.00)
interestRate = 0.03;
else
interestRate = 0.00;
printf("Amount = %f Interest Rate =
%f",balance,interestRate);
}
Exercise
Big Bazzar gives festival discount on purchase
of their products in the following percentages
i.If purchase amount < 1000 than 5% discount
ii.If purchase amount >=1000 than but <3000 then 10%
discount
iii.If purchase amount >=3000 but <5000 then 12%
discount
iv.If purchase amount > 5000 then 15% discount
Switch statement
The switch statement is used to select one of several alternatives when
the selection is based on the value of a single variable or an
expression.
Expression may be of type int or char, but not of type double or string.
Syntax:
switch (expression)
{
case label1:
statement1
break;
case label2:
statement2
break;
case labeln:
statementn
break;
default:
statementd;
}
Cont
Flowchart
case label1
true
statement1
break
statement2
break
statementn
break
false
case label2
true
false
.
.
.
case
labeln
false
default statementd
true
Exercise
Output the days of the week
#include<stdio.h>
void main()
{
int day;
printf("Enter the day number:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday");
case 2:
printf("Monday");
case 3:
printf("Tuesday");
case 4:
printf("Wednesday");
}
}
Cont
Output:
Enter the day number: 3
TuesdayWednesday
Enter the day number: 4
Wednesday
Cont
Output:
Enter the day number: 3
Tuesday
Enter the day number: 5
#include<stdio.h>
void main()
{
int day;
printf("Enter the day number:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
default:
printf("Invaild day number.");
break;
}
}
Cont
Output:
Enter the day number: 3
Tuesday
Enter the day number: 5
Invaild day number.
Exercise
Class ID
Ship Class
B or b
Battleship
C or c
Cruiser
D or d
Destroyer
F or f
Frigate
4-47
4-48
Exercise
Display number of days in a month
using Switch Statement
Input : Read month number and year.
Output : Display days in a month as 30, 31,
28/29.
Cont
Output
Month No: 2
Enter the year: 2000
Month have 29 days.
Exercise
Check whether a character entered by the
user is a vowel or not.
Input : Read the character.
Processing : a,e,i,o,u characters are vowels.
Output : Display entered character is vowel
or not
Exercise
Write a program to carry out the
arithmetic operations addition,
subtraction, multiplication and division
using switch statement.
Input : Read the two numbers and operator.
Processing : Perform the +,-,/,* operation.
Output : Display the result.
Exercise
Write a menu driven program to carry out
the arithmetic operations addition,
subtraction, multiplication and division
using switch statement
Input :
1. Display Menu (1. Addition, 2.
Subtraction..etc)
2. Read the option and read the numbers.
switch statement
syntax display is more readable in many contexts
each label set contains a reasonable number of
case labels (maximum of ten)
default label will help you to consider what will
happen if the value falls outside your set of case
label values
4-55