PROBLEM SOLVING THROUGH
PROGRAMMING
unit-2
1
Shalini Joseph,ISE
Decision Control and Looping
Statements
Shalini Joseph,ISE
Decision Control Statements
• Decision control statements are used to alter the flow of a sequence of
instructions.
• These statements help to jump from one part of the program to another
depending on whether a particular condition is satisfied or not.
• These decision control statements include:
Simple If statement
If else statement
Nesting of If else statement
The else...if ladder
Switch statement
3
Shalini Joseph,ISE
If Statement
• Executes a block of code only if the condition is true.
• First the test expression is evaluated. If the test expression is true, the statement of if
block (statement 1 to n) are executed otherwise these statements will be skipped and
the execution will jump to statement x.
FALSE
SYNTAX OF IF STATEMENT
Test
Expression
if (test expression)
{
statement 1; TRUE
.............. Statement Block
statement n; 1
}
statement x;
Statement x
4
Shalini Joseph,ISE
Example: Use if statement
#include <stdio.h>
int main ()
{
int x = 40;
if( x> 20 )
{
printf("x is greater than 20\n" );
}
printf("value of x is : %d\n", x);
return 0;
}
5
Shalini Joseph,ISE
If Else Statement
• In the if-else construct, first the test expression is evaluated. If the expression is true,
statement block 1 is executed and statement block 2 is skipped. Otherwise, if the
expression is false, statement block 2 is executed and statement block 1 is ignored. In any
case after the statement block 1 or 2 gets executed the control will pass to statement x.
Therefore, statement x is executed in every case. FALSE
SYNTAX OF IF STATEMENT Test
TRUE Expression
if (test expression)
{
statement_block 1;
} Statement Block 1 Statement Block 2
else
{
statement_block 2;
}
statement x;
Statement x
6
Shalini Joseph,ISE
Programs to Demonstrate the use If-else Statements
// PROGRAM TO FIND WHETHER A NUMBER IS EVEN OR ODD
#include<stdio.h>
Int main()
{
int a;
printf("\n Enter the value of a : ");
scanf("%d", &a);
if(a%2==0)
printf("\n %d is even", a);
else
printf("\n %d is odd", a);
return 0; Shalini Joseph,ISE
7
Programs to Demonstrate the use If-else Statements
• #include <stdio.h>
• int main() {
• int i = 10;
• if (i > 18) {
• printf("Eligible for vote");
• }
• else {
• printf("Not Eligible for vote");
• }
• return 0; Shalini Joseph,ISE
8
Nesting if else
• Placing one if…else statement inside other.
• It is used when decisions based on previous one.
TRUE FALSE
Test
Expression
1
Statement Block 1
FALSE
Test
Expression
TRUE 2
Statement Block 2 Statement Block X
Statement Y
9
Shalini Joseph,ISE
Nesting of if else statement
• Syntax:
• if (condition1) {
• // Code if condition1 is true
• if (condition2) {
• // Code if condition2 is also true
• } else {
• // Code if condition1 is true but condition2 is false
• }
• } else {
• // Code if condition1 is false
• }
10
Shalini Joseph,ISE
Checking if a number is positive,
negative, or zero
• #include <stdio.h>
• int main() {
• int num;
• printf("Enter a number: ");
• scanf("%d", &num);
• if (num >= 0) {
• if (num == 0) {
• printf("The number is ZERO.\n");
• } else {
• printf("The number is POSITIVE.\n");
• }
• } else {
• printf("The number is NEGATIVE.\n");
• }
• return 0; 11
Shalini Joseph,ISE
Else… if ladder
• The else...if ladder is a decision-making structure in C where multiple conditions are
checked one after another in sequence.
• If one condition is true, its block executes and the rest are skipped.
• If no conditions are true, the final else block (if present) executes.
12
Shalini Joseph,ISE
• SYNTAX
• if ( test expression 1)
• {
• statement block 1;
• }
• else if ( test expression 2)
• {
• statement block 2;
• }
• ...........................
• else if (test expression N)
• {
• statement block N;
• }
• else
• {
• Statement Block X;
• }
13
• Statement Y; Shalini Joseph,ISE
Example: else if ladder Program
Write a C program to find the roots of a quadratic
equation ax2+bx+c=0.
• #include <stdio.h>
• #include <math.h> // for sqrt() function
• int main() {
• float a, b, c, d, root1, root2;
• // Step 1: Input values
• printf("Enter coefficients a, b and c: ");
• scanf("%f %f %f", &a, &b, &c);
• // Step 2: Calculate discriminant
• d = b*b - 4*a*c;
• // Step 3: Check discriminant and find roots
• if(d > 0) {
• root1 = (-b + sqrt(d)) / (2*a);
• root2 = (-b - sqrt(d)) / (2*a);
14
Shalini Joseph,ISE
• printf("Roots are real and different.\n");
• printf("Root1 = %.2f , Root2 = %.2f\n", root1, root2);
• }
• else if(d == 0) {
• root1 = -b / (2*a);
• printf("Roots are real and equal.\n");
• printf("Root1 = Root2 = %.2f\n", root1);
• }
• else {
• printf("Roots are imaginary (complex roots).\n");
• }
• return 0;
• }
15
Shalini Joseph,ISE
Grade Classification using else-if Ladder
• #include <stdio.h>
• int main() {
• int marks = 82;
• if (marks >= 90) {
• printf("Grade A\n");
• }
• else if (marks >= 75) {
• printf("Grade B\n");
• }
• else if (marks >= 50) {
• printf("Grade C\n");
• }
• else {
• printf("Fail\n");
• }
16
• return 0; Shalini Joseph,ISE
Difference between nested if else and
else if ladder
Feature Nested if...else else-if ladder
if inside another if Chain of conditions
Structure
(hierarchical) (sequential ladder)
When decisions depend When you want to check
Use case
on earlier decisions multiple choices
Inner if is checked only if Each condition is
Flow
outer is true checked one by one
Straight ladder (top to
Shape (logic) Tree-like branching
bottom)
17
Shalini Joseph,ISE
Switch Case
• A switch case statement is a multi-way decision statement.
• Alternative to else if ladder statement.
• Switch statements are used:
When there is only one variable to evaluate in the expression
When many conditions are being tested for
• Switch case statement advantages include:
Easy to debug, read, understand and maintain
Execute faster than its equivalent if-else construct
18
Shalini Joseph,ISE
Switch Case
switch(grade)
{
case 'A':
printf("\n Excellent");
break;
case 'B':
printf("\n Good");
break;
case 'C':
printf("\n Fair");
break;
default:
printf("\n Invalid Grade");
break; 19
Shalini Joseph,ISE
}
20
Shalini Joseph,ISE
• Expression must evaluate to an integer or character (not float or
string).
• case values must be keyword, not variables.
• break; is used to stop execution after a case. Without break;,
control falls through to the next case.
• default is optional and executes if no case matches.
21
Shalini Joseph,ISE
Example:Switch Case
// PROGRAM TO PRINT THE DAY OF THE WEEK
#include<stdio.h>
int main()
{
int day;
printf(“\n Enter any number from 1 to 7 : “);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“\n SUNDAY”); break;
case 2 : printf(“\n MONDAY”); break;
case 3 : printf(“\n TUESDAY”); break;
case 4 : printf(“\n WEDNESDAY”); break;
case 5 : printf(“\n THURSDAY”); break;
case 6 : printf(“\n FRIDAY”); break;
case 7 : printf(“\n SATURDAY”); break;
default: printf(“\n Wrong Number”);
}
return 0;
} 22
Shalini Joseph,ISE
Example 2: Simple Calculator
• #include <stdio.h>
• int main() {
• int a, b;
• char op;
• printf("Enter two numbers: ");
• scanf("%d %d", &a, &b);
• printf("Enter operator (+, -, *, /): ");
• scanf(" %c", &op); // space before %c to ignore newline
• switch(op) {
• case '+': printf("Sum = %d\n", a + b); break;
• case '-': printf("Difference = %d\n", a - b); break;
• case '*': printf("Product = %d\n", a * b); break;
• case '/': printf("Quotient = %d\n", a / b); break;
• default: printf("Invalid operator\n");
• }
• return 0; 23
Shalini Joseph,ISE
• Example 3: Vowel or Consonant
• #include <stdio.h>
• int main() {
• char ch;
• printf("Enter a character: ");
• scanf(" %c", &ch);
• switch(ch) {
• case 'a':
• case 'e':
• case 'i':
• case 'o':
• case 'u':
• case 'A':
• case 'E':
• case 'I':
• case 'O':
• case 'U':
• printf("Vowel\n");
• break;
• default:
• printf("Consonant\n");
• } 24
• Shalini Joseph,ISE
return 0;
Decision Looping statement (Iterative Statements)
• Loops are used to execute a block of code multiple times
until a condition is met.
while loop
do-while
for loop
25
Shalini Joseph,ISE
While Loop
WHILE LOOP
• The while loop is used to repeat one or more statements
while a particular condition is true.
• In the while loop, the condition is tested before any of the
statements in the statement block is executed. It is an entry
Controlled loop.
• While loop is used when the number of iterations is not
known in advance and it continues as long the condition is
true.
26
Shalini Joseph,ISE
while (condition)
{
statement_block;
}
statement x;
27
Shalini Joseph,ISE
Example 1: Printing numbers 1 to 5
• #include <stdio.h>
• int main() {
• int i = 1;
• while (i <= 5) {
• printf("%d\n", i);
• i++;
• }
• return 0;
•}
28
Shalini Joseph,ISE
Lab Program using While loop
• Write a C program to find the sum of all the digits and
occurrence of a digit in the number.
• Write a C program to find the GCD and LCM of given two
numbers using method.
29
Shalini Joseph,ISE
Do While Loop
DO WHILE LOOP
• The do-while loop is similar to the while loop. The only difference is that in a
do-while loop, the test condition is tested at the end of the loop. It is an
Exit Controlled loop.
• The body of the loop gets executed at least one time (even if the condition is
false).
• Do-while loops are widely used to print a list of options for a menu driven
program.
30
Shalini Joseph,ISE
Iterative Statements
DO WHILE LOOP
Statement x;
do
{
statement_block;
} while (condition);
statement y;
31
Shalini Joseph,ISE
Programs to Demonstrate the use of do while loop
// PROGRAM TO PRINT NUMBERS FROM 0-10 USING DO-WHILE LOOP
#include<stdio.h>
int main()
{
int i = 0;
do
{
printf(“\n %d”, i);
i = i + 1;
} while(i<=10);
return 0;
}
32
Shalini Joseph,ISE
Difference between while loop and
do. While loop
Feature while Loop do...while Loop
Condition check At the start of loop At the end of loop
Runs at least once
May never run if
Execution even if condition is
condition is false
false
No semicolon after Must have a semicolon
Syntax ending
loop after condition
33
Shalini Joseph,ISE
For Loop
• For loop is used to repeat a task until a particular condition is true.
• The syntax of a for loop
for (initialization; condition; increment/decrement)
{
statement block;
}
Statement Y;
34
Shalini Joseph,ISE
35
Shalini Joseph,ISE
For Loop
• Initialization
• Executed only once at the start.
• Used to declare and set the starting value of the loop variable.
• Condition
• Checked before each iteration.
• If true → loop body executes.
• If false → loop stops.
• Update
• Executed after each iteration.
• Used to modify the loop variable (increment/decrement).
36
Shalini Joseph,ISE
Program for For-Loop
Look at the code given below which print first n numbers using a for loop.
#include<stdio.h>
int main()
{
int i, n;
printf(“\n Enter the value of n :”);
scanf(“%d”, &n);
for(i=0; i<= n; i++)
printf(“\n %d”, i);
return 0;
}
37
Shalini Joseph,ISE
When to Use for loop and while loop
Feature for loop while loop
Known number of Unknown number of
Best used for
iterations iterations
Inside the loop
Initialization Before the loop
header
Inside the loop
Update Inside the loop body
header
Condition checking Before each iteration Before each iteration
38
Shalini Joseph,ISE
Factorial
#include <stdio.h>
int main() {
int n, i;
int fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d = %d", n, fact);
}
return 0; 39
Shalini Joseph,ISE
}
Fibonacci Program
• #include <stdio.h>
• int main() {
• int n, i;
• int t1 = 0, t2 = 1, next;
• printf("Enter the number of terms: ");
• scanf("%d", &n);
• printf("Fibonacci Series: ");
• for(i = 1; i <= n; i++) {
• printf("%d ", t1);
• next = t1 + t2;
• t1 = t2;
• t2 = next;
• }
• return 0; 40
Shalini Joseph,ISE
• Write a C program to print the prime numbers in a given range
41
Shalini Joseph,ISE
Print Star pattern
• #include <stdio.h>
• int main() {
• int i, j, n;
• printf("Enter the number: ");
• scanf("%d", &n);
• for(j = 1; j <= n; j++) {
• for(i = 1; i <= n; i++) {
• printf("*");
• }
• printf("\n"); // Move to next line after each row
• }
• return 0; 42
Shalini Joseph,ISE
Print Star pattern
• #include <stdio.h>
• int main() {
• int i, j, n;
• printf("Enter the number: ");
• scanf("%d", &n);
• for(j = 1; j <= n; j++) {
• for(i = 1; i <= j; i++) {
• printf("*");
• }
• printf("\n"); // Move to next line after each row
• }
• return 0; 43
Shalini Joseph,ISE
• #include <stdio.h>
• int main() {
• int i,n,j;
• printf("Enter the number:");
• scanf("%d",&n);
• for(j=1;j<=n;j++) {
• for(i=1;i<=n;i++)
• { printf("%d",j);
• } printf("\n");
• } return 0;
•} Shalini Joseph,ISE
44
• #include <stdio.h>
• int main() {
• int i,n,j;
• printf("Enter the number:");
• scanf("%d",&n);
• for(j=1;j<=n;j++) {
• for(i=1;i<=n;i++)
• { printf("%d",i);
• } printf("\n");
• } return 0;
•} Shalini Joseph,ISE
45
• #include <stdio.h>
• int main() {
• int i,n,j;
• printf("Enter the number:");
• scanf("%d",&n);
• for(j=1;j<=n;j++) {
• for(i=1;i<=j;i++)
• { printf("%d",i);
• } printf("\n");
• } return 0;
•} Shalini Joseph,ISE
46
Inverted Triangle of Stars
• #include <stdio.h>
• int main() {
• int i, j, n;
• printf("Enter size: ");
• scanf("%d", &n);
• for(i = n; i >= 1; i--) {
• for(j = 1; j <= i; j++) {
• printf("*");
• }
• printf("\n");
• }
• return 0;
• }
47
Shalini Joseph,ISE
Right-Aligned Right-Angled Triangle
• #include <stdio.h>
• int main() {
• int n, i, j;
• printf("Enter number of rows: ");
• scanf("%d", &n);
• for (i = 1; i <= n; i++) {
• // Print spaces
• for (j = 1; j <= n - i; j++) {
• printf(" "); // double space for alignment
• }
• // Print stars
• for (j = 1; j <= i; j++) {
• printf("* ");
• }
• printf("\n");
• }
48
• return 0; Shalini Joseph,ISE
Pyramid Pattern Program in C
• #include <stdio.h>
• int main() {
• int i, j, space, n;
• printf("Enter number of rows: ");
• scanf("%d", &n);
• for(i = 1; i <= n; i++) {
• // Print spaces before stars
• for(space = 1; space <= n - i; space++) {
• printf(" ");
• }
• // Print stars
• for(j = 1; j <= (2*i - 1); j++) {
• printf("*");
• }
• printf("\n");
• }
49
• return 0; Shalini Joseph,ISE
Break Statement
• The break statement is used to terminate the execution of the nearest enclosing
loop in which it appears.
• break statement is also used to terminate looping statements like while, do-
while and for.
• Its syntax is quite simple, just type keyword break followed with a semi-colon.
break;
• In switch statement if the break statement is missing then every case from the
matched case label to the end of the switch, including the default, is executed.
50
Shalini Joseph,ISE
51
Shalini Joseph,ISE
Continue Statement
• The continue statement is used to move the program execution
control to the beginning of the looping statement.
• Its syntax is quite simple, just type keyword continue followed with a
semi-colon.
continue;
• When the continue statement is encountered in a looping statement,
the execution control skips the rest of the statements in the looping
block and directly jumps to the beginning of the loop.
52
Shalini Joseph,ISE
int i;
for(i=0; i<= 10; i++)
{ if (i==5)
continue;
printf(“\t %d”, i);
}
53
Shalini Joseph,ISE
54
Shalini Joseph,ISE
Goto Statement
• The goto statement is used to jump from one line to another line in
the program.
• Using goto statement we can jump from top to bottom or bottom to
top.
• To jump from one line to another line, the goto statement requires
a label.
• Label is a name given to the instruction or line in the program.
• When we use a goto statement in the program, the execution control
directly jumps to the line with the specified label.
55
Shalini Joseph,ISE
#include<stdio.h>
void main(){
printf("We are at first printf statement!!!\n") ;
goto last ;
printf("We are at second printf statement!!!\n") ;
printf("We are at third printf statement!!!\n") ;
last: printf("We are at last printf statement!!!\n") ;
}
56
Shalini Joseph,ISE
Thank You!
57
Shalini Joseph,ISE