6.1 Branching (or Selection)

Size: px
Start display at page:

Download "6.1 Branching (or Selection)"

Transcription

1 Chapter 6 Branching 6.1 Branching (or Selection) 6.2 Relational and Logical Operators 6.3 The if Statement 6.4 The if-else Statement 6.5 The if-else if-else Statement 6.6 The Nested-if Statement 6.7 The switch Statement 6.8 The Conditional Operator

2 6.1 Branching (or Selection) Decision Decision Decision!!!! C statements are executed normally. Sometimes we need to tell the computer to do something only when some conditions are satisfied. This will alter the normal sequential execution. The if statement serves this purpose. Three forms: if statement if... else statement if... else if... else statement The switch statement provides a multi-way decision structure. We can choose one action out of a number of actions depending on some conditions.

3 6.2 Relational and Logical Operators Used for comparison between two values. Return boolean result: true or false. Relational Operators: operator example meaning == ch == a equal to!= f!= 0.0 not equal to < num < 10 less than <= num <=10 less than or equal to > f > -5.0 greater than >= f >= 0.0 greater than or equal to

4 Logical operators: Work on one or more relational expressions to yield a logical value: true or false. Allows testing results of comparison of expressions. operator example meaning!!(num < 0) not && (num1 > num2) && (num2 >num3) and (ch == \t ) (ch == ) or! returns true when the operand is false and returns false when the operand is true. && returns true when both operands are true otherwise it returns false. returns false when both operands are false otherwise it returns true.

5 list of operators of decreasing precedence:! not * / multiply and divide + - add and subtract < <= > >= less, less or equal, greater, greater or equal ==!= equal, not equal && logical and logical or The result of evaluating an expression involving relational and/or logical operators is either 1 or 0. When the result is true, it is 1. Otherwise it is 0, i.e. C uses 0 to represent a false condition.

6 /* example of the value of relational & logical expressions */ #include <stdio.h> main(void) { float logic_value; /* Numeric value of relational and logical expression */ printf("logic values of the following relations:\n"); logic_value = (3 > 5); printf("(3 > 5) is %f\n", logic_value); logic_value = (3 <= 5); printf("(3 <= 5) is %f\n", logic_value); logic_value = (15!= 3*5); printf("(15!= 3*5) is %f\n", logic_value); logic_value = (10 < 5) && (24 <= 15); printf("(10 < 5) && (24 <=15) is %f\n", logic_value);

7 } /* notice the portion: 24/0!!! */ logic_value = (10 < 5) && (24/0 <= 15); printf("(10 < 5) && (24/0 <=15) is %f\n", logic_value); logic_value = (36/6 > 2*3) (8 == 8); printf("(36/6 > 2*3) (8 == 8) is %f\n", logic_value); /* 36/0 == 0??? what s going on??? */ logic_value = (8 == 8) (36/0 == 0); printf("(8 == 8) (36/0 == 0) is %f\n", logic_value); $ex6_1 Logic values of the following relations: (3 > 5) is (3 <= 5) is (15!= 3*5) is (10 < 5) && (24 <=15) is (10 < 5) && (24/0 <=15) is (36/6 > 2*3) (8 == 8) is (8 == 8) (36/0 == 0) is $

8 In general, any integer expression whose value is non-zero is considered true; else it is false. For example: 3 is true, 0 is false 1 && 0 is false 1 0 is true!(5 >= 3) (1) is true scanf(...) returns the number of items read. It returns the value EOF, i.e. -1, when end of input is encountered. For example, scanf( %d %d, &n1, &n2); returns 2 if there is no error in the process of input

9 Flowcharts (from Chapter 2) Symbol Name Process Decision Input/Output Terminal Flowlines

10 6.3 The if Statement The format of the if statement: if (expression) statement; false expression true statement statement may be a single statement terminated by a semicolon or a compound statement enclosed by { }

11 #include <stdio.h> main(void) /* example of simple if statement */ { int num; /* Value supplied by user. */ printf("give me a number from 1 to 10 => "); scanf("%d",&num); if (num > 5) printf("your number is larger than 5.\n"); printf("%d was the number you entered.\n",num); return 0; } $./ex6_2 Give me a number from 1 to 10 => 3 3 was the number you entered. $./ex6_2 Give me a number from 1 to 10 => 7 Your number is larger than 5. 7 was the number you entered. $

12 6.4 The if-else Statement The format of the if... else statement is if (expression) statement1; else statement2; false true expression statement_2 statement_1 Both statement1 and statement2 may be single statements terminated by a semicolon or a compound statement enclosed by { }

13 /* This program computes the maximum of num1, num2 */ main(void) { int num1, num2, max; printf("enter two integers:"); scanf("%d %d", &num1, &num2); if (num1 > num2) max = num1; else max = num2; printf("the maximum is %d\n", max); } return 0; Start Read num1, num2 true false num1>num2? max=num1 max=num2 Print max End $./ex6_3 Enter two integers: 9 4 The maximum is 9 $ $./ex6_3 Enter two integers: -2 0 The maximum is 0 $

14 6.5 The if-else if-else Statement The format is if (expression1) statement1; else if (expression2) statement2; else statement3; true false expression1 true false statement1 expression2 statement2 statement3 Each of statement1, statement2 and statement3 may be a single statement terminated by a semicolon or a compound statement enclosed by { }

15 We may have as many else if in the if statement as we want (within the compiler limit): if (expression1) statement1; else if (expression2) statement2; else if (expression3) statement3;... else statementn; Statement i is executed when the first i-1 expressions are false and the ith expression is true.

16 /* an example of if... else if... else statements */ #include <stdio.h> main(void) { float temp; /* Temperature reading. */ printf("give me the temperature reading: "); scanf("%f",&temp); if ((temp >= 100.0) && ( temp <= 120.0)) printf("temperature OK, continue process.\n"); else if (temp < 100.0) printf("temperature too low, increase energy.\n"); else printf("temperature too high, decrease energy.\n"); return 0; } $./ex6_4 Give me the temperature reading: Temperature OK, continue process. $./ex6_4 Give me the temperature reading: Temperature too high, decrease energy.

17 The if-else if-else Statement Start if (mark <= 100 && mark >= 80) Read mark grade = 'A'; else if (mark < 80 && mark >= 70) T mark<=100 && mark>=80? F grade = 'B'; else if (mark < 70 && mark >= 60) grade = 'C'; grade='a' T mark<80 && mark>=70? F else grade = 'F'; grade='b' T mark<70 && mark>=60? F grade='c' grade='f' Print grade End

18 6.6 Nested-if Both the if branch and the else branch may contain if statement(s). The level of nested if statements can be as many as we want (up to the compiler limit). E.g. if (expression1) statement1; else if (expression2) else statement2; statement3; true statement1 expression1 true statement2 false expression2 false statement3

19 if (expression1) else if (expression2) statement1; else statement2; statement3; Nested true false expression1 true false expression2 statement3 statement1 statement2

20 Rule -> C compiler associates an else part with the nearest unresolved if. if (expression1) if (expression2) statement1; else statement2; statement1 - is executed when expression1 and expression2 are true. statement2 - is executed when expression1 is true and expression2 is false. When expression1 is false - neither statements are executed.

21 Example: Nested-if statements /* This program computes the maximum value of three numbers */ main(void) { int n1, n2, n3, max; printf("please enter three integers:"); Start scanf("%d %d %d", &n1, &n2, &n3); Read n1, n2, n3 if (n1 >= n2) true false if (n1 >= n3) n1>=n2 max = n1;? true false true else max = n3; n1>=n3 n2>=n3 else if (n2 >= n3)?? max = n2; max=n1 max=n3 max=n2 else max = n3; printf("the maximum of the three is %d\n", max); return 0; Print max } End false max=n3

22 $./ex6_5 Please enter three integers: The maximum of the three is 3 $./ex6_5 Please enter three integers: The maximum of the three is 3 $./ex6_5 Please enter three integers: The maximum of the three is 3 $./ex6_5 Please enter three integers: The maximum of the three is 3 $

23 6.7 The switch Statement switch (expression) { } case constant1: statement1; break; case constant2: statement2; break; case constantn: statementn; break; default: The syntax of a switch statement is statementd; expression constant1? true statement1 break false constant2? true statement2 break false constant3? true statement3 break false statementd

24 switch, case, break and default are reserved words. the result of expression in ( ) must be integral type constant1, constant2,... are called labels. Each must be an integer constant, a character constant or an integer constant expression, e.g. 3, 'A', 4+'b', 5+7,... etc. each of the labels constant1, constant2,... must deliver unique integer value. Duplicates are not allowed.

25 #include <stdio.h> main(void) { Example ex6_6.c - Using Switch statement char selection; /* Item to be selected by the user */ printf("select the form of Ohm's law needed by letter:\n"); printf("a] Voltage; B] Current; C] Resistance;\n"); printf("your selection (A, B, or C) => "); scanf("%c", &selection); switch (selection) { case 'A' : printf("v = I * R\n"); break; case 'B' : printf("i = V / R\n"); break; case 'C' : printf("r = V / I\n"); break; default : printf("that was not one of the proper selections.\n"); } /* End of switch. */ return 0; } Menu

26 $./ex6_6 Select the form of Ohm's law needed by letter: A] Voltage; B] Current; C] Resistance; Your selection (A, B, or C) => A V = I * R $./ex6_6... Your selection (A, B, or C) => B I = V / R $./ex6_6... Your selection (A, B, or C) => C R = V / I $./ex6_6... Your selection (A, B, or C) => D That was not one of the proper selections. $

27 #include <stdio.h> /* Menu-driven application */ main(void) { we may have multiple labels for a statement, for example, to allow both the lower and upper case selection in the previous example, we do this: char selection; /* Item to be selected by the user */ printf("select the form of Ohm's law needed by letter:\n"); printf("a] Voltage; B] Current; C] Resistance;\n"); printf("your selection (A, B, or C) => "); scanf("%c",&selection); switch (selection) { case a : case 'A' : printf("v = I * R\n"); break; case b : case 'B' : printf("i = V / R\n"); break; Menu

28 } case c : case 'C' : printf("r = V / I\n"); break; default : printf("that was not one of the \ proper selections.\n"); } /* End of switch. */ return 0; if we do not use break after some statements in the switch statement, execution will continue with the statements for the subsequent labels until a break statement or the end of switch statement. This is called fall through situation.

29 #include <stdio.h> main(void) { char selection; /* User input selection. */... printf("your selection => "); scanf("%c", &selection); switch(selection) { case 'A' : printf("rt = R1 + R2 + R3\n"); case 'B' : printf("it = Vt / Rt\n"); case 'C' : printf("pt = Vt * It\n"); break; default : printf("that was not a correct selection!"); } /* End of switch. */ return 0; }

30 $./ex6_7... Your selection => A /* fall through */ Rt = R1 + R2 + R3 It = Vt / Rt Pt = Vt * It $./ex6_7... Your selection => B /* fall through */ $./ex65_7... Your selection => C Pt = Vt * It $./ex6_7... Your selection => D That was not a correct selection! $ It = Vt / Rt Pt = Vt * It

31 Another Example: switch(choice) { case 'a': case 'A': result = num1 + num2; printf("%d + %d = %d\n", num1,num2,result); case 's': case 'S': result = num1 - num2; printf("%d - %d = %d\n", num1,num2,result); case 'm': case 'M': result = num1 * num2; printf("%d1 * %d = %d\n",num1,num2,result); break; default: printf("not the proper choices.\n"); }

32 Start Read choice, num1, num2 choice case 'A' or 'a'? true result=num1+num2 Print result false case 'S' or 's'? true result=num1-num2 false case 'M' or 'm'? true false Print error message Print result result=num1*num2 Print result break Start

33 6.8 The Conditional Operator The conditional operator is used in the following way: expression_1? expression_2 : expression_3 The value of this expression depends on whether expression_1 is true or false. If expression_1 is true, then the value of the expression is that of expression_2 otherwise it is expression_3.

34 Example ex6_8.c: conditional expressions #include <stdio.h> main(void) { float led_voltage; /* Voltage across LED (all in V) */ float resistor_voltage; /* Voltage across resistor */ float source_voltage; /* Voltage of source */ float circuit_current; /* Current in the LED (A) */ float resistor_value; /* Value of resistor in ohms */ printf("enter the source voltage in volts => "); scanf("%f", &source_voltage); printf("enter value of resistor in ohms => "); scanf("%f", &resistor_value); led_voltage = (source_voltage < 2.3)? source_voltage : 2.3;

35 resistor_voltage = source_voltage - led_voltage; circuit_current = resistor_voltage / resistor_value; printf("total circuit current is %f amperes.\n", circuit_current); } return 0; $./ex6_8 Enter the source voltage in volts => 2 Enter value of resistor in ohms => 50 Total circuit current is amperes. $./ex6_8 Enter the source voltage in volts => 5 Enter value of resistor in ohms => 27 Total circuit current is amperes. $

36 /* example ex6_9.c: a conditional expression */ #include <stdio.h> main(void) { int selection; /* User input selection */ printf("enter a 1 or a 0 => "); scanf("%d",&selection); selection? printf("a one.\n") : printf("a zero.\n"); } return 0; $./ex6_9 Enter a 1 or a 0 => 1 A one. $./ex6_9 Enter a 1 or a 0 => 0 A zero. $

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

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

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

Chapter 5. Selection 5-1

Chapter 5. Selection 5-1 Chapter 5 Selection 5-1 Selection (Decision) The second control logic structure is selection: Selection Choosing between two or more alternative actions. Selection statements alter the sequential flow

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

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

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

Chapter 8 Selection 8-1

Chapter 8 Selection 8-1 Chapter 8 Selection 8-1 Selection (Decision) The second control logic structure is selection: Selection Choosing between two or more alternative actions. Selection statements alter the sequential flow

More information

The C Programming Language

The C Programming Language Chapter 1 The C Programming Language In this chapter we will learn how to write simple computer programs using the C programming language; perform basic mathematical calculations; manage data stored in

More information

Conditionals (with solutions)

Conditionals (with solutions) Conditionals (with solutions) For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations: final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87;

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

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority) Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

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

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers 0.. +127. char variables mostly used to store characters

More information

Two-way selection. Branching and Looping

Two-way selection. Branching and Looping 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.

More information

C Programming. Charudatt Kadolkar. IIT Guwahati. C Programming p.1/34

C Programming. Charudatt Kadolkar. IIT Guwahati. C Programming p.1/34 C Programming Charudatt Kadolkar IIT Guwahati C Programming p.1/34 We want to print a table of sine function. The output should look like: 0 0.0000 20 0.3420 40 0.6428 60 0.8660 80 0.9848 100 0.9848 120

More information

13 Classes & Objects with Constructors/Destructors

13 Classes & Objects with Constructors/Destructors 13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.

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

arrays C Programming Language - Arrays

arrays C Programming Language - Arrays arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

More information

Chapter One Introduction to Programming

Chapter One Introduction to Programming Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of

More information

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions Phys4051: C Lecture 2 & 3 Functions (Review) Comment Statements Variables & Operators Branching Instructions Comment Statements! Method 1: /* */! Method 2: // /* Single Line */ //Single Line /* This comment

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

Student Exploration: Circuits

Student Exploration: Circuits Name: Date: Student Exploration: Circuits Vocabulary: ammeter, circuit, current, ohmmeter, Ohm s law, parallel circuit, resistance, resistor, series circuit, voltage Prior Knowledge Questions (Do these

More information

Module 816. File Management in C. M. Campbell 1993 Deakin University

Module 816. File Management in C. M. Campbell 1993 Deakin University M. Campbell 1993 Deakin University Aim Learning objectives Content After working through this module you should be able to create C programs that create an use both text and binary files. After working

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

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer About The Tutorial C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system.

More information

AP Physics Electricity and Magnetism #4 Electrical Circuits, Kirchoff s Rules

AP Physics Electricity and Magnetism #4 Electrical Circuits, Kirchoff s Rules Name Period AP Physics Electricity and Magnetism #4 Electrical Circuits, Kirchoff s Rules Dr. Campbell 1. Four 240 Ω light bulbs are connected in series. What is the total resistance of the circuit? What

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

Tristan s Guide to: Solving Series Circuits. Version: 1.0 Written in 2006. Written By: Tristan Miller Tristan@CatherineNorth.com

Tristan s Guide to: Solving Series Circuits. Version: 1.0 Written in 2006. Written By: Tristan Miller Tristan@CatherineNorth.com Tristan s Guide to: Solving Series Circuits. Version: 1.0 Written in 2006 Written By: Tristan Miller Tristan@CatherineNorth.com Series Circuits. A Series circuit, in my opinion, is the simplest circuit

More information

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3. 2013 Computer Science 2nd Year Solved Exercise Chapter # 3 Programs Chapter # 4 Chapter # 5 Chapter # 6 Chapter # 7 Important Work Sheets Aumir Shabbir Pakistan School & College Muscat 3/4/2013 Chap #

More information

CP Lab 2: Writing programs for simple arithmetic problems

CP Lab 2: Writing programs for simple arithmetic problems Computer Programming (CP) Lab 2, 2015/16 1 CP Lab 2: Writing programs for simple arithmetic problems Instructions The purpose of this Lab is to guide you through a series of simple programming problems,

More information

Conditions & Boolean Expressions

Conditions & Boolean Expressions Conditions & Boolean Expressions 1 In C++, in order to ask a question, a program makes an assertion which is evaluated to either true (nonzero) or false (zero) by the computer at run time. Example: In

More information

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs Writing Simple C Programs ESc101: Decision making using if- and switch statements Instructor: Krithika Venkataramani Semester 2, 2011-2012 Use standard files having predefined instructions stdio.h: has

More information

CS 241 Data Organization Coding Standards

CS 241 Data Organization Coding Standards CS 241 Data Organization Coding Standards Brooke Chenoweth University of New Mexico Spring 2016 CS-241 Coding Standards All projects and labs must follow the great and hallowed CS-241 coding standards.

More information

Writing Control Structures

Writing Control Structures Writing Control Structures Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 5-1 Objectives After completing this lesson, you should be able to do the following: Identify

More information

The C Programming Language course syllabus associate level

The C Programming Language course syllabus associate level TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

More information

Computer Programming Tutorial

Computer Programming Tutorial Computer Programming Tutorial COMPUTER PROGRAMMING TUTORIAL by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Computer Prgramming Tutorial Computer programming is the act of writing computer

More information

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex Introduction to Lex General Description Input file Output file How matching is done Regular expressions Local names Using Lex General Description Lex is a program that automatically generates code for

More information

Dr. Estell s C Programs and Examples

Dr. Estell s C Programs and Examples Dr. Estell s C Programs and Examples Version 5.0-14 June 1996 All programs contained within this document Copyright 1994, 1995, 1996 by John K. Estell. Permission is given for reasonable, non-commercial

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

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

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true. Boolean Expressions Boolean Expressions Sometimes a programmer would like one statement, or group of statements to execute only if certain conditions are true. There may be a different statement, or group

More information

Gates, Circuits, and Boolean Algebra

Gates, Circuits, and Boolean Algebra Gates, Circuits, and Boolean Algebra Computers and Electricity A gate is a device that performs a basic operation on electrical signals Gates are combined into circuits to perform more complicated tasks

More information

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example Microcontroller Systems ELET 3232 Topic 8: Slot Machine Example 1 Agenda We will work through a complete example Use CodeVision and AVR Studio Discuss a few creative instructions Discuss #define and #include

More information

Experiment NO.3 Series and parallel connection

Experiment NO.3 Series and parallel connection Experiment NO.3 Series and parallel connection Object To study the properties of series and parallel connection. Apparatus 1. DC circuit training system 2. Set of wires. 3. DC Power supply 4. Digital A.V.O.

More information

So far we have considered only numeric processing, i.e. processing of numeric data represented

So far we have considered only numeric processing, i.e. processing of numeric data represented Chapter 4 Processing Character Data So far we have considered only numeric processing, i.e. processing of numeric data represented as integer and oating point types. Humans also use computers to manipulate

More information

Conditional Statements. 15-110 Summer 2010 Margaret Reid-Miller

Conditional Statements. 15-110 Summer 2010 Margaret Reid-Miller Conditional Statements 15-110 Summer 2010 Margaret Reid-Miller Conditional statements Within a method, we can alter the flow of control (the order in which statements are executed) using either conditionals

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

Introduction to Python

Introduction to Python Caltech/LEAD Summer 2012 Computer Science Lecture 2: July 10, 2012 Introduction to Python The Python shell Outline Python as a calculator Arithmetic expressions Operator precedence Variables and assignment

More information

Introduction to Java

Introduction to Java Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high

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 2: Elements of Java

Chapter 2: Elements of Java Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction

More information

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005 University of Toronto Department of Electrical and Computer Engineering Midterm Examination CSC467 Compilers and Interpreters Fall Semester, 2005 Time and date: TBA Location: TBA Print your name and ID

More information

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu C / C++ and Unix Programming Materials adapted from Dan Hood and Dianna Xu 1 C and Unix Programming Today s goals ú History of C ú Basic types ú printf ú Arithmetic operations, types and casting ú Intro

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

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

Visual Logic Instructions and Assignments

Visual Logic Instructions and Assignments Visual Logic Instructions and Assignments Visual Logic can be installed from the CD that accompanies our textbook. It is a nifty tool for creating program flowcharts, but that is only half of the story.

More information

J a v a Quiz (Unit 3, Test 0 Practice)

J a v a Quiz (Unit 3, Test 0 Practice) Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points

More information

Performing Simple Calculations Using the Status Bar

Performing Simple Calculations Using the Status Bar Excel Formulas Performing Simple Calculations Using the Status Bar If you need to see a simple calculation, such as a total, but do not need it to be a part of your spreadsheet, all you need is your Status

More information

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 04 Digital Logic II May, I before starting the today s lecture

More information

Programming in C an introduction

Programming in C an introduction Programming in C an introduction PRC for E Maarten Pennings Version 0.5 (2007-12-31) 0 0 Preface This book explains the C programming language. It assumes no programming knowledge. The reader is supposed

More information

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE PROGRAMMING IN C CONTENT AT A GLANCE 1 MODULE 1 Unit 1 : Basics of Programming Unit 2 : Fundamentals Unit 3 : C Operators MODULE 2 unit 1 : Input Output Statements unit 2 : Control Structures unit 3 :

More information

FEEG6002 - Applied Programming 5 - Tutorial Session

FEEG6002 - Applied Programming 5 - Tutorial Session FEEG6002 - Applied Programming 5 - Tutorial Session Sam Sinayoko 2015-10-30 1 / 38 Outline Objectives Two common bugs General comments on style String formatting Questions? Summary 2 / 38 Objectives Revise

More information

W03 Analysis of DC Circuits. Yrd. Doç. Dr. Aytaç Gören

W03 Analysis of DC Circuits. Yrd. Doç. Dr. Aytaç Gören W03 Analysis of DC Circuits Yrd. Doç. Dr. Aytaç Gören ELK 2018 - Contents W01 Basic Concepts in Electronics W02 AC to DC Conversion W03 Analysis of DC Circuits (self and condenser) W04 Transistors and

More information

Parallel DC circuits

Parallel DC circuits Parallel DC circuits This worksheet and all related files are licensed under the Creative Commons Attribution License, version 1.0. To view a copy of this license, visit http://creativecommons.org/licenses/by/1.0/,

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

Tristan s Guide to: Solving Parallel Circuits. Version: 1.0 Written in 2006. Written By: Tristan Miller Tristan@CatherineNorth.com

Tristan s Guide to: Solving Parallel Circuits. Version: 1.0 Written in 2006. Written By: Tristan Miller Tristan@CatherineNorth.com Tristan s Guide to: Solving Parallel Circuits. Version: 1.0 Written in 2006 Written By: Tristan Miller Tristan@CatherineNorth.com Parallel Circuits. Parallel Circuits are a little bit more complicated

More information

Circuit Analysis using the Node and Mesh Methods

Circuit Analysis using the Node and Mesh Methods Circuit Analysis using the Node and Mesh Methods We have seen that using Kirchhoff s laws and Ohm s law we can analyze any circuit to determine the operating conditions (the currents and voltages). The

More information

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan http://www.comp.nus.edu.sg/~cs1010/ 20 July 2015

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan http://www.comp.nus.edu.sg/~cs1010/ 20 July 2015 CS1010 Programming Methodology A beginning in problem solving in Computer Science Aaron Tan http://www.comp.nus.edu.sg/~cs1010/ 20 July 2015 Announcements This document is available on the CS1010 website

More information

Series and Parallel Circuits

Series and Parallel Circuits Series and Parallel Circuits Components in a circuit can be connected in series or parallel. A series arrangement of components is where they are inline with each other, i.e. connected end-to-end. A parallel

More information

Cornerstone Electronics Technology and Robotics I Week 15 Combination Circuits (Series-Parallel Circuits)

Cornerstone Electronics Technology and Robotics I Week 15 Combination Circuits (Series-Parallel Circuits) Cornerstone Electronics Technology and Robotics I Week 15 Combination Circuits (Series-Parallel Circuits) Administration: o Prayer o Turn in quiz Electricity and Electronics, Chapter 8, Introduction: o

More information

Chapter 5. Parallel Circuits ISU EE. C.Y. Lee

Chapter 5. Parallel Circuits ISU EE. C.Y. Lee Chapter 5 Parallel Circuits Objectives Identify a parallel circuit Determine the voltage across each parallel branch Apply Kirchhoff s current law Determine total parallel resistance Apply Ohm s law in

More information

TECH TIP # 37 SOLVING SERIES/PARALLEL CIRCUITS THREE LAWS --- SERIES CIRCUITS LAW # 1 --- THE SAME CURRENT FLOWS THROUGH ALL PARTS OF THE CIRCUIT

TECH TIP # 37 SOLVING SERIES/PARALLEL CIRCUITS THREE LAWS --- SERIES CIRCUITS LAW # 1 --- THE SAME CURRENT FLOWS THROUGH ALL PARTS OF THE CIRCUIT TECH TIP # 37 SOLVING SERIES/PARALLEL CIRCUITS Please study this Tech Tip along with assignment 4 in Basic Electricity. Parallel circuits differ from series circuits in that the current divides into a

More information

1Meg. 11.A. Resistive Circuit Nodal Analysis

1Meg. 11.A. Resistive Circuit Nodal Analysis 11. Creating and Using Netlists PART 11 Creating and Using Netlists For this entire text, we have created circuits schematically and then run simulations. Behind the scenes, Capture generates a netlist

More information

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16 s schedule Lecture 7 - C Programming for the Atmel AVR School of Electrical, l Computer and Telecommunications i Engineering i University of Wollongong Australia Week Lecture (2h) Tutorial (1h) Lab (2h)

More information

(hours worked,rateofpay, and regular and over time pay) for a list of employees. We have

(hours worked,rateofpay, and regular and over time pay) for a list of employees. We have Chapter 9 Two Dimensional Arrays In Chapter 7 we have seen that C provides a compound data structure for storing a list of related data. For some applications, however, such a structure may not be sucient

More information

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share. LING115 Lecture Note Session #4 Python (1) 1. Introduction As we have seen in previous sessions, we can use Linux shell commands to do simple text processing. We now know, for example, how to count words.

More information

Keywords: Dynamic pricing, Hotel room forecasting, Monte Carlo simulation, Price Elasticity, Revenue Management System

Keywords: Dynamic pricing, Hotel room forecasting, Monte Carlo simulation, Price Elasticity, Revenue Management System Volume 3, Issue 5, May 2013 ISSN: 2277 128X International Journal of Advanced Research in Computer Science and Software Engineering Research Paper Available online at: www.ijarcsse.com Dynamic Pricing

More information

The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5

The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5 The design recipe Readings: HtDP, sections 1-5 (ordering of topics is different in lectures, different examples will be used) Survival and Style Guides CS 135 Winter 2016 02: The design recipe 1 Programs

More information

T-SQL STANDARD ELEMENTS

T-SQL STANDARD ELEMENTS T-SQL STANDARD ELEMENTS SLIDE Overview Types of commands and statement elements Basic SELECT statements Categories of T-SQL statements Data Manipulation Language (DML*) Statements for querying and modifying

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

Variables, Constants, and Data Types

Variables, Constants, and Data Types Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class: L&L, 2.1-2.3, App C 1 Primitive Data There are eight

More information

Statements and Control Flow

Statements and Control Flow Contents 1. Introduction 2. Types and Variables 3. Statements and Control Flow 4. Reading Input 5. Classes and Objects 6. Arrays 7. Methods 8. Scope and Lifetime 9. Utility classes 10. Introduction to

More information

Example of a Java program

Example of a Java program Example of a Java program class SomeNumbers static int square (int x) return x*x; public static void main (String[] args) int n=20; if (args.length > 0) // change default n = Integer.parseInt(args[0]);

More information

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion 15.3.2014-20.3.2014

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion 15.3.2014-20.3.2014 German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Ahmed Gamal Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion 15.3.2014-20.3.2014

More information

PHYSICS 111 LABORATORY Experiment #3 Current, Voltage and Resistance in Series and Parallel Circuits

PHYSICS 111 LABORATORY Experiment #3 Current, Voltage and Resistance in Series and Parallel Circuits PHYSCS 111 LABORATORY Experiment #3 Current, Voltage and Resistance in Series and Parallel Circuits This experiment is designed to investigate the relationship between current and potential in simple series

More information

SOME EXCEL FORMULAS AND FUNCTIONS

SOME EXCEL FORMULAS AND FUNCTIONS SOME EXCEL FORMULAS AND FUNCTIONS About calculation operators Operators specify the type of calculation that you want to perform on the elements of a formula. Microsoft Excel includes four different types

More information

Java Basics: Data Types, Variables, and Loops

Java Basics: Data Types, Variables, and Loops Java Basics: Data Types, Variables, and Loops If debugging is the process of removing software bugs, then programming must be the process of putting them in. - Edsger Dijkstra Plan for the Day Variables

More information

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java CS1020 Data Structures and Algorithms I Lecture Note #1 Introduction to Java Objectives Java Basic Java features C Java Translate C programs in CS1010 into Java programs 2 References Chapter 1 Section

More information

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals: Numeral Systems Which number is larger? 25 8 We need to distinguish between numbers and the symbols that represent them, called numerals. The number 25 is larger than 8, but the numeral 8 above is larger

More information

Fig. 1 Analogue Multimeter Fig.2 Digital Multimeter

Fig. 1 Analogue Multimeter Fig.2 Digital Multimeter ELECTRICAL INSTRUMENT AND MEASUREMENT Electrical measuring instruments are devices used to measure electrical quantities such as electric current, voltage, resistance, electrical power and energy. MULTIMETERS

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

More information

C PROGRAMMING FOR MATHEMATICAL COMPUTING

C PROGRAMMING FOR MATHEMATICAL COMPUTING UNIVERSITY OF CALICUT SCHOOL OF DISTANCE EDUCATION BSc MATHEMATICS (2011 Admission Onwards) VI Semester Elective Course C PROGRAMMING FOR MATHEMATICAL COMPUTING QUESTION BANK Multiple Choice Questions

More information

1.00/1.001 - Session 2 Fall 2004. Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

1.00/1.001 - Session 2 Fall 2004. Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types 1.00/1.001 - Session 2 Fall 2004 Basic Java Data Types, Control Structures Java Data Types 8 primitive or built-in data types 4 integer types (byte, short, int, long) 2 floating point types (float, double)

More information

Decision Logic: if, if else, switch, Boolean conditions and variables

Decision Logic: if, if else, switch, Boolean conditions and variables CS 1044 roject 3 Fall 2009 Decision Logic: if, if else, switch, Boolean conditions and variables This programming assignment uses many of the ideas presented in sections 3 through 5 of the Dale/Weems text

More information

13.10: How Series and Parallel Circuits Differ pg. 571

13.10: How Series and Parallel Circuits Differ pg. 571 13.10: How Series and Parallel Circuits Differ pg. 571 Key Concepts: 5. Connecting loads in series and parallel affects the current, potential difference, and total resistance. - Using your knowledge of

More information

My EA Builder 1.1 User Guide

My EA Builder 1.1 User Guide My EA Builder 1.1 User Guide COPYRIGHT 2014. MyEABuilder.com. MetaTrader is a trademark of MetaQuotes www.metaquotes.net. Table of Contents MAIN FEATURES... 3 PC REQUIREMENTS... 3 INSTALLATION... 4 METATRADER

More information

Experiment: Series and Parallel Circuits

Experiment: Series and Parallel Circuits Phy203: General Physics Lab page 1 of 6 Experiment: Series and Parallel Circuits OBJECTVES MATERALS To study current flow and voltages in series and parallel circuits. To use Ohm s law to calculate equivalent

More information