Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

‭ITP Notes for I B.

Tech CSM - A, SVCET, Chittoor‬


‭ NIT - II‬
U
‭Control Structures‬

‭Control‬ ‭statements‬ ‭control‬ ‭the‬ ‭flow‬‭of‬‭execution‬‭of‬‭the‬‭statements‬‭of‬‭a‬‭program.‬‭The‬‭various‬


‭types of control statements in C language are as under:‬
‭I.‬ ‭Sequential‬
‭II.‬ ‭Conditional‬
‭III.‬ ‭Iteration‬

‭Sequential control:‬
‭In‬ ‭sequential‬ ‭control,‬ ‭the‬ ‭C‬ ‭program‬ ‭statements‬ ‭are‬ ‭executed‬ ‭sequentially‬ ‭i.e.,‬ ‭one‬ ‭after‬ ‭the‬
‭another from beginning to end.‬

‭ xample for simple sequential program‬


E
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int x , y, sum;‬
‭printf(“Enter the two numbers”);‬
‭scanf(“%d%d”,&x,&y);‬
‭sum=x+y;‬
‭printf(“The sum of the two numbers is =%d”,sum);‬
‭}‬
‭Output:‬
‭Enter the two numbers33‬
‭4‬
‭The sum of the two numbers is =37‬

‭Conditional Control Structure/Statement (Selection Control or Decision Control):‬


‭●‬ ‭In conditional control , the execution of statements depends upon the condition-test.‬
‭●‬ ‭If‬‭the‬‭condition‬‭evaluates‬‭to‬‭true,‬‭then‬‭a‬‭set‬‭of‬‭statements‬‭is‬‭executed,‬‭otherwise‬‭another‬‭set‬
‭of statements is followed.‬
‭●‬ ‭This‬ ‭control‬ ‭is‬ ‭also‬ ‭called‬ ‭Decision‬ ‭Control‬ ‭because‬ ‭it‬ ‭helps‬ ‭in‬ ‭making‬ ‭decisions‬ ‭about‬
‭which set of statements is to be executed.‬

‭Decision control structure in C can be implemented by using:-‬


‭1. If statement‬
‭2. If-else statement‬
‭3. Nested if else statement‬
‭4. else-if ladder‬
‭1‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭ . case control structure‬
5
‭6. conditional operator‬

‭Iteration Control ( Loops )‬


‭●‬ ‭Iterations‬ ‭or‬ ‭loops‬ ‭are‬ ‭used‬ ‭when‬ ‭we‬ ‭want‬ ‭to‬ ‭execute‬ ‭a‬ ‭statement‬ ‭or‬ ‭block‬ ‭of‬ ‭statements‬
‭several times.‬
‭●‬ ‭The repetition of loops is controlled with the help of a test condition.‬
‭●‬ ‭The‬ ‭statements‬ ‭in‬ ‭the‬ ‭loop‬ ‭keep‬‭on‬‭executing‬‭repetitively‬‭until‬‭the‬‭test‬‭condition‬‭becomes‬
‭false.‬
‭●‬ ‭There are the following three types of loops:-‬
‭1. While loop‬
‭2. Do-while loop‬
‭3. For loop‬
‭Decision control‬
‭ onditional‬ ‭Control‬ ‭(Selection‬ ‭Control‬ ‭or‬ ‭Decision‬ ‭Control)‬ ‭:‬ ‭In‬ ‭conditional‬ ‭control,‬ ‭the‬
C
‭execution‬ ‭of‬‭statements‬‭depends‬‭upon‬‭the‬‭condition-test.‬‭If‬‭the‬‭condition‬‭evaluates‬‭to‬‭true,‬‭then‬‭a‬‭set‬
‭of‬ ‭statements‬ ‭is‬ ‭executed,‬‭otherwise‬‭another‬‭set‬‭of‬‭statements‬‭is‬‭followed.‬‭This‬‭control‬‭is‬‭also‬‭called‬
‭Decision‬ ‭Control‬ ‭because‬ ‭it‬ ‭helps‬ ‭in‬ ‭making‬ ‭decisions‬ ‭about‬ ‭which‬ ‭set‬ ‭of‬ ‭statements‬ ‭is‬ ‭to‬ ‭be‬
‭executed.‬
‭Decision control structure in C can be implemented by using:‬
‭1. Simple if statement‬
‭2. if-else statement‬
‭3. Nested if else statement‬
‭4. else-if ladder‬
‭5. case control structure‬
‭6. conditional operator‬
‭1. Simple if statement:‬
‭This‬‭is‬‭the‬‭most‬‭simple‬‭form‬‭of‬‭decision‬‭control‬‭statement.‬‭In‬‭this‬‭form,‬‭a‬‭set‬‭of‬‭statements‬‭are‬
‭executed‬ ‭only‬ ‭if‬ ‭the‬ ‭condition‬ ‭given‬ ‭with‬‭if‬‭evaluates‬‭to‬‭true.‬‭Its‬‭general‬‭syntax‬‭and‬‭flow‬‭chart‬‭is‬‭as‬
‭under:‬
‭if(condition)‬
‭{‬
‭Statements ;‬
‭}‬

‭2‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬

‭ xample program to understand the if statement:‬


E
‭/* program to check whether the given number is negative*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int num;‬
‭printf(“Enter the number:”);‬
‭scanf(“%d”,&num);‬
‭if(num<0)‬
‭{‬
‭printf(“The entered number is negative”);‬
‭}‬
‭}‬
‭Output:‬
‭Enter the number:-9‬
‭The entered number is negative‬

‭2. if- else statement:‬


‭This‬ ‭is‬ ‭a‬ ‭bi-directional‬ ‭control‬ ‭statement.‬ ‭This‬ ‭statement‬ ‭is‬ ‭used‬ ‭to‬ ‭test‬‭a‬‭condition‬‭and‬‭take‬
‭one‬ ‭of‬ ‭the‬ ‭two‬ ‭possible‬ ‭actions.‬ ‭If‬ ‭the‬ ‭condition‬ ‭evaluates‬ ‭to‬ ‭true‬ ‭then‬ ‭one‬ ‭statement‬ ‭(or‬ ‭block‬ ‭of‬
‭statements) is executed otherwise another statement (or block of statements) is executed.‬
‭The general form and flowchart of if-else statement is as under:‬
‭if(condition)‬
‭{‬
‭block of statements;‬
‭}‬
‭else‬
‭{‬
‭block of statements;‬
‭}‬

‭3‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬

‭ xample program to illustrate if-else statement:‬


E
‭/*Program to find the biggest of two numbers*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int num1,num2 ;‬
‭printf("Enter two numbers:");‬
‭scanf("%d %d",&num1,&num2);‬
‭if(num1>num2)‬
‭{‬
‭printf("The number %d is big.",num1);‬
‭}‬
‭else‬
‭{‬
‭printf("The number %d is big.",num2);‬
‭}‬
‭}‬

‭ utput:‬
O
‭Enter two numbers:‬
‭43‬
‭54‬
‭The number 54 is big.‬

‭3. Nested if-else statement:‬


‭If‬ ‭we‬ ‭have‬ ‭if-else‬ ‭statements‬ ‭within‬ ‭either‬ ‭the‬ ‭body‬ ‭of‬ ‭an‬ ‭if‬ ‭statement‬ ‭or‬ ‭the‬ ‭body‬ ‭of‬ ‭else‬
‭statement or in the body of both‬‭if‬‭and‬‭else‬‭, then‬‭this is known as nesting of if else statement.‬
‭The general syntax of nested if-else statement is as follows:‬
‭if(condition1)‬
‭{‬
‭if(condition2)‬
‭{‬
‭statements;‬
‭}‬
‭4‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
e‭ lse‬
‭{‬
‭statements;‬
‭}‬
}‭ ‬
‭else‬
‭{‬
i‭f(condition3)‬
‭{‬
‭statements;‬
‭}‬
‭else‬
‭{‬
‭statements;‬
‭}‬
}‭ ‬
‭Example program to explain nesting of if else:‬
‭/*program to find the largest of three numbers*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a, b,c,large;‬
‭printf(“Enter three numbers:”);‬
‭scanf(“%d%d%d”,&a,&b,&c);‬
‭if(a>b)‬
‭{‬
‭if(a>c)‬
‭large=a;‬
‭else‬
‭large=c;‬
‭}‬
‭else‬
‭{‬
‭if(b>c)‬
‭large=b;‬
‭else‬
‭large=c;‬
‭}‬
‭printf(“Largest number is %d”,large);‬
‭}‬

‭ utput:‬
O
‭Enter three numbers:‬
‭22‬
‭55‬
‭4‬
‭Largest number is 55‬
‭5‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬

‭4.Else if ladder:‬
‭This‬‭is‬‭a‬‭type‬‭of‬‭nesting‬‭in‬‭which‬‭there‬‭is‬‭an‬‭if-else‬‭statement‬‭in‬‭every‬‭else‬‭part‬‭except‬‭the‬‭last‬
‭else part. This type of nesting is called else if ladder.‬
‭The general syntax and flowchart of else if ladder is as follows:-‬
‭if(condition1)‬
‭statementA;‬
‭else if(condition2)‬
‭statementB;‬
‭else if(condition3)‬
‭statementC;‬
‭else‬
‭statementD;‬

‭ xample program for else if ladder::‬


E
‭/*program to find the grade of the student*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int marks;‬
‭printf(“Enter the percentage of the student:”);‬
‭scanf(“%d”,&marks);‬
‭if(marks>=80)‬
‭printf(“A grade”);‬
‭else if(marks>=70)‬
‭printf(“B grade”);‬
‭else if(marks>=60)‬
‭printf(“C grade”);‬
‭6‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭else if(marks>=50)‬
‭printf(“D grade”);‬
‭else‬
‭printf(“FAIL”);‬
‭}‬

‭ utput:‬
O
‭Enter the percentage of the student:89‬
‭A grade‬

‭5.Switch case statement:‬


‭ witch‬‭case‬‭statements‬‭are‬‭a‬‭substitute‬‭for‬‭long‬‭if‬‭statements‬‭and‬‭have‬‭more‬‭flexibility‬‭and‬‭a‬
S
‭clearer format than else-if ladder.‬
‭This‬ ‭statement‬ ‭is‬ ‭used‬ ‭to‬ ‭select‬ ‭one‬ ‭out‬ ‭of‬ ‭the‬ ‭several‬ ‭numbers‬ ‭of‬ ‭alternatives‬ ‭present‬ ‭in‬ ‭a‬
‭block.‬‭This‬‭selection‬‭statement‬‭successively‬‭tests‬‭the‬‭value‬‭of‬‭an‬‭expression‬‭against‬‭a‬‭list‬‭of‬‭integer‬‭or‬
‭character constants. When a match is found, the statements associated with that constant are executed.‬
‭The general syntax of switch case statement is as follows:‬
‭switch(expression)‬
‭{‬
‭case constant1: statements;‬
‭case constant2: statements;‬
‭case constant3: statements;‬
‭………………………‬
‭………………………‬
‭case constantN: statements;‬
‭default : statement;‬
‭}‬
‭Here‬‭switch‬‭,‬‭case‬‭and‬‭default‬‭are keywords.‬
‭●‬ ‭The‬ ‭expression‬ ‭following‬ ‭the‬ ‭switch‬ ‭keyword‬ ‭must‬ ‭give‬ ‭an‬ ‭integer‬ ‭value.‬ ‭This‬
‭expression‬ ‭can‬ ‭be‬ ‭any‬ ‭integer‬ ‭or‬ ‭character‬ ‭variable,‬ ‭or‬ ‭a‬ ‭function‬ ‭call‬ ‭that‬‭returns‬‭an‬
‭integer.‬
‭●‬ ‭It can also be an arithmetic, relational, logical or bitwise expression yielding an integer.‬
‭●‬ ‭It can be integer or character constant also.‬
‭●‬ ‭Data types long int and short int are also allowed.‬
‭The‬ ‭constant‬ ‭following‬ ‭the‬ ‭case‬ ‭keyword‬ ‭should‬ ‭be‬ ‭of‬ ‭integer‬ ‭or‬ ‭character‬ ‭type.‬ ‭We‬ ‭cannot‬ ‭use‬
‭floating point or string constant.‬
‭Example program for switch case statement‬
‭/*program to print day of the week*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int day;‬
‭printf("Enter the day number from 1 to 7 : ");‬
‭scanf("%d",&day);‬
‭switch(day)‬
‭{‬
‭case 1: printf("monday");‬
‭7‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭ reak;‬
b
‭case 2:printf("tuesday");‬
‭break;‬
‭case 3: printf("wednesday");‬
‭break;‬
‭case 4:printf("thursday");‬
‭break;‬
‭case 5: printf("friday");‬
‭break;‬
‭case 6:printf("saturday");‬
‭break;‬
‭case 7: printf("sunday");‬
‭break;‬
‭default:printf("wrong input");‬
‭}‬
‭}‬

‭ utput:‬
O
‭Enter the day number from 1 to 7 : 6‬
‭saturday‬

‭ OTE:‬‭In‬‭switch‬‭case‬‭statements,‬‭if‬‭we‬‭do‬‭not‬‭associate‬‭a‬‭break‬‭statement‬‭with‬‭every‬‭case‬‭then‬‭from‬
N
‭the‬‭case‬‭for‬‭which‬‭the‬‭expression‬‭matches‬‭with‬‭the‬‭constant,‬‭all‬‭the‬‭statements‬‭are‬‭executed‬‭until‬‭the‬
‭switch case block ends. This situation is referred to as Fall Through.‬

‭6. Conditional operator:‬


‭It‬ ‭is‬ ‭also‬‭called‬‭as‬‭ternary‬‭operator‬‭and‬‭is‬‭used‬‭to‬‭perform‬‭simple‬‭conditional‬‭operations.‬‭It‬‭is‬
‭used to do operations similar to if-else statement.‬
‭The general syntax of conditional operator is as under:‬
‭Test expression? expression1:expression2;‬
‭Here firstly the test expression is evaluated.‬
‭If‬ ‭the‬ ‭test‬ ‭expression‬ ‭evaluates‬ ‭to‬‭true‬‭then‬‭the‬‭expression1‬‭is‬‭evaluated‬‭and‬‭it‬‭becomes‬‭the‬‭value‬‭of‬
‭the whole expression.‬
‭If‬‭the‬‭test‬‭expression‬‭evaluates‬‭to‬‭false‬‭then‬‭the‬‭expression2‬‭is‬‭evaluated‬‭and‬‭it‬‭becomes‬‭the‬‭value‬‭of‬
‭the whole expression.‬

‭For eg., a>b ? a : b‬


‭Here‬‭firstly‬‭the‬‭expression‬‭a>b‬‭is‬‭evaluated.‬‭If‬‭it‬‭evaluates‬‭to‬‭true‬‭then‬‭the‬‭value‬‭of‬‭a‬‭becomes‬
‭the value of the whole expression otherwise‬‭value‬‭of b‬‭becomes the value of the whole expression.‬

‭Iterations/Loops‬
‭Iterations‬‭or‬‭loops‬‭are‬‭used‬‭when‬‭we‬‭want‬‭to‬‭execute‬‭a‬‭statement‬‭or‬‭block‬‭of‬‭statements‬‭several‬
‭times.‬
‭The‬ ‭repetition‬ ‭of‬ ‭loops‬ ‭is‬ ‭controlled‬ ‭with‬ ‭the‬ ‭help‬ ‭of‬ ‭a‬ ‭test‬ ‭condition.‬ ‭The‬ ‭statements‬‭in‬‭the‬
l‭oop keep on executing repetitively until the test condition becomes false.‬
‭There are the following three types of loops:-‬

‭8‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭ . While loop‬
1
‭2. Do-while loop‬
‭3. For loop‬
‭ ll these loops are explained as under:‬
A

‭while loop :‬
I‭ t‬‭is‬‭the‬‭fundamental‬‭looping‬‭statement‬‭in‬‭C.‬‭It‬‭is‬‭suited‬‭for‬‭problems‬‭where‬‭it‬‭is‬‭not‬‭known‬‭in‬
‭advance how many times a statement or block of statements will be executed.‬
‭The general‬‭syntax‬‭of while loop is as under:‬
‭Initialization;‬
‭while(condition)‬
‭{‬
‭Statement(s)‬
‭Increment / Decrement;‬
‭}‬
‭We can able to understand the‬‭working of while loop‬‭with the help of flow chart below:‬

‭ .‬ F
1 ‭ irstly the‬‭condition‬‭given with‬‭while‬‭is evaluated.‬
‭2.‬ ‭If‬ ‭the‬ ‭condition‬ ‭evaluates‬ ‭to‬ ‭true‬ ‭then‬ ‭the‬ ‭statements‬ ‭given‬ ‭in‬ ‭the‬ ‭body‬ ‭of‬ ‭the‬ ‭loop‬ ‭are‬
‭executed.‬
‭3.‬ ‭After‬‭the‬‭execution‬‭of‬‭all‬‭the‬‭statements‬‭the‬‭condition‬‭is‬‭again‬‭checked‬‭and‬‭if‬‭it‬‭again‬‭evaluates‬
‭to true then the statements given in the body of the loop are again executed.‬
‭4.‬ ‭In‬‭this‬‭way‬‭the‬‭statements‬‭are‬‭executed‬‭again‬‭and‬‭again‬‭until‬‭the‬‭condition‬‭given‬‭evaluates‬‭to‬
‭false.‬

‭ or‬ ‭terminating‬ ‭the‬ ‭loop,‬ ‭a‬ ‭termination‬ ‭condition‬ ‭is‬ ‭given‬‭in‬‭the‬‭loop‬‭body‬‭which‬‭makes‬‭the‬


F
‭condition‬‭false‬‭at‬‭some‬‭point‬‭of‬‭time‬‭and‬‭as‬‭a‬‭result‬‭of‬‭which‬‭the‬‭loop‬‭terminates.‬‭If‬‭we‬‭do‬‭not‬‭give‬‭the‬
‭termination‬ ‭condition‬ ‭then‬ ‭the‬ ‭loop‬ ‭goes‬ ‭on‬‭repeating‬‭again‬‭and‬‭again‬‭infinite‬‭times.‬‭Such‬‭loops‬‭are‬
‭referred to as‬‭infinite loops.‬

1‭ . Example program for while loop‬


‭/*Program to print numbers 1 to 5 */‬
‭#include<stdio.h>‬
‭int main()‬
‭9‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭{‬
i‭nt i;‬
‭i=1;‬
‭while(i<=5)‬
‭{‬
‭printf("\n%d",i);‬
‭i++;‬
‭}‬
}‭ ‬
‭Output:‬
‭1‬
‭2‬
‭3‬
‭4‬
‭5‬

2‭ . Example program for while loop‬


‭/*program to find the sum of digits of the given number.*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int n,num,rem,sum=0;‬
‭printf(“Enter the number : ”);‬
‭scanf(“%d”,&num);‬
‭n=num;‬
‭while(num>0)‬
‭{‬
‭rem=num%10;‬
‭sum =sum+rem;‬
‭num=num/10;‬
‭}‬
‭printf(“The sum of the digits of %d is = %d”,n,sum);‬
‭}‬
‭Output:‬
‭Enter the number : 455‬
‭The sum of the digits of 455 is = 14‬

‭do-while loop :‬

‭ ‬ ‭The do-while statement is also used for looping.‬
‭➢‬ ‭It‬‭is‬‭similar‬‭to‬‭while‬‭loop‬‭and‬‭is‬‭used‬‭for‬‭problems‬‭where‬‭it‬‭is‬‭not‬‭known‬‭in‬‭advance‬‭how‬‭many‬
‭times the statement or block of statements will be executed.‬
‭➢‬ ‭However,‬‭unlike‬‭while‬‭loop,‬‭in‬‭case‬‭of‬‭do-while,‬‭firstly‬‭the‬‭statements‬‭inside‬‭the‬‭loop‬‭body‬‭are‬
‭executed and then the condition is evaluated.‬
‭➢‬ ‭As a result of which this loop is executed at least once even if the condition is initially false.‬
‭➢‬ ‭After that the loop is repeated until the condition evaluates to false.‬
‭➢‬ ‭Since‬‭in‬‭this‬‭loop‬‭the‬‭condition‬‭is‬‭tested‬‭after‬‭the‬‭execution‬‭of‬‭the‬‭loop,‬‭it‬‭is‬‭also‬‭known‬‭as‬‭the‬
‭post test loop.‬
‭10‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭ he general syntax of do-while loop is as follows:‬
T
‭Initialization;‬
‭do‬
‭{‬
‭Statement(s)‬
‭Increment / Decrement;‬
‭}while(condition);‬

‭The flow chart of‬‭do-while‬‭is given as under:‬

1‭ . Example program for‬‭do-while‬‭loop‬


‭/*Program to print numbers 4 to 1 */‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int i;‬
‭i=4;‬
‭do‬
‭{‬
‭printf("\n%d",i);‬
‭i--;‬
‭}while(i>=1);‬
‭}‬
‭Output:‬
‭4‬
‭3‬
‭2‬
‭1‬

2‭ . Example program for‬‭do-while‬‭loop‬


‭/*program to find the sum of digits of the given number.*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭11‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
i‭nt n,num,rem,sum=0;‬
‭printf(“Enter the number : ”);‬
‭scanf(“%d”,&num);‬
‭n=num;‬
‭do‬
‭{‬
‭rem=num%10;‬
‭sum =sum+rem;‬
‭num=num/10;‬
‭}while(num>0) ;‬
‭printf(“The sum of the digits of %d is = %d”,n,sum);‬
}‭ ‬
‭Output:‬
‭Enter the number : 567‬
‭The sum of the digits of 567 is = 18‬

‭Difference between while and do-while loop‬

‭for loop :‬
‭ he general syntax of for loop consists of three expressions separated by semicolons. It is‬
T
‭given as follows:-‬
‭12‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
f‭ or(initialization;termination condition;increment / decrement)‬
‭{‬
‭Statement(s);‬
‭}‬

‭1.‬ I‭ nitialization‬ ‭is‬ ‭executed‬ ‭only‬ ‭once‬ ‭when‬ ‭the‬ ‭loop‬ ‭starts‬ ‭and‬ ‭is‬ ‭used‬ ‭to‬ ‭initialize‬ ‭the‬ ‭loop‬
‭variables.‬
‭2.‬ ‭Termination condition‬‭is a condition and is tested‬‭before each iteration of the loop.‬
‭3.‬ ‭Increment‬ ‭/‬ ‭decrement‬ ‭is‬ ‭an‬ ‭update‬ ‭expression‬ ‭used‬ ‭to‬ ‭update‬ ‭the‬ ‭loop‬ ‭variable‬ ‭and‬ ‭is‬
‭executed each time after the body of the loop is executed.‬
‭ he flow chart of for loop is given as follows:‬
T

1‭ . Example program for‬‭for‬‭loop‬


‭// Program to print numbers 1 to 5‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int i;‬
‭for(i=1;i<=5;i++)‬
‭{‬
‭printf("\n%d",i);‬
‭}‬
‭}‬

‭ utput:‬
O
‭1‬
‭2‬
‭3‬
‭4‬
‭5‬

‭13‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
2‭ . Example program for‬‭for‬‭loop‬
‭/* program to generate the Fibonacci series */‬
‭/*‬ ‭The‬ ‭Fibonacci‬ ‭sequence‬ ‭is‬ ‭a‬ ‭sequence‬ ‭where‬ ‭the‬ ‭next‬‭term‬‭is‬‭the‬‭sum‬‭of‬‭the‬‭previous‬‭two‬‭terms.‬
‭The first two terms of the Fibonacci sequence are 0 followed by 1.*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int x,y,z;‬
‭int i,n;‬
‭x=0;‬
‭y=1;‬
‭printf("Enter the number of terms : ");‬
‭scanf("%d",&n);‬
‭printf("\n%d",x);‬
‭printf("\n%d",y);‬
‭for(i=1;i<n;i++)‬
‭{‬
‭z=x+y;‬
‭printf("\n%d",z);‬
‭x=y;‬
‭y=z;‬
‭}‬
‭}‬
‭Output:‬
‭Enter the number of terms : 5‬

0‭ ‬
‭1‬
‭1‬
‭2‬
‭3‬
‭5‬

‭●‬ A ‭ ll‬‭the‬‭three‬‭expressions‬‭in‬‭the‬‭for‬‭loop‬‭are‬‭optional‬‭and‬‭therefore‬‭we‬‭can‬‭omit‬‭any‬‭or‬‭all‬‭the‬
‭three expressions but in any case the two separating semi colons should always be present.‬
‭●‬ ‭If‬‭we‬‭omit‬‭the‬‭test‬‭expression‬‭then‬‭it‬‭is‬‭always‬‭assumed‬‭to‬‭be‬‭true‬‭and‬‭therefore‬‭the‬‭loop‬‭never‬
‭terminates and becomes an infinite loop.‬
‭●‬ ‭If‬‭we‬‭want‬‭to‬‭make‬‭such‬‭loops‬‭a‬‭finite‬‭loop‬‭then‬‭a‬‭separate‬‭terminate‬‭condition‬‭is‬‭given‬‭within‬
‭the loop body‬
‭Jump statements (or) Unconditional control transfer statements‬
J‭ ump statements are used to transfer the control from one part of the program to another part.‬
‭The various jump statements in C are as under:‬
‭1. break statement‬
‭2. continue statement‬
‭3. goto statement‬
‭1. break statement:‬

‭14‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭ he‬‭break‬‭statement is used inside the loops or switch statement. This statement causes‬
T
‭an immediate exit from the loop or the switch case block in which it appears.‬
‭It can be written as‬
‭break;‬

‭ xample program for‬‭break‬


E
‭/*Program to understand the use of break statement*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int n;‬
‭for(n=1;n<=5;n++)‬
‭{‬
‭if(n==3)‬
‭{‬
‭printf(“Loop is terminated using break statement \n”);‬
‭break;‬
‭}‬
‭printf(“Number = %d \n”,n);‬
‭}‬
‭printf(“Out of for loop \n”);‬
‭}‬
‭Output:‬
‭Number = 1‬
‭Number = 2‬
‭Loop is terminated using break statement‬
‭Outside of for loop‬

‭15‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭2. continue statement :‬
‭ he‬‭continue‬‭statement‬‭is‬‭used‬‭inside‬‭the‬‭body‬‭of‬‭the‬‭loop‬‭statement.‬‭It‬‭is‬‭used‬‭when‬‭we‬‭want‬
T
‭to go to the next iteration of the loop after skipping some of the statements of the loop.‬
‭The continue statement is written as under:‬
‭continue;‬

‭ ‬ I‭ t is generally used with a condition.‬



‭●‬ ‭When‬ ‭a‬ ‭continue‬ ‭statement‬ ‭is‬ ‭encountered‬ ‭all‬ ‭the‬ ‭remaining‬ ‭statements‬ ‭(statements‬ ‭after‬
‭continue)‬ ‭in‬ ‭the‬ ‭current‬ ‭iteration‬ ‭are‬ ‭not‬ ‭executed‬ ‭and‬ ‭the‬ ‭loop‬ ‭continues‬ ‭with‬ ‭the‬ ‭next‬
‭iteration.‬
‭●‬ ‭The‬ ‭difference‬ ‭between‬ ‭break‬ ‭and‬ ‭continue‬ ‭is‬‭that‬‭when‬‭a‬‭break‬‭statement‬‭is‬‭encountered‬
‭the‬‭loop‬‭terminates‬‭and‬‭the‬‭control‬‭is‬‭transferred‬‭to‬‭the‬‭next‬‭statement‬‭following‬‭the‬‭loop‬‭i.e.,‬
‭outside‬ ‭of‬ ‭for‬ ‭loop,‬ ‭but‬ ‭when‬ ‭a‬ ‭continue‬‭statement‬‭is‬‭encountered‬‭the‬‭loop‬‭is‬‭not‬‭terminated‬
‭and the control is transferred to the beginning of the loop.‬
‭●‬ ‭In‬ ‭while‬ ‭and‬ ‭do-while‬ ‭loops,‬ ‭after‬ ‭continue‬ ‭statement‬ ‭the‬ ‭control‬ ‭is‬ ‭transferred‬ ‭to‬ ‭the‬ ‭test‬
‭condition‬‭and‬‭then‬‭the‬‭loop‬‭continues,‬‭whereas‬‭in‬‭for‬‭loop‬‭after‬‭continue‬‭statement‬‭the‬‭control‬
‭is transferred to update expression and then the condition is tested.‬
‭Example program for‬‭continue‬
‭/*Program to understand the use of break statement*/‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int n;‬
‭for(n=1;n<=5;n++)‬
‭{‬
‭if(n==3)‬
‭{‬
‭printf(“Loop is continued with next iteration using continue statement \n”);‬
‭continue;‬
‭}‬
‭printf(“Number = %d \n”,n);‬
‭}‬
‭16‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
‭printf(“Out of for loop \n”);‬
}‭ ‬
‭Output:‬
‭Number = 1‬
‭Number = 2‬
‭Loop is continued with next iteration using continue statement‬
‭Number = 4‬
‭Number = 5‬
‭Outside of for loop‬

‭3. goto statement:‬


‭ he‬ ‭C‬ ‭goto‬ ‭statement‬ ‭is‬ ‭a‬ ‭jump‬ ‭statement‬ ‭which‬ ‭is‬ ‭sometimes‬ ‭also‬ ‭referred‬ ‭to‬ ‭as‬ ‭an‬
T
‭unconditional‬‭jump‬‭statement.‬‭The‬‭goto‬‭statement‬‭can‬‭be‬‭used‬‭to‬‭jump‬‭from‬‭anywhere‬‭to‬‭anywhere‬
‭within a function.‬
‭Syntax 1:‬

‭ oto label;‬
g
‭...................‬
‭...................‬
‭label:‬
‭....................‬
‭....................‬
‭ yntax 2:‬
S

l‭abel:‬
‭...................‬
‭...................‬
‭goto label;‬
‭....................‬
‭....................‬


‭ ‬ ‭Here label is any valid C identifier and is followed by colon.‬
‭➔‬ ‭The goto statement transfers the control to the statement after the label.‬
‭➔‬ ‭If‬ ‭the‬ ‭label‬ ‭is‬ ‭placed‬ ‭after‬ ‭the‬ ‭goto‬ ‭statement‬‭then‬‭the‬‭control‬‭is‬‭transferred‬‭forward‬‭and‬‭it‬‭is‬
‭known‬ ‭as‬ ‭forward‬ ‭jump.‬ ‭If‬ ‭the‬ ‭label‬ ‭is‬ ‭placed‬ ‭before‬ ‭the‬ ‭goto‬ ‭statement‬ ‭then‬ ‭the‬ ‭control‬ ‭is‬
‭transferred backward and the jump is called backward jump.‬
‭➔‬ ‭In the forward jump all the statements between the goto statement and the label are skipped.‬
‭➔‬ ‭In‬ ‭case‬ ‭of‬ ‭backward‬ ‭jump‬ ‭all‬ ‭the‬ ‭statement‬ ‭between‬ ‭the‬ ‭goto‬ ‭statement‬ ‭and‬ ‭the‬ ‭label‬ ‭are‬
‭executed.‬
‭➔‬ ‭There should always be a statement after any label.‬
‭Example program for‬‭goto‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a;‬
‭printf("\nEnter a value : ");‬

‭17‬
‭ITP Notes for I B.Tech CSM - A, SVCET, Chittoor‬
s‭ canf("%d",&a);‬
‭printf("Hello");‬
‭if(a == 0){‬
‭goto label;‬
‭}‬
‭printf("World");‬
‭label:printf("Exit");‬
}‭ ‬
‭Output 1:‬
‭Enter a value : 9‬
‭HelloWorldExit‬
‭Output 2:‬
‭Enter a value : 0‬
‭HelloExit‬

‭18‬

You might also like