Unit 3 FPL
Unit 3 FPL
Syllabus:
Decision Making and Branching: Simple If Statement, If-Else, Else-If, Switch Statement,
Goto Statement
Decision Making and Looping: While Statement, Do-While, For Statement, Break and
Continue
Control flow refers to the order in which statements within a program execute. While programs
typically follow a sequential flow from top to bottom, there are scenarios where we need more
flexibility.
1
PPS Unit-III DIT,Pimpri
2. Branching statements
a. goto statement
b. break statement
c. continue statement
Simple if statements are carried out to perform some operation when the condition is
only true.
If the condition / test expression of the if statement is true then the statement block under
the if statement is executed otherwise the if statement–block is skipped and control is
transferred to the statements outside the if block.
The statement block may be a single statement or a group of statements.
When the condition is true both the statement-block and statement-x are executed in
sequence.
Syntax
if (condition / test expression)
{
Statement-block;
}
Statement-x;
2
PPS Unit-III DIT,Pimpri
Flowchart
#include <stdio.h>
void main()
{
int age = 29;
if (age >= 18)
{
printf("Eligible to vote");
}
if (age < 18)
{
printf("Not eligible to vote");
}
}
3
PPS Unit-III DIT,Pimpri
3.1.2 If-Else
The if-else statement is an extension of simple if statement
If the test expression is true, then the true-block statements immediately following the if
statement are executed; otherwise, the false-block statements are executed.
In either case, statements inside if block or statements inside else block will be executed,
not both.
In both cases, the control is transferred subsequently to the statement-x that is statements
outside if-else block.
Syntax
if (condition / test expression)
{
True-block statements;
}
else
{
False-block statements;
}
Statement-x;
Flowchart
4
PPS Unit-III DIT,Pimpri
Syntax
if (test condition-1)
{
if (test condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-x;
In the above syntax, if the condition-1 is false, the statement-3 will be executed;
otherwise it continues to perform second test.
5
PPS Unit-III DIT,Pimpri
Flowchart
#include <stdio.h>
void main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("%d is positive.\n", num);
}
else
{
if (num < 0)
{
printf("%d is negative.\n", num);
}
else
{
printf("%d is zero.\n", num);
}
}
}
6
PPS Unit-III DIT,Pimpri
The if-else-if ladder statement is used in the scenario where there are multiple cases to be
performed for different conditions.
In if-else-if ladder statement, if a condition is true then the statements defined in the if
block will be executed, otherwise if some other condition is true then the statements
defined in the else-if block will be executed, at the last if none of the condition is true
then the statements defined in the else block will be executed.
There are multiple else-if blocks possible.
Syntax
if (condition 1)
{
Statement-1;
}
else if (condition 2)
{
Statement-2;
}
else if(condition n)
{
Statement-n;
}
else
{
Default-statement;
}
Statement-x;
7
PPS Unit-III DIT,Pimpri
Flowchart
The break statement is optional. That is, two or more case labels may belong to the same
statements.
The default label is optional. If present, it will be executed when the expression does not
find a matching case label.
There can be at most one default label.
The default may be placed anywhere but usually placed at the end.
It is permitted to nest switch statements.
Syntax
switch (expression)
{
case value-1:
block-1
break;
case value-2:
block-1
break;
...
...
default:
default-block
break;
}
statement-x
Flowchart
10
PPS Unit-III DIT,Pimpri
#include <stdio.h>
int main()
{
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Outstanding!\n" );
break;
case 'B':
printf("Excellent!\n");
break;
case 'C':
printf("Well Done\n" );
break;
case 'D':
printf("You passed\n" );
break;
case 'F':
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade);
return 0;
}
11
PPS Unit-III DIT,Pimpri
C supports the goto statement to branch unconditionally from one point to another in the
program.
The goto requires a label in order to identify the place where the branch is to be made.
A label is any valid variable name, and must be followed by a colon.
The label is placed immediately before the statement where the control is to be
transferred.
The label: can be anywhere in the program either before or after the goto label;
statement.
If the label: is placed after the goto label; some statements will be skipped and the jump
is known as a forward jump.
If the label: is before the statement goto label; a loop will be formed and some statements
will be executed repeatedly. Such a jump is known as a backward jump.
During running of a program when a statement like
goto begin;
is met, the flow of control will jump to the statement immediately following the label
begin:. This happens unconditionally.
A goto breaks normal sequential execution of the program.
12
PPS Unit-III DIT,Pimpri
The C language provides for three constructs for performing loop operations. They are
1. The while statement.
2. The do statement (do-while statement).
3. The for statement.
13
PPS Unit-III DIT,Pimpri
14
PPS Unit-III DIT,Pimpri
On exit, the program continues with the statement immediately after the body of the
loop.
The body of the loop may have one or more statements.
The braces are needed only if the body contains two or more statements.
However, it is a good practice to use braces even if the body has only one statement.
Syntax:
while (test condition)
{
Body of the loop
}
3.2.2 Do-While
On some occasions it might be necessary to execute the body of the loop before the test
is performed. Such situations can be handled with the help of the do statement.
On reaching the do statement, the program proceeds to evaluate the body of the loop
first.
At the end of the loop, the test-condition in the while statement is evaluated.
If the condition is true, the program continues to evaluate the body of the loop once
again. This process continues as long as the condition is true.
When the condition becomes false, the loop will be terminated and the control goes to
the statement that appears immediately after the while statement.
Since the test-condition is evaluated at the bottom of the loop, the do...while construct
provides an exit- controlled loop and therefore the body of the loop is always executed at
least once.
Syntax of do...while statement
do
{
body of the loop
} while(test-condition);
#include <stdio.h>
void main()
{
int i = 0;
do
{
printf("%d\n", i);
i++;
}while (i < 5);
}
16
PPS Unit-III DIT,Pimpri
2. The value of the control variable is tested using the test-condition. If the condition is true, the
body of the loop is executed; otherwise the loop is terminated and the execution continues with
the statement that immediately follows the loop.
3. When the body of the loop is executed, the control is transferred back to the for statement
after evaluating the last statement in the loop. Now, the control variable is incremented or
decremented and the new value of the control variable is again tested to see whether it satisfies
the loop condition. If the condition is satisfied, the body of the loop is again executed. This
process continues till the value of the control variable fails to satisfy the test-condition.
Syntax:
for (initialization; test-condition; increment or decrement)
{
body of the loop
}
17
PPS Unit-III DIT,Pimpri
18
PPS Unit-III DIT,Pimpri
Break statement
Jumping Out of a Loop
An early exit from a loop can be accomplished by using the break statement.
19
PPS Unit-III DIT,Pimpri
When a break statement is encountered inside a loop, the loop is immediately exited and
the program continues with the statement immediately following the loop.
When the loops are nested, the break would only exit from the loop containing it. That is,
the break will exit only a single loop.
break statement can be used within while, do, or for loops.
Syntax:
break;
Example:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
printf("%d\n", i);
}
return 0;
}
20
PPS Unit-III DIT,Pimpri
Continue statement
Skipping a Part of a Loop
During the loop operations, it may be necessary to skip a part of the body of the loop
under certain conditions.
For example, in processing of applications for some job, we might like to exclude the
processing of data of applicants belonging to a certain category. On reading the category
code of an applicant, a test is made to see whether his application should be considered
or not. If it is not to be considered, the part of the program loop that processes the
application details is skipped and the execution continues with the next loop operation.
The continue, as the name implies, causes the loop to be continued with the next iteration
after skipping any statements in between.
The continue statement tells the compiler, "SKIP THE FOLLOWING STATEMENTS
AND CONTINUE WITH THE NEXT ITERATION'.
In while and do loops, continue causes the control to go directly to the test-condition and
then to continue the iteration process.
In the case of for loop, the increment section of the loop is executed before the test-
condition is evaluated.
21
PPS Unit-III DIT,Pimpri
Syntax:
continue;
Example:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
printf("%d\n", i);
}
return 0;
}
Loop This statement lets a user exit It does not let a user make an exit from an
Construct from an overall loop construct. overall loop construct.
One can easily use the break
statement along with the switch You cannot use the continue statement
Switch and statement. You can also use it with the switch statement. Still, you can
Loop within the for loop, do-while use it within the for loop, do-while loop,
Statement loop, and the while loop. It means and the while loop. It means that continue
that break can easily occur in both can only occur in the loop and not switch.
loop and switch.
As soon as the control encounters the
The control exits immediately
continue statement, it passes automatically
Control from a loop construct as soon as it
from the very beginning of a loop
encounters the break statement.
statement.
The continue statement doesn’t cause a
The break statement causes a loop
loop termination- but leads it into its next
or a switch to terminate a case at
iteration. It means that a loop will execute
the very moment of its execution.
Function all of its iterations even if it encounters a
It means that a switch or a loop
continue statement. We use the continue
would end abruptly as soon as
statement to skip those statements that
they encounter a break.
appear after the continue in a loop.
You can denote it as: You can denote it as:
Syntax
break; continue;
22
PPS Unit-III DIT,Pimpri
Q. No. Question
1 Explain conditional statements in C.
2 Explain if statement with syntax, flowchart and example.
3 Explain if – else statement with syntax, flowchart and example.
4 Explain if – else ladder (if-else if – else) statement with syntax, flowchart and example.
5 Explain nested if statement with syntax, flowchart and example.
6 Explain switch statement with syntax, flowchart and example.
7 Explain goto statement in detail.
8 Explain looping / iterative statements in C.
9 Explain for loop with syntax and example.
10 Explain while loop with syntax and example.
11 Explain do-while loop with syntax and example.
12 Explain nested loops with example.
13 Illustrate difference between for, while and do-while loops.
14 Explain break statement with syntax and example.
15 Explain continue statement with syntax and example.
16 Differentiate between break and continue statements.
17 Write a Program to check whether a person is eligible to vote or not.
18 Write a Program to Find the Greater and smaller of Two Numbers
19 Write a program print 1 to 5 using while loop.
20 Write a c program to compute the sum of the first 10 natural numbers using for loop.
Write a C program using for loop to print all the numbers from m-n thereby classifying
21
them as even or odd.
22 Write a C program to sum the series 1+1/2+1/3+1/4….1/n
23 Write a C program to calculate sum of cubes of numbers from 1-n
24 Write a C program to calculate sum of square of even numbers
Write a C program to display the following pattern
1
25 12
1234
12345
Write a C program to display the following pattern
******
26
******
******
23
PPS Unit-III DIT,Pimpri
24
PPS Unit-III DIT,Pimpri
Case Study
Sr.
Case Study
No.
1 C Program to Make a Simple Calculator
2 C program to display month by month calendar for a given year
25
PPS Unit-III DIT,Pimpri
Case Study
#include <stdio.h>
int dayNumber(int day, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
year -= month < 3;
return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;
}
// Function that returns the name of the month for the given month Number
// January - 0, February - 1 and so on
char* getMonthName(int monthNumber)
{
char* month;
switch (monthNumber)
{
case 0:
month = "January";
break;
case 1:
month = "February";
break;
case 2:
month = "March";
break;
case 3:
month = "April";
break;
case 4:
month = "May";
break;
case 5:
month = "June";
break;
case 6:
month = "July";
break;
case 7:
month = "August";
break;
case 8:
month = "September";
break;
case 9:
month = "October";
break;
27
PPS Unit-III DIT,Pimpri
case 10:
month = "November";
break;
case 11:
month = "December";
break;
}
return month;
}
// February
if (monthNumber == 1)
{
// If the year is leap then Feb has 29 days
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return (29);
else
return (28);
}
// March
if (monthNumber == 2)
return (31);
// April
if (monthNumber == 3)
return (30);
// May
if (monthNumber == 4)
return (31);
// June
if (monthNumber == 5)
return (30);
// July
if (monthNumber == 6)
return (31);
// August
28
PPS Unit-III DIT,Pimpri
if (monthNumber == 7)
return (31);
// September
if (monthNumber == 8)
return (30);
// October
if (monthNumber == 9)
return (31);
// November
if (monthNumber == 10)
return (30);
// December
if (monthNumber == 11)
return (31);
}
// i for Iterate through months j for Iterate through days of the month - i
for (int i = 0; i < 12; i++)
{
days = numberOfDays(i, year);
{
printf("%5d", j);
if (++k > 6)
{
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
// Driver Code
int main()
{
int year = 2016;
// Function Call
printCalendar(year);
return 0;
}
30
PPS Unit-III DIT,Pimpri
}
else
{
printf("The character %c is a consonant.\n", ch);
}
return 0;
}
#include <stdio.h>
int main()
{
int A = 11, B = 2, C = 9;
printf("The numbers A, B and C are: %d, %d, %d\n", A, B,C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
else if (B >= A && B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
return 0;
}
{
printf("%d is a leap year.\n", inputYear);
}
else
{
printf("%d is not a leap year.\n", inputYear);
}
return 0;
}
#include <stdio.h>
int main()
{
int i;
printf("Alphabets from (A-Z) are:\n");
// ASCII value of A=65 and Z=90
for (i = 65; i <= 90; i++)
{
printf("%c ", i);
}
{
printf("The LCM of %d and %d is %d.", x, y,max);
break;
}
++max;
}
return 0;
}
#include <stdio.h>
int main()
{
int n = 153;
int temp = n;
int p = 0;
// Calculating the sum of individual digits
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
// Condition to check whether the value of P equals to user input or not.
if (temp == p)
{
printf("Yes. It is Armstrong No.");
}
else
{
printf("No. It is not an Armstrong No.");
}
return 0;
}
if (original_number == reversed)
{
printf(" Given number %d is a palindrome number",original_number);
}
else
{
printf(" Given number %d is not a palindrome number",original_number);
}
return 0;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Pattern Printing
}
printf("\n");
}
return 0;
}
Output
Number of rows: 5
1
22
333
4444
55555
Output
Number of rows: 6
666666
55555
4444
333
22
1
Output
*
**
***
****
*****
******
#include <stdio.h>
int main()
{
int i, j;
int rows = 5;
char character = 'A';
for (i = 0; i < rows; i++)
{
for (j = 0; j <= i; j++)
{
printf("%c ", character);
}
printf("\n");
character++;
}
return 0;
}
Output
A
BB
CCC
DDDD
EEEEE
39