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

Slides:



Advertisements
Similar presentations
Unit 2 The if-else-if LECTURE – 14
Advertisements

CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
CLASS XI By Sumita Arora
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Programs Day 1 and 2. Write a Program to: 1: Read in two numbers and output the largest 2: Read in an exam mark and output the appropriate grade according.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 4 Program Control Statements
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Control Statements (Decision Making)
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Controlling Execution Dong Shao, Nanjing Unviersity.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Making and Branching
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
UNIT 3 Conditional Program Execution: Applying if and switch statements, nesting if and else, restrictions on switch values, use of break and default with.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CSC COMPUTER EDUCATION, M.K.B.NAGAR CHAPTER 3. CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Objectives Explain If, If..else statements Understand Looping.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Control Structures (Repetition structure) Jump Statements
Control Structures Introduction
‘C’ Programming Khalid Jamal.
Chapter 6: Loops.
Decisions Chapter 4.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
C Program Controls + Flow Structure
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements.
Control Structures.
INC 161 , CPE 100 Computer Programming
IF if (condition) { Process… }
Chapter 6 Decision Making and Looping
Loops in C.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Lab5 PROGRAMMING 1 Loop chapter4.
PROGRAM FLOWCHART Iteration Statements.
Decision making and control functions
Department of Computer Science
CSC215 Lecture Control Flow.
Presentation transcript:

Asstt. Prof Mandeep Kaur Computer Dept

TOPIC: Control Statements in C language

 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

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;

//Program to implement if statement #include 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(); }

2. The if-else statement: It is a two way branching statement Explanation: 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); }

//Program to implement if else statement #include 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(); }

3.Nested if statements: When an if statement appers within anoter if statement, the statement is called nested if statement. Syntax If If(expression-2) Else If(expression-3) Else If

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(); }

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;

//Program to show use of if-else-if ladder #include 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(); }

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;

//Program to implement switch statement #include 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("Algebric Operations\n"); printf("1. summation\n"); printf("2. difference\n"); printf("3. Product\n"); printf("4. Diviosion\n"); printf("Enter the no as per required\n"); scanf("%d",&x); switch(x) {

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(); }

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++.

Following are the loops used in C Language: for loop syntax for(expression-1; expression-2;expression-3) statement; while loop syntax expression-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.

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.

//Program showing use of for loop #include 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(); }

//Program showing use of while loop #include void main() { clrscr(); int n=1; while(n<=10) { printf("\n%d",n); n++; } getch(); }

//Program showing use of do while loop #include void main() { clrscr(); int n=1; do { printf("\n%d",n); n++; } while(n<=10); getch(); }

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. Syntax break; 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.

// 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); }

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. Syntax continue;

// 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); }

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. Syntax goto 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

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; }

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”); }