asstt. prof mandeep kaur computer dept. topic: control statements in c language

27
PRESENTED BY Asstt. Prof Mandeep Kaur Computer Dept

Upload: beverly-rice

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

 Branching(Selective)Statements The if statement The else-if statement The else-if construct The nested if statement The switch statement  Looping Statements The for statement The while statement The do-while statement  Jumping(Breaking control)Statements The break statement The continue statement The goto statement

TRANSCRIPT

Page 1: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

PRESENTED BY

Asstt. Prof Mandeep KaurComputer Dept

Page 2: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

TOPIC: Control Statements in C language

Page 3: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

Control statements are used to change the order of execution of the statements. These are divided into three parts as :

Branching(Selective)Statements The if statement The else-if statement The else-if construct The nested if statement The switch statementLooping Statements The for statement The while statement The do-while statementJumping(Breaking control)Statements The break statement The continue statement The goto statement

Page 4: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

Decision Statements in C

The statements used to alter the flow of the program are known as decision statements.

These are used to check that whether the particular statement is true or not. Following is the list of decision statements often used:

1. The if Statement:   Explanation: if the test expression is true, then statement-block will

execute,otherwise it will jump to statement-x.

Syntax of if Statement:if (test expression) { statement –block; } statement-x;

Page 5: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program to implement if statement#include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,big; printf("Enter two numbers\n"); scanf("%d%d",&a,&b); big=a;if(big>b)big=b; printf("%d is greater",big); getch(); } 

Page 6: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

2. The if-else statement: It is a two way branching statementExplanation: if the test condition is true, then true-block

statements will execute otherwise false-block statements will execute.

Syntax for if-else statement if (test expression){ True-block statement (s); } else { False-block statement(s); }

Page 7: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program to implement if else statement #include<stdio.h> #include<conio.h> void main() { clrscr(); int year; printf("Enter year\n"); scanf("%d",&year); if(year%4==0) printf("leap year"); else printf("not a leap year"); getch(); }

Page 8: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

3.Nested if statements:

When an if statement appers within anoter if statement, the statement is called nested if statement.

SyntaxIf<expression-1>If(expression-2)<Statement-set-1>Else<Statement-set-3>If(expression-3)<Statement-set-3>ElseIf<Statement-set-4>

Page 9: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

Program to implement the use of nested if statement. Void main() {

int a,b,c; printf(“Enter three integer numbers”); scanf(“%d%d%d”,&a,&b,&c); if(a>b) { If(a>c) printf(“|nGreatest number=%d”,a); else printf(“|nGreatest number=%d”,c); } else { If(b>c) printf(“|nGreatest number=%d”,b); else printf(“|nGreatest number=%d”,c); } getch(); }

Page 10: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

4. The if-else-if ladder Explanation: as soon as the true condition is found the statement associated with it is executed and the control is transferred to statement-x. When all the n conditions become false the default statement will be executed.

if(condition 1) statement -1; else if(condition 2) statement -2; else if (condition 3) statement-3; ……………… ……………………………… ……………… else if(condition n) statement-n; else default-statement;

Page 11: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program to show use of if-else-if ladder #include<stdio.h> #include<conio.h> void main() { clrscr(); int day; printf("Enter number between 1-7\n"); scanf("%d",&day); if(day==1) printf("Monday"); else if(day==2) printf("Tuesday"); else if(day==3) printf("Wednesday"); else if(day==4) printf("Thursday"); else if(day==5) printf("Friday"); else if(day==6) printf("Saturday"); else if(day==7) printf("Sunday"); else printf("Enter correct no"); getch(); }

Page 12: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

4. The switch statement Syntax switch(expression) { case 1: statement -1; break; case 2: statement -2; break; case 3: statement -3; break; ….….. …………case n: statement -n; break; default: statement; } statement-x;

Page 13: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program to implement switch statement #include<stdio.h> #include<conio.h> void main() { clrscr(); int x; float a,b; printf("Enter first no\n"); scanf("%f",&a); printf("Enter second no\n"); scanf("%f",&b); printf("@@@@@@@@@@@@@@@@@@@@@@@\n");

printf("Algebric Operations\n"); printf("@@@@@@@@@@@@@@@@@@@@@@@\n");

printf("1. summation\n"); printf("2. difference\n"); printf("3. Product\n"); printf("4. Diviosion\n"); printf("@@@@@@@@@@@@@@@@@@@@@@@\n"); printf("Enter the no as per required\n");

printf("@@@@@@@@@@@@@@@@@@@@@@@\n"); scanf("%d",&x); switch(x) {

Page 14: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

case 1: printf("Sum=%.2f",a+b); break; case 2: printf("Differene=%.2f",a-b); break; case 3: printf("Product=%.2f",a*b); break; case 4: printf("Division=%.2f",a/b); break; default: printf("Enter correct no"); break; } getch(); }

Page 15: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

Loop Control Statements in C

A Loop is a block of statements which are repeatedly executed for certain number of times. Steps in Loop

 a) Expression 1: Initialization e.g. if i is any variable then

in this we have to start the variable. Let us say i=1  b) Expression 2: Test Condition in this the condition is

checked again and again. And if this condition satisfies then it will execute further otherwise not. Let us say i<=5.

 c) Expression 3: Increment or Decrement in this the

value is either increased or decreased in each step. Let us say i++.

Page 16: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

Following are the loops used in C Language: for loop syntax for(expression-1; expression-2;expression-3) statement;

while loop syntaxexpression-1; while(expression-2) { statement; expression-3; } 2.The condition is tested before executing the body of the

loop. 3.Body of while loop is executed only if the condition is true. 

Page 17: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

do- while expression-1; do { statement; expression-3; } while(expression-2);2.The condition is tested after executing the body of the

loop.3.Body of the do loop is executed at least once even if the

condition is false.

Page 18: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program showing use of for loop

#include<stdio.h> #include<conio.h> void main() { int x,i; clrscr(); printf("Enter any natural no\n"); scanf("%d",&x); for(i=1;i<=x;i++) printf("I=%d\n",i); getch(); }

Page 19: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program showing use of while loop

#include<stdio.h> #include<conio.h> void main() { clrscr(); int n=1; while(n<=10) { printf("\n%d",n); n++; } getch(); }

Page 20: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

//Program showing use of do while loop

#include<stdio.h> #include<conio.h> void main() { clrscr(); int n=1; do { printf("\n%d",n); n++; } while(n<=10); getch(); }

Page 21: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

Jumping Statements:

break statement:-The break statement is used to terminate loop or to exit from switch case structure. The break can be used within a for loop, a while loop, a do-while loop or a switch statement to transfer the control out of the block/loop. The statements enclosed in a pair of braces is called a block.

Syntaxbreak;

when break statement is encountered within the loop/block the control the control is transferred to block/statement immediately following loop/block, that is, loop is terminated.

Page 22: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

// Program showing use of break statement.

Void main() { int i,j; clrscr(); for(i=1;i<=2;i++) { for(j=1;j<=2;j++) {

if(i==j) break; printf(“\n%d %d\n”i,j); } } }

Page 23: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

The continue statement

The continue statement can be used within a while , a do-while or a for statement. The continue statement transfers the control to the beginning of the loop, for next iteration, after by passing the statements which one yet executed.

Syntaxcontinue;

Page 24: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

// Program showing use of continue statement.

Void main() { int i,j; clrscr(); for(i=1;i<=2;i++) { for(j=1;j<=2;j++) {

if(i==j) continue; printf(“\n%d %d\n”i,j); } } }

Page 25: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

The goto statement

The goto statement is used to alter the normal executin of program execution by transferring control to and other point of the program.

Syntaxgoto label;

where label is an identifier used to label the target statemnt to which control will be transferred. The target statement must be labelled and the lebel must be followed by a colon.

label : statement

Page 26: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

a)unconditional goto statement:The unconditional goto statement is used to transfer the control from one part of the program to the other part without checking any condition.Normally prefered to use the unconditional goto statement in program as it may lead to a very complicated problem like an infinite loop.

  // Program showing use of unconditional goto statement.Void main(){start:printf(“I am doing MCA”);goto start;}

Page 27: Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language

b)conditional goto statement: The conditional goto statement is used to transferthe control of the execution from one part of the program to the other. This transfer of control is based on certain condition.

// Program showing use of conditional goto statement. Void main() { int n=10; clrscr(); loop: printf(“%d”,n); n--; if(n>0) { goto loop; printf(“FIREEEEEE”); } }