Lecture 2.1.1 Looping statements.ppt

Full Transcript

UNIVERSITY INSTITUTE OF ENGINEERING DEPARTMENT- ACADEMIC UNITS FIRST YEAR ENGINEERING PROGRAMMES Subject Name : Introduction to Problem Solving Subject Code: 24CSH-101 Loop Control structure in C DISCOVER. LEARN. EMPOWER 2 Introduction to...

UNIVERSITY INSTITUTE OF ENGINEERING DEPARTMENT- ACADEMIC UNITS FIRST YEAR ENGINEERING PROGRAMMES Subject Name : Introduction to Problem Solving Subject Code: 24CSH-101 Loop Control structure in C DISCOVER. LEARN. EMPOWER 2 Introduction to Problem Solving Course Objectives The course aims to provide exposure to problem-solving through programming. The course aims to raise the programming skills of students via logic building capability. With knowledge of C programming language, students would be able to model real world problems. Course Outcomes CO Course Outcome Number CO1 Remember the concepts related to fundamentals of C language, draw flowcharts and write algorithm/pseudocode. CO2 Understand the way of execution and debug programs in C language. CO3 Apply various constructs, loops, functions to solve mathematical and scientific problem. CO4 Analyze the dynamic behavior of memory by the use of pointers. CO5 Design and develop modular programs for real world problems using control structure and selection structure. 3 4 ASSESSMENT PATTERN The performance of students is evaluated as follows: Theory Practical Continuous Internal Semester End Continuous Internal Semester End Components Assessment (CAE) Examination (SEE) Assessment (CAE) Examination (SEE) Marks 40 60 60 40 Total Marks 100 100 Contents 5 Looping Statements I/O Statements 6 Types of I/OLooping types statements Entry controlled loop or Pre-Test Loop: In the entry controlled loop or Pre-Test loop, the condition is checked before we start and at the beginning of each iteration of the loop. If the condition is true, we execute body of loop; if the control expression is false, we terminate the loop. Examples: while statement and for statement Exit controlled loop or Post-Test Loop: In the exit controlled loop or Post-Test loop, the condition is checked after or at the end of each iteration of the loop. If the condition is true, we repeat body of loop; if the condition is false, we terminate the loop. Examples: do…while statement 7 Loops Definition & Syntax Statements Definition & Syntax It is a most basic loop in C programming. It has one control condition, and executes as long the condition is true. The condition of the loop is tested before the body of the loop is executed. Syntax: while while (condition) { statement(s); Incrementation; } C for loops is very similar to a while loops in that it continues to process a block of code until a statement becomes false, and everything is defined in a single line. The for loop is also entry -controlled loop. Syntax: for for ( init; condition; increment ) { statement(s); } C do while loops are very similar to the while loops, but it always executes the code block at least once and furthermore as long as the condition remains true. Syntax: do { Do while statement(s); }while( condition ); 8 9 While loop Working: step1: The loop variable is initialized with some value and then it has been tested for the condition. step2: If the condition returns true then the statements inside the body of while loop are executed else control comes out of the loop. step3: The value of loop variable is incremented/decremented then it has been tested again for the loop condition. 1 0 Example 1 Example 1 Program to print first 10 natural numbers. #include void main( ) { int x; x = 1; while(x =0); } 1 4 For loop Flowchart of continue statement Working: Step 1: First initialization happens and the counter variable gets initialized. Step 2: In the second step the condition is checked, where the counter variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed, if the condition returns false then the for loop gets terminated and the control comes out of the loop. Step 3: After successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –) 1 5 Example Program to print factorial of a number #include #include void main() { int fact, i, n; fact = 1; printf("Enter the number\t"); scanf("%d" , &n); for(i = 1; i

Use Quizgecko on...
Browser
Browser