Two-way selection. Branching and Looping

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Two-way selection. Branching and Looping"

Transcription

1 Control Structures: are those statements that decide the order in which individual statements or instructions of a program are executed or evaluated. Control Structures are broadly classified into: 1. Conditional statements: Specifies a condition to execute a group of statements in a program. The control structures, if, if-else, nested if, if else ladder and switch, belongs to this category. 2. Iterative statements: Executes one or more statements repeatedly until a condition is met. The control structures: while, do while, and for loops, belong to this category. 3. Jump statements: Transfer of control from one part to another within the program. Ex. goto, break, continue and return. Two-way selection The basic decision statement in the computer is the two-way selection. The decision is described to the computer as a conditional statement that can be answered either true or false. If the answer is true, one or more action statements are executed. If the answer is false, then a different action or set of actions is executed. Regardless of which set of action is executed, the program continues with the next statement after the selection. The flow chart for the two-way decision logic is shown in Fig.A false Decision statement true false action true action Fig.A. Two-way decision logic Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 1

2 if statements The if statements executes a simple or compound statements, depending on whether or not an expression is true of false. The syntax to use if statements is: if(conditional expression) statements; //simple statement if(conditional expression) //compound statements statement 1; statement 1; statement 1; In the preceding syntaxes, a simple or compound statement (block of statements) is executed when the condition expression given in the if statement it turn out to be true. Otherwise, the program control passes to next statement. If the condition expression is false then the C compiler does not do anything. Note that the condition expression given in parenthesis, must be evaluated as true(non-zero value) or false(zero values). In addition, a compound statement must be provided by opening and closing braces. Examples: if(7) // a non zero value returns true if(0) //zero value returns false if(i==0) //True if i=0 other wise False if(i=0) //false because value of the expression is zero. *Note:- For programming examples, please refer text books. if-else statement The if-else statement executes a simple or compound statement when the conditional expression provided in the if statement is true. It executes another simple or compound statement, followed by the else statement, when the conditional expression is false. Syntax: if(conditional expression) simple or compound statement 1 else simple or compound statement 2 *Note:- For programming examples, please refer text books. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 2

3 Nested if statements (Multi-way selection) For the if-else, the statements may be any statement, including, another if-else. When an ifelse is included within an if-else, it is known as a nested if statement. The control of a program moves into the inner if statements when the outer if statement is evaluated to be true. Syntax: if(expression 1) if(expression 2) Statement 1 else Statement2 else Statement 3 Statement3 Expr1 Statement2 Expr2 Statement1 (a) code (b)logic Flow Syntax: if(conditional expression1) //Statement 1 to be executed in case the conditional expression1 is true if(conditional expression2) // Statement 2 to be executed in case the conditional expression2 is true if-else ladder(multi-way selection) When more than one if-else statements are used in a sequence, it is called as if-else ladder. The syntax is: if(conditional expression1) simple or compound statement else if(conditional expression2) simple or compound statement else if(conditional expression3) Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 3

4 simple or compound statement. else if(conditional expression n) simple or compound statement *Note:- For programming examples, please refer text books. Switch statements (Multi-way selection) A switch statement is a conditional statement that tests a value against different values. If the value is matched, the corresponding group of statements is executed. A switch statement begins with the switch keyword that followed by a value expression in the parenthesis( ). It is a combination of multiple case labels that must be separated by the break statement. Every case label contains a constant value that is matched against the value, which is specified in the switch expression. If the value is matched, the statements of that case label are executed. In addition, we can specify the default label, which is executed when the value specified in the switch expression, does not match with the given case labels. The syntax is: switch(expression) case value 1: statements break; case value 2: statements break; sase value 3: statements break; default: default statements break; *Note:- For programming examples, refer text books and class notes. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 4

5 Conditional operator (?:) or Ternary operator: is used to check a condition and select a value depending on the value of the condition. The selected value will be assigned to a variable which has the following form:- variable = (condition)? value1 : value2; Ex:- big = (a > b)? a : b; Repetition Concept of a loop The concept of a loop is shown in the flow chart in Fig.1. In this flow chart, the action is repeated over and over again. It never stops. Since the loop in fig. never stops, the actions will be repeated forever. We want the loop to end when the work is done. To make sure it ends, we must have a condition that controls the loop. In other words, we must desing the loop so that before or after each iteration, it checks to see if it is done. If it is not done, it repeats one more time; if it is done, it exits the loop. This test is known as a loop control expression. An action or a series of actions Fig.1. The concept of loop. Pretest and Post-Test loops Programming languages allow us to check the loop control expressions either before or after each iteration of the loop. In other words, we can have either a pre- or a posttest terminating condition. In a pretest loop, the condition is checked before we start and at the beginning of each iteration after the first. If the test condition is true, we execute the code; if the test condition is false, we terminate the loop. Examples: for loop, while loop. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 5

6 In a post-test loop, we always execute the code at least once. At the completion of the loop code, the loop control expression is tested. If the expression is true, the loop repeats; if the expressions is false, the loop terminates. The flowcharts in Fig.2. shows these two loop types. Example: do while Condition False An action or series of actions True An action or series of actions True Condition False a. Pretest Loop b. Post-test loop Fig.2. Pretest and post-test loops Pretest Loop Executions Initialization: 1 Number of tests: n+1 Minimum iterations: 0 n is the number of iterations. Post-test Loop Executions Initialization: 1 Number of tests: n Minimum iterations: 1 Table 1. Loop comparision Loops in C C has three loop statements: the while, the for, and the do while. The first two are pretest loops, and the do while is a post-test loop. We can use all of them for eventcontrolled and counter-controlled loops. Fig.6. shows these loop constructs. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 6

7 Loop statements while for do while Pretest loop Pretest loop post-test loop Fig.6. C loop constructs 1.The while loop The while statement is the pretest loop. It uses an expression to control the loop. Since it is a pretest loop, it tests the expressions before every iterations of the loop. The basic syntax of the while statement is shown in the fig.7. expression s While (expression) statement statement (a)flowchart (b)sample Code Fig.7. The while statement Note that the sample code in Fig.7 shows that the loop body is a single statement; that is, the body of the loop must be one, and only one, statement. If we want to include multiple statements in the body, we must put them in a compound statement(block). This concept is shown in the Fig.8. below. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 7

8 expression False While(expression). /*end while*/ Exit (a)flowchart (b) C language Fig.8. Compound while statement *Note:- For programming examples, please refer text books. 2.The for loop The for statement is a pretest loop that uses three expressions. The first expression contains any initialization statements, the second contains the limit-test expression, and the third contains the updating expression. Fig.9 shows the flow chart and an expanded interpretation, for a sample for statement. 1. Expression 1 is executed when the for starts. 2. Expression 2 is the limit test expression. As shown in the expanded flowchart, it is executed before every iteration. Since the for is a pretest loop, the body is not executed if the limit condition is false at the start of the loop. 3. Expression 3 is the update expressions. This means that you cannot use statements, such as return, in the for statement itself. 4. Like the while statements, the for statements does not need a semicolon. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 8

9 expr3 expr1 Effd f df expr2 f False expr1 expr2 False True True statement statement expr3 (a)flowchart (b) Expanded Flowchart Fig.9. for statement for(expr1;expr2;expr3) for(expr1;expr2;expr3) (a)simple for statement (b)compound for statement *Note:- For programming examples, please refer text books. Nested for loops Any statement, even another for loop, can be included in the body of a for statement. In other words, a for loop inside for loop is called nested for. For(expr1;expr2;expr3) //Outer for loop for(expr1;expr2;expr3) //Inner for loop statements; //end of inner for //end of outer for *Note:- For programming examples, please refer text books. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 9

10 2.1 Variations in for loop 1.In for loop, the expression i<=n can be replaced by the expression i<n+1. For example, consider for(i=1; i<=5 ; i++) Printf( %d, i); //Output: //can also be written as for(i=0 ; i<6 ; i++) Printf( %,i); //Output: The initialization expression can be moved just before the loop and updation expression can be shifted to end of body loop. For ex. for(i=1; i<=5 ; i++) i=1 Printf( %d, i); is same as for( ; i<=5 ; ) Printf( %d, i); i++; 3. For loop should not end with semicolon. For(i=1; i<=5 ; i++) Printf( %d, i) ; Observe that semicolon in the body of the loop is treated as NULL statement. So, for all the values of i=1,2,3,4,5 the NULL statement is executed which does nothing. Finally, i will be 6 and control comes out of for loop. 4. The initialization expression expr1, limit test expression expr2 and update expression expr3 are optional in the for loop. For ex. for( ; ;) printf( \n ); Whenever limit test expression is not there in the for loop, it is treated as infinite loop. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 10

11 3. The do while statement The do while statement is a post-test loop. Like the while and for loops, it also uses an expression to control the loop, but it tests this expression after the execution of the body. The format of the do.while statement is shown in Fig.10. statement do statement expression while ( expression ); true false do expression true false (a)flowchart while (expression ); (b)sample Code Comparison of Loop Control Structures for loop while loop do while loop 1.A for loop is used to execute and repeat a statement block depending on a condition at the beginning of the loop. Example for(i=1; i<=10; i++) A while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of the loop. Example i=1; A do while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the end of the loop. Example i=1; Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 11

12 s=s+i; p=p*i; 2. A variable value is initialized at the beginning of the loop and is used in the condition. 3. A statement to change the value of the condition or to increment the value of the variable is given at the beginning of the loop. 4.The statement block will not be executed when the value of the condition is false. 5.A for loop is commonly used by many programmers. while(i<=10) s=s+i; p=p*i; i++; A variable value is initialized at the beginning or before the loop and is used in the condition. A statement to change the value of the condition or to increment the value of the variable is given at the inside of the loop. The statement block will not be executed when the value of the condition is false. A while loop is also widely used by many programmers. do s=s+i; p=p*i; i++; while(i<=10); A variable value is initialized before the loop or assigned inside the loop and is used in the condition. A statement to change the value of the condition or to increment the value of the variable is given at the inside of the loop. The statement block will not be executed when the value of the condition is false, but the block is executed at least once irrespective of the value of the condition. A do while loop is used in some cases where the condition need to be checked at the end of the loop. Jump statements Jump statements are the statements that transfer control from one part of the program to another. The jump statements supported by C are follows: break statements continue statements goto statements return statements break statement/ Jump statements related to looping The break statement is used to break any type of loop as well as switch statement. Breaking a loop means terminating the loop. It has the following form: Example: break; Printf( Press B to break, any other key to continue ); for(i=1 ; i<=80 ; i++) Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 12

13 ch = getche(); if (ch == B ) break; control is transferred to the end of block continue statement/ Jump statements related to looping The continue statement is used to transfer the control to the beginning of a statement block in a loop. In other words, a break statement breaks the entire loop, but a continue statement breaks the current iteration. That is, the continue statement breaks the current execution of a loop condition and then continue the loop with next condition. If has the following form: Example: continue; for(i=1 ; i<=80 ; i++) ch = getche(); if (ch == C ch == c ) control is transferred to the beginning of the block. printf( C for continue is pressed ); continue; goto statement The goto statement is an unconditional transfer of control statement. It is used to transfer the control from one part of the program to another. The place to which the control is transferred is identified by a statement label. It has the following form: goto label; where label is the statement label which is available anywhere in the program. Example: ; goto display; ; ; display; ; Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 13

14 exit( ) Function The exit( ) function is used to transfer the control to the end of a program (i.e to terminate the program execution). It uses one argument in ( ) and the value is zero for normal termination or non-zero for abnormal termination. For example. if (n<0) printf( Factorial in not available for negative numbers ); exit(0); Note that the program execution is terminated when the value of the variable n is negative. The compiler directive #include<stdlib> is used when this function is used in a program. return: It is used to return the control to the calling function with/without a value. For example, if a function is not returning any value, use the return keyword If a function is returning a value, then return; return value; Question Bank 1. What is a two way selection statement? Explain with an example. 2. With a suitable example program, demonstrate the working of if-else statement 3. What are multi-way selection/decision making statements? Explain them 4. What is nested if? Explain with a suitable program. 5. What is if-else ladder? Explain with a suitable program. 6. What is a switch? With syntax explain with an example program. 7. What is a loop? How loops are classified in C? 8. Explain the working of for, while and do_while loops with syntax and suitable example programs. 9. What is the difference between while and do_while? 10. Differentiate between for, while and do_while? 11. What are pre-test loops and post-test loops? Explain with examples. 12. What are jump statements? Explain the different types of jump statements available in C. 13. Explain the following jump statements. i.goto ii.break iii.continue. 14. Where and why break and continue statements are used in C? 15. What are Ternary operators? Explain them. Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 14

15 Sunil Kumar B, Dept of CSE, NCET, Bengaluru. 15

14 MAKING DECISIONS IN C

14 MAKING DECISIONS IN C 14 MAKING DECISIONS IN C 14.1 INTRODUCTION So far we have seen that in C programs the instructions are executed in the same order in which they appear in the program. Each instruction is executed once

More information

Lecture 2 Notes: Flow of Control

Lecture 2 Notes: Flow of Control 6.096 Introduction to C++ January, 2011 Massachusetts Institute of Technology John Marrero Lecture 2 Notes: Flow of Control 1 Motivation Normally, a program executes statements from first to last. The

More information

6. Control Structures

6. Control Structures - 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,

More information

Chapter 8 Statement-Level Control Structures Chapter 8 Topics

Chapter 8 Statement-Level Control Structures Chapter 8 Topics Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Chapter 8 Statement-Level Control Structures Introduction A

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 3 Control Structures 1 Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring

More information

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements 9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending

More information

Conditional and Looping Construct

Conditional and Looping Construct Chapter 3 Conditional and Looping Construct After studying this lesson, students will be able to: Understand the concept and usage of selection and iteration statements. Know various types of loops available

More information

MIT Aurangabad FE Computer Engineering

MIT Aurangabad FE Computer Engineering MIT Aurangabad FE Computer Engineering Unit 1: Introduction to C 1. The symbol # is called a. Header file c. include b. Preprocessor d. semicolon 2. The size of integer number is limited to a. -32768 to

More information

NRI Institute of Technology Principles of Programming Languages III B.Tech I Sem (R09)-CSE

NRI Institute of Technology Principles of Programming Languages III B.Tech I Sem (R09)-CSE Unit-4 Expressions and Assignment Statements Introduction Expressions are the fundamental means of specifying computations in a programming language. To understand expression evaluation, need to be familiar

More information

Statement-Level Control Structures

Statement-Level Control Structures Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Levels of Control Flow Within expressions Among program units Among program statements

More information

Senem Kumova Metin & Ilker Korkmaz 1

Senem Kumova Metin & Ilker Korkmaz 1 Senem Kumova Metin & Ilker Korkmaz 1 A loop is a block of code that can be performed repeatedly. A loop is controlled by a condition that is checked each time through the loop. C supports two categories

More information

CONTROL STRUCTURES AND STATEMENTS IN C AND CPP

CONTROL STRUCTURES AND STATEMENTS IN C AND CPP CONTROL STRUCTURES AND STATEMENTS IN C AND CPP Control structures form the basic entities of a structured programming language. We all know languages like C/C++ or Java are all structured programming languages.

More information

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

More information

COMPSCI 210 Part II Decisions & Iterations

COMPSCI 210 Part II Decisions & Iterations Agenda COMPSCI 210 Part II Decisions & Iterations Based on slides @ McGraw-Hill Agenda: Decision-making statements: he if statement he switch statement Iterations he for statement he while statement he

More information

Indicates the end of data when the number of data is not known in advance Must be distinct from the data but be of the same type

Indicates the end of data when the number of data is not known in advance Must be distinct from the data but be of the same type Program Control Essentials of repetition Loop control variable Counts the number of repetitions in the counter-controlled loop Controls the execution of loop Sentinel value Indicates the end of data when

More information

Control Structure. Pseudocode: Input number If number is even Then Print even Else print odd. Flowchart: Begin. End. Print Odd. number even?

Control Structure. Pseudocode: Input number If number is even Then Print even Else print odd. Flowchart: Begin. End. Print Odd. number even? Control Structure Normally, a program is executed in a sequential manner.however, in many cases, a program has to choose among alternative statements C++ provides constructs that enable the programmer

More information

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void 1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

More information

A relational operator is used to make comparison between two expressions. All these operators are binary and require two operands.

A relational operator is used to make comparison between two expressions. All these operators are binary and require two operands. Control Structures in C++ Index Control Structures in C++ 3.1 Relational Operators A relational operator is used to make comparison between two expressions. All these operators are binary and require two

More information

Iterative Statements. 1. How is iteration controlled? 2. Where is the control mechanism in the loop? Logical expression or counter.

Iterative Statements. 1. How is iteration controlled? 2. Where is the control mechanism in the loop? Logical expression or counter. Iterative Statements The repeated execution of a statement or compound statement is accomplished either by iteration or recursion General design issues for iteration control statements: 1. How is iteration

More information

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions 8-2 1 Levels of Control Flow Within

More information

C Control Structures. Chapter 13

C Control Structures. Chapter 13 C Control Structures Chapter 13 Cyrus Bazeghi Conditional Control Structures making a decision about which code to execute, based on evaluated expression if Iterative if-else switch executing code multiple

More information

Chapter 8. Statement-Level Control Structures ISBN

Chapter 8. Statement-Level Control Structures ISBN Chapter 8 Statement-Level Control Structures ISBN 0-321-33025-0 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions Copyright 2006

More information

6.087 Lecture 3 January 13, 2010

6.087 Lecture 3 January 13, 2010 6.087 Lecture 3 January 13, 2010 Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables 1 Review:

More information

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1 QUIZ-II Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For

More information

Model Paper Computer Science Objective. Paper Code Time Allowed: 20 minutes

Model Paper Computer Science Objective. Paper Code Time Allowed: 20 minutes Note: This is Model Paper for guidance of students & teachers. Q. Model Paper Computer Science Objective Intermediate Part II ( th Class) Examination Session -4 and onward Total marks: 7 Paper Code Time

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 14, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

UNIT-1. C Programming & Data Structures. Introduction to Computers: Computing Environment: Types of Computing Environments:

UNIT-1. C Programming & Data Structures. Introduction to Computers: Computing Environment: Types of Computing Environments: Introduction to Computers: C Programming & Data Structures UNIT-1 A computer system consists of hardware and software. Computer hardware is the collection of physical elements that comprise a computer

More information

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. The Need For Repetition (Loops) Writing out a simple counting program (1

More information

Problem Solving With C++

Problem Solving With C++ Dept. of Geomatics Eng. University of Tehran Cell: 0912 132 5823 Email: marajabi@ut.ac.ir http://www.marajabi.com Unfinished Business Casts (force an expression to be of a specific type) #include

More information

C AND C++ PROGRAMMING

C AND C++ PROGRAMMING C AND C++ PROGRAMMING Bharathidasan University A Courseware prepared by University Informatics Centre Part I - Programming in C Getting Started This courseware is intended to be an introduction to C programming

More information

Outline. Repetition (Loops) Types of Loops. Repetitions with Loops. Terminating Loops. PreTest vs. PostTest Loops. II.

Outline. Repetition (Loops) Types of Loops. Repetitions with Loops. Terminating Loops. PreTest vs. PostTest Loops. II. Repetition (Loops) Outline Want to do some repetitive sequence of actions: print vertical line of s Corresponding program: printf( \n ); printf( \n ); printf( \n ); printf( \n ); printf( \n ); II. Program

More information

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be... What is a Loop? CSC Intermediate Programming Looping A loop is a repetition control structure It causes a single statement or a group of statements to be executed repeatedly It uses a condition to control

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Tutorial No. 5 - Solution (Decision Making and Branching)

Tutorial No. 5 - Solution (Decision Making and Branching) Tutorial No. 5 - Solution (Decision Making and Branching) 1. Explain if...if ladder with flowchart [7] The if ladder is a way of putting together ifs together when multipath decisions are involved. A multipath

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

Chapter 4 C Program Control

Chapter 4 C Program Control Chapter 4 C Program Control Objectives of this chapter: Repetitions will be considered in greater detail for.. repetition do while repetition Also multiple selection switch case statement will be learned.

More information

Introduction to Programming in C++: Algorithms, Flowcharts and Pseudocode

Introduction to Programming in C++: Algorithms, Flowcharts and Pseudocode Introduction to Programming in C++: Algorithms, Flowcharts and Pseudocode by Brent Daviduck The following material was developed by Brent Daviduck of the Computer Systems Technology program at Red Deer

More information

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero THEN the Average is Sum divided by Count Conditions

More information

6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011 6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

More information

JavaScript: Control Statements I

JavaScript: Control Statements I 1 7 JavaScript: Control Statements I 7.1 Introduction 2 The techniques you will learn here are applicable to most high-level languages, including JavaScript 1 7.2 Algorithms 3 Any computable problem can

More information

INTRODUCTION TO FLOWCHARTING

INTRODUCTION TO FLOWCHARTING CHAPTER 1 INTRODUCTION TO FLOWCHARTING 1.0 Objectives 1.1 Introduction 1.2 Flowcharts 1.3 Types of Flowcharts 1.3.1 Types of flowchart 1.3.2 System flowcharts 1.4 Flowchart Symbols 1.5 Advantages of Flowcharts

More information

Software II: Principles of Programming Languages

Software II: Principles of Programming Languages Software II: Principles of Programming Languages Lecture 8 Statement-Level Control Structures Control Statements: Evolution FORTRAN I control statements were based directly on IBM 704 hardware Much research

More information

Organization of Programming Languages CS3200 / 5200N. Lecture 08

Organization of Programming Languages CS3200 / 5200N. Lecture 08 Organization of Programming Languages CS3200 / 5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Control Flow Control flow = the flow of control, or execution

More information

switch Multiple-Selection Statement

switch Multiple-Selection Statement switch Multiple-Selection Statement (This feature is rarely useful, although it s perfect for programming the iterative song The Twelve Days of Christmas!) If no match occurs, the default case is executed,

More information

Mathematica Programming

Mathematica Programming program.nb Mathematica Programming Dennis Silverman Mathematical Physics B U. C. Irvine Built in Programming Mathematica already has several important built over and above standard programming languages.

More information

5 Let s all move one place on. Lewis Carroll

5 Let s all move one place on. Lewis Carroll Control Statements: Part 2, Solutions 5 Let s all move one place on Lewis Carroll The wheel is come full circle William Shakespeare How many apples fell on Newton s head before he took the hint! Robert

More information

1. A(n) structure is a logical design that controls the order in which a set of statements execute. a. function b. control c. sequence d.

1. A(n) structure is a logical design that controls the order in which a set of statements execute. a. function b. control c. sequence d. Chapter Four MULTIPLE CHOICE 1. A(n) structure is a logical design that controls the order in which a set of statements execute. a. function b. control c. sequence d. iteration 2. The decision structure

More information

FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I:

FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I: FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The following statement is legal: cout >> Hello, my name is

More information

Selection Statements

Selection Statements Chapter 5 Selection Statements 1 Statements So far, we ve used return statements and expression ess statements. e ts. Most of C s remaining statements fall into three categories: Selection statements:

More information

Branching. We ll continue to look at the basics of writing C++ programs including. Overview

Branching. We ll continue to look at the basics of writing C++ programs including. Overview Branching Overview We ll continue to look at the basics of writing C++ programs including Boolean Expressions (need to know this before we can using branching and looping!) Branching J.S. Bradbury CSCI

More information

Fundamentals of Python: First Programs. Chapter 3: Control Statements modifications by Mr. Dave Clausen

Fundamentals of Python: First Programs. Chapter 3: Control Statements modifications by Mr. Dave Clausen Fundamentals of Python: First Programs Chapter 3: Control Statements modifications by Mr. Dave Clausen Objectives After completing this chapter, you will be able to: Write a loop to repeat a sequence of

More information

UEE1302 (1102) F10 Introduction to Computers and Programming

UEE1302 (1102) F10 Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming Programming Lecture 03 Flow of Control (Part II): Repetition while,for & do..while Learning

More information

BHARATHIAR UNIVERSITY: COIMBATORE CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++ LANGUAGE

BHARATHIAR UNIVERSITY: COIMBATORE CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++ LANGUAGE Certificate in Advanced Programming - C++ Language Page 1 of 7 BHARATHIAR UNIVERSITY: COIMBATORE 641046 CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++

More information

cin >> num1 >> num2 >> num3 >> num4 >> num5; sum = num1 + num2 + num3 + num4 + num5; average = sum / 5.

cin >> num1 >> num2 >> num3 >> num4 >> num5; sum = num1 + num2 + num3 + num4 + num5; average = sum / 5. Lesson Outcomes At the end of this chapter, student should be able to: Understand the requirement of a loop Understand the Loop Control Variable (LCV) Use increment (++) and decrement (--) operators Program

More information

Chapter 5 Looping. At a Glance. Overview. Objectives. Quick Quizzes. Additional Resources. Key Terms

Chapter 5 Looping. At a Glance. Overview. Objectives. Quick Quizzes. Additional Resources. Key Terms Chapter 5 Looping At a Glance Overview Objectives Quick Quizzes Additional Resources Key Terms Lecture Notes Overview Chapter 5 covers loops in detail. Students learn the advantages of looping. They also

More information

Looping Exercises Deciding Which Loop to Use

Looping Exercises Deciding Which Loop to Use Looping Exercises Deciding Which Loop to Use At this time you have learned four looping constructs (also called repetition statements). Each of these four loops are ideal to use in different situations.

More information

INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

More information

GTU Questions. Computer Programming & Utilization. Chief Course Coordinator Prof. Mitul K.Patel. (Head of Department & Assistant Professor)

GTU Questions. Computer Programming & Utilization. Chief Course Coordinator Prof. Mitul K.Patel. (Head of Department & Assistant Professor) GTU Questions Computer Programming & Utilization Chief Course Coordinator Prof. Mitul K.Patel (Head of Department & Assistant Professor) Course Coordinator Prof. Vrutti D. Shah (Assistant Professor) Lab

More information

VB.NET Programming Fundamentals

VB.NET Programming Fundamentals Chapter 3 Objectives Programming Fundamentals In this chapter, you will: Learn about the programming language Write a module definition Use variables and data types Compute with Write decision-making statements

More information

ALGORITHMS AND FLOWCHARTS

ALGORITHMS AND FLOWCHARTS ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problem this sequence of steps

More information

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANKULATHUR 603 203 DEPARTMENT OF COMPUTER APPLICATIONS QUESTION BANK IN REVISED BLOOM S TAXONOMY

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANKULATHUR 603 203 DEPARTMENT OF COMPUTER APPLICATIONS QUESTION BANK IN REVISED BLOOM S TAXONOMY ACADEMIC YEAR: 0 7 VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANKULATHUR 0 0 SEMESTER: ODD BRANCH: MCA YEAR: I SEMESTER: I SUBJECT CODE AND NAME: MC70 Problem Solving and Programming NAME OF THE FACULTY

More information

While Loop. 6. Iteration

While Loop. 6. Iteration While Loop 1 Loop - a control structure that causes a set of statements to be executed repeatedly, (reiterated). While statement - most versatile type of loop in C++ false while boolean expression true

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Common to: CSE 2 nd sem IT 2 nd sem Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart

More information

GETTING STARTED WITH C++ C++ BASICS - 1 -

GETTING STARTED WITH C++ C++ BASICS - 1 - - 1 - GETTING STARTED WITH C++ Programming is a core activity in the process of performing tasks or solving problems with the aid of a computer. An idealised picture is: PROBLEM COMPUTER SOLUTION Unfortunately

More information

I PUC - Computer Science. Practical s Syllabus. Contents

I PUC - Computer Science. Practical s Syllabus. Contents I PUC - Computer Science Practical s Syllabus Contents Topics 1 Overview Of a Computer 1.1 Introduction 1.2 Functional Components of a computer (Working of each unit) 1.3 Evolution Of Computers 1.4 Generations

More information

Chapter 6 Repetition

Chapter 6 Repetition Chapter 6 Repetition 6-1 Repetition in C++ The final control/logic structure is repetition: Repetition repeating a block of code until a condition is met There are three repetition statements available

More information

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. ==

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. == Group 1 Question 1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value. Question 2. What is the only

More information

Reading Assignment Control Flow Statements switch while for break continue Scopes of Declaration Scopes and Visibility Good Style: smallest scope

Reading Assignment Control Flow Statements switch while for break continue Scopes of Declaration Scopes and Visibility Good Style: smallest scope Reading Assignment Control Flow Statements Scopes of Declaration K.N. King, Chapter 8 Assert function Grouping: and Decision making: if, switch Loop building: while, do, for Loop ending: break, continue

More information

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem

More information

C-Sheep (v0.2) Reference 2006 Eike Anderson & Leigh McLoughlin

C-Sheep (v0.2) Reference 2006 Eike Anderson & Leigh McLoughlin The C-Sheep Programming Language Introduction This text is a reference for the C-Sheep programming language. C-Sheep is a member of the family of mini-languages 1 (toy languages) used for teaching programming

More information

Going from Python to C

Going from Python to C Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

More information

Fundamentals of Programming and Software Development Lesson Objectives

Fundamentals of Programming and Software Development Lesson Objectives Lesson Unit 1: INTRODUCTION TO COMPUTERS Computer History Create a timeline illustrating the most significant contributions to computing technology Describe the history and evolution of the computer Identify

More information

Thinking in C. Darin Brezeale. March 25, 2010

Thinking in C. Darin Brezeale. March 25, 2010 Thinking in C Darin Brezeale March 25, 2010 NOTE: This is definitely a work in progress. 1 Introduction One of the most difficult parts of learning to program is knowing how to deconstruct a problem in

More information

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: IMPORTANT INSTRUCTIONS: 1. Question Paper in English and Hindi and Candidate can choose any one language. 2. In case of discrepancies in

More information

MGM s JNEC Question Bank Subject: Computer Engineering

MGM s JNEC Question Bank Subject: Computer Engineering MGM s JNEC Question Bank Subject: Computer Engineering 1. All of the following are examples of computer input units EXCEPT: a) Scanner b) Speaker c) Bar code reader d) Keyboard Answer: b 2. Which of the

More information

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging? Freshers Club Important 100 C Interview Questions & Answers 1) How do you construct an increment statement or decrement statement in C? There are actually two ways you can do this. One is to use the increment

More information

Pseudocode. Pseudocode. Guide for Pseudocode. Computers in Engineering Pseudocode and C Language Review. Example Pseudocode.

Pseudocode. Pseudocode. Guide for Pseudocode. Computers in Engineering Pseudocode and C Language Review. Example Pseudocode. Computers in Engineering Pseudocode and C Language Review Pseudocode Pseudocode is an artificial and informal language that helps you develop algorithms Pseudocode is similar to everyday English; it is

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

3/13/2012. ESc101: Multiple statement execution using loops. Loops are used when instructions are repeated. while statement can be used for a loop

3/13/2012. ESc101: Multiple statement execution using loops. Loops are used when instructions are repeated. while statement can be used for a loop ESc101: Multiple statement execution using loops Instructor: Krithika Venkataramani Semester 2, 2011-2012 The content of most of these slides are taken from the lecture slides of Prof. Arnab Bhattacharya

More information

Java iteration statements

Java iteration statements Java iteration statements Iteration statements are statements which appear in the source code only once, but it execute many times. Such kind of statements are called loops. Almost all the programming

More information

Relational & Logical Operators, if and switch Statements

Relational & Logical Operators, if and switch Statements 1 Relational & Logical Operators, if and switch Statements Topics Relational Operators and Expressions The if Statement The if- Statement Nesting of if- Statements switch Logical Operators and Expressions

More information

Control structures: Conditionals

Control structures: Conditionals Control structures: Conditionals Leo Ferres Department of Computer Science Universidad de Concepción leo@inf.udec.cl April 5, 2011 1a 1 Controlling the flow of a program: if... then... else In order to

More information

C Programming Lecture IV

C Programming Lecture IV C Programming Lecture IV Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ break and continue Statements break statement is used to terminate any type of loop such as while loop, do while loop and for

More information

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch Living with the Lab What s this void loop thing? Fall 2011 Gerald Recktenwald v: October 7, 2011 gerry@me.pdx.edu 1 Overview This document aims to explain two kinds of loops: the loop function that is

More information

Chapter 5 Programming Statements. Chapter Table of Contents

Chapter 5 Programming Statements. Chapter Table of Contents Chapter 5 Programming Statements Chapter Table of Contents OVERVIEW... 57 IF-THEN/ELSE STATEMENTS... 57 DO GROUPS... 58 IterativeExecution... 59 JUMPING... 61 MODULES... 62 Defining and Executing a Module....

More information

1 Introduction. 2 Overview of the Tool. Program Visualization Tool for Educational Code Analysis

1 Introduction. 2 Overview of the Tool. Program Visualization Tool for Educational Code Analysis Program Visualization Tool for Educational Code Analysis Natalie Beams University of Oklahoma, Norman, OK nataliebeams@gmail.com Program Visualization Tool for Educational Code Analysis 1 Introduction

More information

4.2 The while Loop. ***Same as Fig 3.6 in intro5e p91

4.2 The while Loop. ***Same as Fig 3.6 in intro5e p91 Chapter 4 Loops Objectives To use while, do-while, and for loop statements to control the repetition of statements ( 4.2-4.4). To understand the flow of control in loop statements ( 4.2-4.4). To use Boolean

More information

Programming for MSc Part I

Programming for MSc Part I Herbert Martin Dietze University of Buckingham herbert@the-little-red-haired-girl.org July 24, 2001 Abstract The course introduces the C programming language and fundamental software development techniques.

More information

Selection Structures: If and Switch Statements

Selection Structures: If and Switch Statements Selection Structures: If and Switch Statements Chuan-Ming Liu Computer Science & Information Engineering National Taipei University of Technology Taiwan 1 Outline Control Structures Conditions The If Statement

More information

C Programming Dr. Hasan Demirel

C Programming Dr. Hasan Demirel C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

Tutorial on C Language Programming

Tutorial on C Language Programming Tutorial on C Language Programming Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Introduction to System Software p.1/64 Tutorial on C programming C program structure:

More information

MPATE-GE 2618: C Programming for Music Technology. Unit 1.3

MPATE-GE 2618: C Programming for Music Technology. Unit 1.3 MPATE-GE 2618: C Programming for Music Technology Unit 1.3 More operators e ++ and -- are increment and decrement operators ++i or i++ is equivalent to i = i + 1; --i or i-- is equivalent to i = i - 1;

More information

Controlling The Real World With Computers ::. Control And Embedded Systems.::

Controlling The Real World With Computers ::. Control And Embedded Systems.:: Controlling The Real World With Computers ::. Control And Embedded Systems.:: Table Of Contents Previous: Reading a Schematic & Basic Electronics Next: Data lines, bits, nibbles, bytes, words, binary and

More information

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system. http://www.tutorialspoint.com/java/java_quick_guide.htm JAVA - QUICK GUIDE Copyright tutorialspoint.com What is Java? Java is: Object Oriented Platform independent: Simple Secure Architectural- neutral

More information

MATLAB Programming. Problem 1: Sequential

MATLAB Programming. Problem 1: Sequential Division of Engineering Fundamentals, Copyright 1999 by J.C. Malzahn Kampe 1 / 21 MATLAB Programming When we use the phrase computer solution, it should be understood that a computer will only follow directions;

More information

Today s Lecture. SYS-1A4Y Programming - Languages and Software Construction. Conditional Execution. Conditional Execution.

Today s Lecture. SYS-1A4Y Programming - Languages and Software Construction. Conditional Execution. Conditional Execution. SYS-1A4Y Programming - Languages and Software Construction Dr Gavin Cawley Lecture 9 - Selection Today s Lecture In the previous lectures: Using objects Strings NumberFormat In this lecture: Relational

More information

REPETITION WITH PYTHON

REPETITION WITH PYTHON REPETITION WITH PYTHON José M. Garrido Department of Computer Science May 2015 College of Computing and Software Engineering Kennesaw State University c 2015, J. M. Garrido Repetition with Python 2 Repetition

More information