GANDHINAGAR INSTITUTE OF TECHNOLOGY SUBJECT : COMPUTER PROGRAMMING AND UTILIZATION(2110003) TOPIC : “Control Structures in C .” Guided by : Prof. Archana Singh MADE BY:-JAYDEV PATEL(150120119127) KRUNAL PATEL(150120119128) JAIMIN PATEL(150120119126)
CONTENT • Introduction • Decision Making Statement i. If else statement ii. Ladder if else iii. Switch statement • Looping i. While loop ii. do-while loop iii. For loop iv. Nesting loop v. Flow breaking statement
Introduction • The important and essential part of a programming language is their control structures. 1. Sequence 2. Decision 3. Looping
Decision making statement • This is same as choosing one of the alternatives out of the two alternatives available. • There are various types of decision making statements:- i. If else statement ii. Ladder if-else iii. Switch iv. goto
If-else statement • The syntax of if else statement is: if(condition) statement 1; else statement 2;
Flow chart of if else statement
If-else statement • Here the condition is represented by relational or logical expression. • If condition is true, then statement 1 is executed and if condition is false statement 2 is executed. • A compound statement must be enclosedin pair of braces{}. • Enclosing in a single statement is not compulsory. • The else part is is optional. The if statement without else looks like:- if(condition) statement;
Write a program to print minimum of two integers. #include<stdio.h> Void main() { Int x,y,min; Printf(“enter two integers”); Scanf(“%d %d”,&x,&y); If(x<y) min=x; Else min=y; Printf(“minimum is:%dn”,min); } • Output:- enter two integers 10 6 Minimum is :6
Ladder if-else • The if else is used for two way decision where we select one of the alternative. • But for more than that c supports more multiple if-else or ladder if else for this purpose. • The syntax of multiple if else is:- • If(condition 1) statement1; else if(condition2) statement2; . . else if(conditionN) statementN; else default_statement;
Ladder if else • The condition1,condition2,…,conditionN are represented in logical or relational expression s . • if the first condition is true than statement1 will be executed or the other conditions will be evaluated till the correct condition is found. • If the true condition is not found than at last the default statement will be executed.
Write a program to compute the electricity bill. #include<stdio.h> Void main() { Int units; Float ucharge,fcharge,gtax,meter_rent= 25.0,total; Printf(“enter number of units consumed”); Scanf(“%d”,units); If(units<=50) Ucharge=0.5*units; Else if(units<=100) Ucharge=50*0.5+(units-50)*0.75; Else if(units<=200) Ucharge=50*0.5+50*0.75+(units-100)*1.00; Else Ucharge=50*0.5+50*0.75+100*1.00+(units-200)*1.50; Fcharge=0.40*ucharge; Gtax=0.10*(ucharge+fcharge); Total=ucharge+fcharge+gtax+meter_rent; Printf(“total bill: %fn”,total); }
Output Enter the value of units consumed 85 Total bill : 103.925003
Switch statement • The switch in C is a multi-choice statement. • It provides one choice for each value of variable expression. • The syntax of switch is given below: Switch(variable) { Case label1: statement_block1; break; Case label2: statement_block2; break; . . default: default_block
Switch statement • Lebel1 ,label2,…. Show the possible value of the variable or expression. • After every statement block there is a break. • Break sends control to the next switch statement. • The last choice is default which is chosen when the value of expression does not match to any of the expression. • But default is optional.
Write a program to perform arithmetic operations. #include<stdio.h> #include<conio.h> Void main() { Int choice,a,b; Clrscr(); Printf(“1. add n”); Printf(“2. subn”); Printf(“3. muln”); Printf(“4. divn”); Printf(“Enter your choice:”); Scanf(“%d”,&choice); Printf(“enter two numbers”); Scanf(“%d %d “,&a,&b); Switch(choice) { Case 1: printf(“answer=%dn”,a+b); break; Case 2: printf(“answer=%dn”,a-b); break; Case 3: printf(“answer=%dn”,a*b); break; Case4 : printf(“answer=%dn”a/b); break; Default: break; } }
Output 1. Add 2. Sub 3. Mul 4. Div Enter your choice :1 Enter two numbers 23 35 Answer =58
Goto statement • The goto statement in C programming is used to transfer the control unconditionally from one part of program to another. • The goto statement can be dangerous to programming languages. • The syntax is Goto label;
Write a program to print the sum of 1 to N using goto statement. #include<stdio.h> Void main() { Int i=1,sum=0,n; Printf(“Enter value of N”); Scanf(“%d”,&n); next: Sum= sum+I; i+ =1; If(I <= n ) Goto next; Printf(“sum = %dn”,sum); }
Output Enter the value of N 4 Sum = 10
Looping • A looping is an important control structure in higher level programming language. • The objective of looping is to provide repetition of the part of the program i.e. block of statements in a program, number of times.
Types of loop • There are two types of loop: 1. Entry control 2. Exit control • In entry control loop the condition is checked first and then the body is executed, while in exit control the body is excuted first and then the condition is checked.
While loop • The syntax of while loop is: While(condition) { body; }
While loop • Here the condition is evaluated first and then the body is executed. After executing the body the condition is evaluated again and if it is true body is executed again. This process is repeated as long as the condition is true. • While loop is a entry control loop. • It is not necessary to enclose body of loop in pair of {} if body contains single statement.
Write program to compute the sum of following series. 1²+2²+…+10² #include<stdio.h> Void main() { Int sum=0,n=1; While(n<=10) { sum = = n*n; n++; } Printf(“sum of series :%dn”,sum); }
Output Sum of series : 385
Do-while loop • The syntax of do-while loop is: do{ Body; } while (condition);
Do-while loop • In do-while first the body is executed and then the condition is checked. If the condition is true the body is again executed. The body is executed as long as the condition is true. • Do-while loop is a exit control loop. • This ensures the body is atleast once executed even if the condition is false.
For loop • The syntax of for loop is : For(<e1>;<e2>;<e3>) { body; }
For loop • <e1> is initialization of variable by initial values.<e1> executes only once when we enter in loop. If they are more than one they should be separated by commas. • <e2> is the condition and the if it is true, then the control enters into the body of the loop and executes the statement in body. • After executing the body of the loop, <e3> is executed which modifies the variable.
Write a program to print the multiplication table for a given number N. #include<stdio.h> Void main() { Int I,n; Printf(“enter a number”); Scanf(“%d”,&n); Printf(“Table->n”); For(i=1,1<=10;i++) Printf(“%4d*%2d=%4dn’,n,I,n*i); }
Output Enter a number 3 Table-> 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
Nesting of loops • Loop inside loop is called nested loops. For example, … … For( ) { ….. for( ) { ……. …….. } ……. } ……. …….
Nesting of loops
Write a program to print the triangle.for n=5 #include<stdio.h> Void main() { Int n,l,j; Printf(“enter number of lines”); Scanf(“%d”,&n); Printf(“triangle ->n”); For(l=1;l<=n;l++) {for(j=1;j<=1;j++) Printf(“%d”,j); Printf(“n”); } }
Output Enter number of lines 5 Triangle-> 1 12 123 1234 12345
Thank you

computer programming and utilization

  • 1.
    GANDHINAGAR INSTITUTE OF TECHNOLOGY SUBJECT: COMPUTER PROGRAMMING AND UTILIZATION(2110003) TOPIC : “Control Structures in C .” Guided by : Prof. Archana Singh MADE BY:-JAYDEV PATEL(150120119127) KRUNAL PATEL(150120119128) JAIMIN PATEL(150120119126)
  • 2.
    CONTENT • Introduction • DecisionMaking Statement i. If else statement ii. Ladder if else iii. Switch statement • Looping i. While loop ii. do-while loop iii. For loop iv. Nesting loop v. Flow breaking statement
  • 3.
    Introduction • The importantand essential part of a programming language is their control structures. 1. Sequence 2. Decision 3. Looping
  • 4.
    Decision making statement •This is same as choosing one of the alternatives out of the two alternatives available. • There are various types of decision making statements:- i. If else statement ii. Ladder if-else iii. Switch iv. goto
  • 5.
    If-else statement • Thesyntax of if else statement is: if(condition) statement 1; else statement 2;
  • 6.
    Flow chart ofif else statement
  • 7.
    If-else statement • Herethe condition is represented by relational or logical expression. • If condition is true, then statement 1 is executed and if condition is false statement 2 is executed. • A compound statement must be enclosedin pair of braces{}. • Enclosing in a single statement is not compulsory. • The else part is is optional. The if statement without else looks like:- if(condition) statement;
  • 8.
    Write a programto print minimum of two integers. #include<stdio.h> Void main() { Int x,y,min; Printf(“enter two integers”); Scanf(“%d %d”,&x,&y); If(x<y) min=x; Else min=y; Printf(“minimum is:%dn”,min); } • Output:- enter two integers 10 6 Minimum is :6
  • 9.
    Ladder if-else • Theif else is used for two way decision where we select one of the alternative. • But for more than that c supports more multiple if-else or ladder if else for this purpose. • The syntax of multiple if else is:- • If(condition 1) statement1; else if(condition2) statement2; . . else if(conditionN) statementN; else default_statement;
  • 11.
    Ladder if else •The condition1,condition2,…,conditionN are represented in logical or relational expression s . • if the first condition is true than statement1 will be executed or the other conditions will be evaluated till the correct condition is found. • If the true condition is not found than at last the default statement will be executed.
  • 12.
    Write a programto compute the electricity bill. #include<stdio.h> Void main() { Int units; Float ucharge,fcharge,gtax,meter_rent= 25.0,total; Printf(“enter number of units consumed”); Scanf(“%d”,units); If(units<=50) Ucharge=0.5*units; Else if(units<=100) Ucharge=50*0.5+(units-50)*0.75; Else if(units<=200) Ucharge=50*0.5+50*0.75+(units-100)*1.00; Else Ucharge=50*0.5+50*0.75+100*1.00+(units-200)*1.50; Fcharge=0.40*ucharge; Gtax=0.10*(ucharge+fcharge); Total=ucharge+fcharge+gtax+meter_rent; Printf(“total bill: %fn”,total); }
  • 13.
    Output Enter the valueof units consumed 85 Total bill : 103.925003
  • 14.
    Switch statement • Theswitch in C is a multi-choice statement. • It provides one choice for each value of variable expression. • The syntax of switch is given below: Switch(variable) { Case label1: statement_block1; break; Case label2: statement_block2; break; . . default: default_block
  • 16.
    Switch statement • Lebel1,label2,…. Show the possible value of the variable or expression. • After every statement block there is a break. • Break sends control to the next switch statement. • The last choice is default which is chosen when the value of expression does not match to any of the expression. • But default is optional.
  • 17.
    Write a programto perform arithmetic operations. #include<stdio.h> #include<conio.h> Void main() { Int choice,a,b; Clrscr(); Printf(“1. add n”); Printf(“2. subn”); Printf(“3. muln”); Printf(“4. divn”); Printf(“Enter your choice:”); Scanf(“%d”,&choice); Printf(“enter two numbers”); Scanf(“%d %d “,&a,&b); Switch(choice) { Case 1: printf(“answer=%dn”,a+b); break; Case 2: printf(“answer=%dn”,a-b); break; Case 3: printf(“answer=%dn”,a*b); break; Case4 : printf(“answer=%dn”a/b); break; Default: break; } }
  • 18.
    Output 1. Add 2. Sub 3.Mul 4. Div Enter your choice :1 Enter two numbers 23 35 Answer =58
  • 19.
    Goto statement • Thegoto statement in C programming is used to transfer the control unconditionally from one part of program to another. • The goto statement can be dangerous to programming languages. • The syntax is Goto label;
  • 20.
    Write a programto print the sum of 1 to N using goto statement. #include<stdio.h> Void main() { Int i=1,sum=0,n; Printf(“Enter value of N”); Scanf(“%d”,&n); next: Sum= sum+I; i+ =1; If(I <= n ) Goto next; Printf(“sum = %dn”,sum); }
  • 21.
    Output Enter the valueof N 4 Sum = 10
  • 22.
    Looping • A loopingis an important control structure in higher level programming language. • The objective of looping is to provide repetition of the part of the program i.e. block of statements in a program, number of times.
  • 23.
    Types of loop •There are two types of loop: 1. Entry control 2. Exit control • In entry control loop the condition is checked first and then the body is executed, while in exit control the body is excuted first and then the condition is checked.
  • 25.
    While loop • Thesyntax of while loop is: While(condition) { body; }
  • 27.
    While loop • Herethe condition is evaluated first and then the body is executed. After executing the body the condition is evaluated again and if it is true body is executed again. This process is repeated as long as the condition is true. • While loop is a entry control loop. • It is not necessary to enclose body of loop in pair of {} if body contains single statement.
  • 28.
    Write program tocompute the sum of following series. 1²+2²+…+10² #include<stdio.h> Void main() { Int sum=0,n=1; While(n<=10) { sum = = n*n; n++; } Printf(“sum of series :%dn”,sum); }
  • 29.
  • 30.
    Do-while loop • Thesyntax of do-while loop is: do{ Body; } while (condition);
  • 32.
    Do-while loop • Indo-while first the body is executed and then the condition is checked. If the condition is true the body is again executed. The body is executed as long as the condition is true. • Do-while loop is a exit control loop. • This ensures the body is atleast once executed even if the condition is false.
  • 33.
    For loop • Thesyntax of for loop is : For(<e1>;<e2>;<e3>) { body; }
  • 35.
    For loop • <e1>is initialization of variable by initial values.<e1> executes only once when we enter in loop. If they are more than one they should be separated by commas. • <e2> is the condition and the if it is true, then the control enters into the body of the loop and executes the statement in body. • After executing the body of the loop, <e3> is executed which modifies the variable.
  • 36.
    Write a programto print the multiplication table for a given number N. #include<stdio.h> Void main() { Int I,n; Printf(“enter a number”); Scanf(“%d”,&n); Printf(“Table->n”); For(i=1,1<=10;i++) Printf(“%4d*%2d=%4dn’,n,I,n*i); }
  • 37.
  • 38.
    Nesting of loops •Loop inside loop is called nested loops. For example, … … For( ) { ….. for( ) { ……. …….. } ……. } ……. …….
  • 39.
  • 40.
    Write a programto print the triangle.for n=5 #include<stdio.h> Void main() { Int n,l,j; Printf(“enter number of lines”); Scanf(“%d”,&n); Printf(“triangle ->n”); For(l=1;l<=n;l++) {for(j=1;j<=1;j++) Printf(“%d”,j); Printf(“n”); } }
  • 41.
    Output Enter number oflines 5 Triangle-> 1 12 123 1234 12345
  • 42.