Dept of CSE (KRCT)
MODULE-2 (PART 2)
Decision Making and Looping Statements
Unconditional Branching (Jumping) statements:
Jump statements interrupt the sequential execution of statements, so that
execution continues at a different point in the program. A jump destroys
automatic variables if the jump destination is outside their scope. There
are four statements that cause unconditional jumps in C: break,
continue, goto, and return.
Break Statement:
The break statement is used to jump out of a loop. An early exit from a
loop can be accomplished using the break statement. 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.
Syntax:
while ( .)
if (condition)
break; Gede Forever
The break statement is used to jump out of a loop. An early exit from a loop
can be accomplished using the break statement. 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.
Note 1: The break statement is only used in either switch statements or loop
statements like for, do- while, and while.
Note 2: Normally it is used with if-statement in the loop but it is not
compulsory.
Example:
#include<stdio.h>
main()
{
int i;
for(i=1; i<=10;i++)
{
printf("%d",i);
if(i==6)
break;
printf("###");
}
}
Out
put:
1##
#2#
##3
###
4##
#5#
##6
1
Scanned with OKEN Scanner
Continue Statement
Dept of CSE (KRCT)
The continue statement causes the loop to continue with the next
iteration skipping the remaining statements in that loop.
Note 1: The continue statement is only used in loop statements like for, do-
while, and while.
Note 2: Normally it is used with if-statement in the loop but it is not
compulsory.
Example :
#include<stdio.h>
main()
{
int j;
for(j=1;j<=10;j++)
{
printf("%d",j);
if(j==6)
continue;
printf("###");
}
}
Output: 1###2###3###4###5###67###8###9###10###
goto statement
The goto statement changes the normal sequence of the program
execution by transferring control to another part of the program. It is
usually not used because C is completely structured language and most
probably, the break and continue statements are used.
Syntax:
demo :
//label
if(
{
break;
}
else
goto demo; //goto statement
Example :
#include<stdio.h>
main()
{
a: goto c;
z: printf(" with");
goto e;
b: printf(" coding");
goto d;
c: printf("Learn");
goto b;
d: printf(" free");
goto z,
e: printf(" Code Forever");
}
2
Scanned with OKEN Scanner
Output:
Dept of CSE (KRCT)
Learn coding free with Code Forever
Looping statements/ Iterative Statements/:
• Looping statements in C execute the sequence of statements many times
until the stated condition becomes false.
A loop in C consists of two parts, a body of a loop and a control statement.
The control statement is a combination of some conditions that direct the
body of the loop to execute until the specified condition becomes
false.
The purpose of the C loop is to repeat the same code a number of
times.
Types of Loops in C:
Depending upon the position of a control statement in a program,
looping statement in C is classified into two types:
O
O
1. Entry controlled loop
2. Exit controlled loop
In an entry control loop in C, a condition is checked before executing the
body of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the
body of a loop. It is also called as a post-checking loop.
The control conditions must be well defined and specified otherwise
the loop will execute an infinite number of times. The loop that does
not stop executing and processes the statements number of times is
called as an infinite loop. An infinite loop is also called as an
"Endless loop."
Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
The specified condition determines whether to execute the loop body or
not.
'C' programming language provides us with three types of loop
constructs:
1. The while loop
2. The do-while loop
3. The for loop
While Loop
Do-While Loop
For Loop
In while loop, a condition is evaluated before processing a body of the loop.
If a condition is true then and only then the body of a loop is executed.
In a do... while loop, the condition is always executed after the body of a
loop. It is also called an exit-controlled loop.
In a for loop, the initial value is performed only once, then the condition
tests and compares the counter to a fixed value after each iteration, stopping
the for loop when false is returned.
Scanned with OKEN Scanner
Dept of CSE (KRCT)
While Loop in C
A while loop is the most straightforward looping structure. While
loop syntax in C programming language is as follows:
Syntax of While Loop in C
while (condition) {
}
statements;
It is an entry-controlled loop. In while loop, a condition is evaluated
before processing a body of the loop. If a condition is true then and only
then the body of a loop is executed. After the body of a loop is executed
then control again goes back at the beginning, and the condition is checked
if it is true, the same process is executed until the condition becomes false.
Once the condition becomes false, the control goes out of the loop.
After exiting the loop, the control goes to the statements which are
immediately after the loop. The body of a loop can contain more than
one statement. If it contains only one statement, then the curly braces
are not compulsory. It is a good practice though to use the curly braces even
we have a single statement in the body.
In while loop, if the condition is not true, then the body of a loop will
not be executed, not even once. It is different in do while loop which
we will see shortly.
Following program illustrates while loop in C programming example:
#include<stdio.h>
#include<conio.h>
int main()
int num=1; //initializing the variable
while(num<=10)
//while loop with condition
//incrementing operation
printf("%d\n",num);
num++
return 0;
Output:
3
نيا
4
Scanned with OKEN Scanner
7
Dept of CSE (KRCT)
10
The above program illustrates the use of while loop. In the above
program, we have printed series of
numbers from 1 to 10 using a while loop.
日
#include<stdio.h>
#include<conio.h>
int main()
1 int num=1; //initializing the variab
while (num<=10) 2/while loop with con
printf("%d\n", num);
}
num++;
return 0;
//incrementing operat 3
Do-While loop in C
A do... while loop in C is similar to the while loop except that the
condition is always executed after the body of a loop. It is also called an
exit-controlled loop.
Syntax of do while loop in C programming language is as follows:
Syntax of Do-While Loop in C
do {
statements
} while (expression);
As we saw in a while loop, the body is executed if and only if the condition
is true. In some cases, we have to execute a body of the loop at least
once even if the condition is false. This type of operation can be
achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at least once.
After the body is executed, then it checks the condition. If the condition is
true, then it will again execute the body of a loop otherwise control is
transferred out of the loop.
Similar to the while loop, once the control goes out of the loop the
statements which are immediately after the loop is executed.
LO
Scanned with OKEN Scanner
Dept of CSE (KRCT)
The critical difference between the while and do-while loop is that in while
loop the while is written at the beginning. In do-while loop, the while
condition is written at the end and terminates with a semi-colon (;)
The following loop program in C illustrates the working of a do-while
loop:
Below is a do-while loop in C example to print a table of number 2:
#include<stdio.h>
#include<conio.h>
int main()
int num=1;
do
Output:
8
10
12
14
16
//initializing the variable
//do-while loop
printf("%d\n",2*num);
num++;
} while(num<=10);
return 0;
//incrementing operation
18
20
In the above example, we have printed multiplication table of 2 using
a do-while loop. Let's see how the program was able to print the series.
Scanned with OKEN Scanner
#include<stdio.h>
#include<conio.h>
int main()
int num-1; 1 initializing t
Go
le loop
{
3
printf("%d
num++;
} while(num<=10); 4
return 0
2*num) 2
incrementi
Dept of CSE (KRCT)
For loop in C
A for loop is a more efficient loop structure in 'C' programming. The
general structure of for loop syntax in C is as follows:
Syntax of For Loop in C
for (initial value; condition; incrementation or decrementation)
statements;
}
The initial value of the for loop is performed only once.
The condition is a Boolean expression that tests and compares the
counter to a fixed value after each iteration, stopping the for loop when
false is returned.
The incrementation/decrementation increases (or decreases) the counter by a
set value.
Following program illustrates the for loop in C programming example:
#include<stdio.h>
int main()
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
printf("%d\n",number);
//to print the number
return 0;
}
Output:
Scanned with OKEN Scanner
2
نيا
10
The above program prints the number series from 1-10 using for loop.
#include<stdio.h>
int main()
{
int number: 1
2 for (number=1; number<=10; number++)
{
}
printf("%d\n", number) 3
return 0;
Dept of CSE (KRCT)
A nested loop means a loop statement inside another loop statement.
That is why nested loops are also called "loop inside loops". We can
define any number of loops inside another loop.
SYNTAX:
Outer_loop {
Inner_loop
// inner loop statements.
}
// outer loop statements.
8
00
Scanned with OKEN Scanner
Illustrate programs:
1. Armstrong Number/Not:
#include <stdio.h> int main() {
int num, r, sum = 0, temp;
printf("Input a number: ");
scanf("%d", &num);
temp = num;
while (num != 0) {
r = num % 10;
sum = sum + (r * r * r);
num = num / 10;
if (sum == temp)
}
printf("%d is an Armstrong number.\n", temp);
else
printf("%d is not an Armstrong number.\n", temp);
return 0; }
2. Display the odd numbers from 1 to n. Get the value of n from the
user.
#include <stdio.h>
int main()
int i, n;
/* Input upper limit from user */
printf("Print odd numbers till: ");
scanf("%d", &n);
printf("All odd numbers from 1 to %d are: \n", n);
/* Start loop from 1 and increment it by 1 */
for(i=1; i<=n; i++)
/* If 'i' is odd then print it */
if(i%2!=0)
{
Dept of CSE (KRCT)
9
Scanned with OKEN Scanner
}
printf("%d\n", i);
Dept of CSE (KRCT)
return 0; }
3. Factorial or not:
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
}
for (int i = 1; i <= n; i++) {
}
fact =fact*i;
printf("Factorial of %d = %d\n", n, fact);
return 0; }
4. Fibonacci Series/Sequence:
#include<stdio.h>
int main() {
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",nl,n2);//printing 0 and 1
for(i=2;i<number; ++i) {
n3=n1+n2;
printf("%d",n3);
n1=n2;
n2=n3; }
10
Scanned with OKEN Scanner
return 0; }
5. Sum of n numbers:
#include <stdio.h>
int main() {
int n, i, sum = 0;
}
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
6. Sum of Digits:
#include <stdio.h>
int main() {
int number, sum = 0;
printf("Enter a number: ");
scanf("%d", &number);
int originalNumber = number;
}
while (number > 0) {
int digit = number % 10;
sum += digit;
number /= 10;
printf("The sum of the digits of %d is: %d\n", originalNumber, sum);
return 0; }
7. Palindrome or not:
#include <stdio.h>
Dept of CSE (KRCT)
11
Scanned with OKEN Scanner
}
int main() {
int num, reversedNum = 0, originalNum, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// Reverse the number
while (num > 0) {
}
remainder = num % 10;
reversedNum = reversedNum * 10+ remainder;
num /= 10;
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum); }
return 0;
8. Pattern program:
12
1 2 3
1234
1 2 3 45
#include <stdio.h>
int main(){
int i, j;
for (i = 1; i <= 5; i++)
for(j=1;j<= i; j++)
printf("%d ", j);
Dept of CSE (KRCT)
12
Scanned with OKEN Scanner
printf("\n");
return 0; }
Dept of CSE (KRCT)
13
Scanned with OKEN Scanner