DECISION CONTROL AND LOOPING STATEMENTS

Similar documents
Module 4: Decision-making and forming loops

Prepared by: Shraddha Modi

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Decision Making -Branching. Class Incharge: S. Sasirekha

Introduction. C provides two styles of flow control:

CHAPTER : 9 FLOW OF CONTROL

Introduction to Programming

Subject: PIC Chapter 2.

CHAPTER 9 FLOW OF CONTROL

Decision Making and Loops

Loops / Repetition Statements

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Computer Programming. Decision Making (2) Loops

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Unit 3 Decision making, Looping and Arrays

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

UIC. C Programming Primer. Bharathidasan University

Flow Control. CSC215 Lecture

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to Computing Lecture 05: Selection (continued)

Lecture 10. Daily Puzzle

4.1. Structured program development Overview of C language

CHAPTER 5 FLOW OF CONTROL

Decision making with if Statement : - Control Statements. Introduction: -

CS1100 Introduction to Programming

Government Polytechnic Muzaffarpur.

Laboratory 4. INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Chapter 4 C Program Control

Loops / Repetition Statements

Chapter 4. Flow of Control

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Control Structure: Loop

Computers Programming Course 7. Iulian Năstac

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

n Group of statements that are executed repeatedly while some condition remains true

A control structure refers to the way in which the Programmer specifies the order of executing the statements

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

More examples for Control statements

SELECTION STATEMENTS:

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

10/9/2012. Comparison and Logical Operators The if Statement The if else Statement Nested if Statements The switch case. switch case Statement

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

C - Basic Introduction

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

MODULE 2: Branching and Looping

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

REPETITION CONTROL STRUCTURE LOGO

AMCAT Automata Coding Sample Questions And Answers

Chapter 5: Control Structures

C 101. Davide Vernizzi. April 29, 2011

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Control Structure: Selection

Chapter 4 C Program Control

Unit 7. Functions. Need of User Defined Functions

Dept. of CSE, IIT KGP

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

6. Control Statements II

Comments. Comments: /* This is a comment */

Repetition and Loop Statements Chapter 5

CSE 130 Introduction to Programming in C Control Flow Revisited

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

BRANCHING if-else statements

1.4 Control Structures: Selection. Department of CSE

COMP 208 Computers in Engineering

Lecture 6. Statements

Relational & Logical Operators, if and switch Statements

Chapter 3: Arrays and More C Functionality

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

8. Control statements

Chapter 3 Structured Program Development

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

3 The L oop Control Structure

Repetition Structures II

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

C++ PROGRAMMING SKILLS Part 2 Programming Structures

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

SFU CMPT Topic: Control Statements

Chapter 21: Introduction to C Programming Language

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Fundamentals of Computer Programming Using C

Programming for Experimental Research. Flow Control

Programming Fundamentals

Conditional Control Structures. Dr.T.Logeswari

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Transcription:

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not. These decision control statements include: If statement If else statement else if statement Switch statement

IF STATEMENT If statement is the simplest form of decision control statements that is frequently used in decision making. The general form of a simple if statement is shown in the figure. First the test expression is evaluated. If the test expression is true, the statement of if block (statement 1 to n) are executed otherwise these statements will be skipped and the execution will jump to statement x. SYNTAX OF IF STATEMENT if (test expression) statement 1;... statement n; statement x; Test Expression TRUE Statement Block 1 Statement x FALSE

IF ELSE STATEMENT In the if-else construct, first the test expression is evaluated. If the expression is true, statement block 1 is executed and statement block 2 is skipped. Otherwise, if the expression is false, statement block 2 is executed and statement block 1 is ignored. In any case after the statement block 1 or 2 gets executed the control will pass to statement x. Therefore, statement x is executed in every case. SYNTAX OF IF STATEMENT if (test expression) statement_block 1; else statement_block 2; statement x; TRUE Statement Block 1 Test Expression FALSE Statement Block 2 Statement x

PROGRAMS TO DEMONSTRATE THE USE OF IF AND IF-ELSE STATEMENTS // Program to demonstrate the use of if-statement #include<stdio.h> int main() int x=10; if ( x>0) x++; printf("\n x = %d", x); return 0; // PROGRAM TO FIND WHETHER A NUMBER IS EVEN OR ODD #include<stdio.h> main() int a; printf("\n Enter the value of a : "); scanf("%d", &a); if(a%2==0) printf("\n %d is even", a); else printf("\n %d is odd", a); return 0;

ELSE IF STATEMENT C language supports if else if statements to test additional conditions apart from the initial test expression. The if-else-if construct works in the same way as a normal if statement. SYNTAX OF IF-ELSE STATEMENT if ( test expression 1) statement block 1; else if ( test expression 2) statement block 2;... else if (test expression N) statement block N; else Statement Block X; Statement Y; TRUE Statement Block 1 Test Expression 1 TRUE Statement Block 2 FALSE Test Expression 2 Statement Y FALSE Statement Block X

SWITCH CASE A switch case statement is a multi-way decision statement. Switch statements are used: When there is only one variable to evaluate in the expression When many conditions are being tested for Switch case statement advantages include: Easy to debug, read, understand and maintain Execute faster than its equivalent if-else construct switch(grade) case 'A': printf("\n Excellent"); break; case 'B': printf("\n Good"); break; case 'C': printf("\n Fair"); break; default: printf("\n Invalid Grade"); break;

// PROGRAM TO CLASSIFY A NUMBER AS POSITIVE, NEGATIVE OR ZERO #include<stdio.h> main() int num; printf("\n Enter any number : "); scanf("%d", &num); if(num==0) printf("\n The value is equal to zero"); else if(num>0) printf("\n The number is positive"); else printf("\n The number is negative"); return 0; // PROGRAM TO PRINT THE DAY OF THE WEEK #include<stdio.h> int main() int day; printf( \n Enter any number from 1 to 7 : ); scanf( %d,&day); switch(day) case 1: printf( \n SUNDAY ); break; case 2 : printf( \n MONDAY ); break; case 3 : printf( \n TUESDAY ); break; case 4 : printf( \n WEDNESDAY ); break; case 5 : printf( \n THURSDAY ); break; case 6 : printf( \n FRIDAY ); break; case 7 : printf( \n SATURDAY ); break; default: printf( \n Wrong Number ); return 0;

ITERATIVE STATEMENTS Iterative statements are used to repeat the execution of a list of statements, depending on the value of an integer expression. In this section, we will discuss all these statements. While loop Do-while loop For loop WHILE LOOP\ENTRY LOOP The while loop is used to repeat one or more statements while a particular condition is true. In the while loop, the condition is tested before any of the statements in the statement block is executed. If the condition is true, only then the statements will be executed otherwise the control will jump to the immediate statement outside the while loop block. We must constantly update the condition of the while loop. Statement x while (condition) statement_block; statement x; Update the condition expression Statement Block TRUE Conditio n FALSE Statement y

DO WHILE LOOP\EXIT LOOP The do-while loop is similar to the while loop. The only difference is that in a do-while loop, the test condition is tested at the end of the loop. The body of the loop gets executed at least one time (even if the condition is false). The do while loop continues to execute whilst a condition is true. There is no choice whether to execute the loop or not. Hence, entry in the loop is automatic there is only a choice to continue it further or not. The major disadvantage of using a do while loop is that it always executes at least once, so even if the user enters some invalid data, the loop will execute. Do-while loops are widely used to print a list of options for a menu driven program. Statement x Statement x; do statement_block; while (condition); statement y; Statement Block Update the condition expression TRUE Condition FALSE Statement y

Program to demonstrate the use of while loop and do while loop // PROGRAM TO PRINT NUMBERS FROM 0 TO 10 USING WHILE LOOP #include<stdio.h> int main() int i = 0; while(i<=10) printf( \n %d, i); i = i + 1; return 0; // condition updated // PROGRAM TO PRINT NUMBERS FROM 0-10 USING DO-WHILE LOOP #include<stdio.h> int main() int i = 0; do printf( \n %d, i); i = i + 1; while(i<=10); return 0;

FOR LOOP For loop is used to repeat a task until a particular condition is true. The syntax of a for loop for (initialization; condition; increment/decrement/update) statement block; Statement Y; When a for loop is used, the loop variable is initialized only once. With every iteration of the loop, the value of the loop variable is updated and the condition is checked. If the condition is true, the statement block of the loop is executed else, the statements comprising the statement block of the for loop are skipped and the control jumps to the immediate statement following the for loop body. Updating the loop variable may include incrementing the loop variable, decrementing the loop variable or setting it to some other value like, i +=2, where i is the loop variable.

PROGRAM FOR FOR-LOOP Look at the code given below which print first n numbers using a for loop. #include<stdio.h> int main() int i, n; printf( \n Enter the value of n : ); scanf( %d, &n); for(i=0; i<= n; i++) return 0; printf( \n %d, i);

BREAK STATEMENT The break statement is used to terminate the execution of the nearest enclosing loop in which it appears. When compiler encounters a break statement, the control passes to the statement that follows the loop in which the break statement appears. Its syntax is quite simple, just type keyword break followed with a semi-colon. break; In switch statement if the break statement is missing then every case from the matched case label to the end of the switch, including the default, is executed. CONTINUE STATEMENT The continue statement can only appear in the body of a loop. When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop. Its syntax is quite simple, just type keyword continue followed with a semi-colon. continue; If placed within a for loop, the continue statement causes a branch to the code that updates the loop variable. For example, int i; for(i=0; i<= 10; i++) if (i==5) continue; printf( \t %d, i);

break Statement The break statement can be used in while, do-while, and for loops to exit from the loop.

Example break in a for Loop #include <stdio.h> int main ( ) int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) break ; printf ( %d, i) ; printf ( \nbroke out of loop at i = %d.\n, i) ; return 0 ; OUTPUT: 1 2 3 4 Broke out of loop at i = 5.

The continue Statement The continue statement can be used in while, do-while, and for loops. It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.

Example continue in a for Loop #include <stdio.h> int main ( ) int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) continue ; printf ( %d, i) ; printf ( \ndone.\n ) ; return 0 ; OUTPUT: 1 2 3 4 6 7 8 9

Nested Loops Loops may be nested (embedded) inside of each other. Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure. It is common to see nested for loops.

Nested Loop 1. int i,j; 2. for(i=0;i<5;i++) 3. 4. for(j=0;j<i;j++) 5. 6. printf("*"); 7. 8. printf("\n"); 9. * ** *** **** *****