Control structures: Conditionals

Size: px
Start display at page:

Download "Control structures: Conditionals"

Transcription

1 Control structures: Conditionals Leo Ferres Department of Computer Science Universidad de Concepción April 5, a 1 Controlling the flow of a program: if... then... else In order to control a program, you often need to tell it what to do if a certain condition holds; that is, if it is true that, for example, a variable x is greater than a variable y. The basic con conditional statement in C is the if statement. The simplest form is if( condition 1 ) statement1; While the complete form is: if( condition 1 ) statement1; else if( condition 2 ) statement2; else if( condition 3 ) statement3;... else statement4; In C, 0 is false, and anything else is true. So if you write something like if(1), the condition will always evaluate to true and the statement1 code will be executed. Conversely, if you write if(0), the condition will be false and no code inside the conditional scope will be executed. This is not too helpful in if statements, but it is helpful in the condition portion of for and while loops: an infinite loop could be encoded as while(1){, while searching for the first zero in an array A using a for loop could be encoded as for(int i=0;a[i];i++). The following is a complete example. It just asks for a character. If you enter?, the program terminates. The following is some boilerplate code: enternum.c 1a #include <stdio.h> int main(void) { enter some character 1b if clause 2a return 0; to enter some character we just 1b enter some character 1b (1a) int c = getchar(); 1

2 and the condition is, quite simply, 2a if clause 2a (1a) if(c==? ) printf ("You typed a question mark!\n"); which means that if the variable c equals the character? (literally when c = 63), then print the message and exit main. Given the nature of C, a lot of stuff you will have to do is input validation, that is, making what is called sanity checks on whatever the user is inputting: be it through command-line options or file-driven input, etc. We thus analyze one such validation strategy in the program that follows 1. We need two variable: one to hold the number we enter, and the other to keep whether the value entered is valid or not. 2b variables 2b (3d) int number; int valid = 0; Defines: number,, never used. valid, used in chunks 2 and 3. We use another way to enter a number by keyboard here: the scanf() function. The scanf() function reads input from the standard input stream stdin, 2c read number 2c (3b) printf("enter a number between 1 and 10: "); /*notice no \n*/ scanf("%d", &number); Our first condition could be whether the number is below (lower than) 1. This is written as 2d if1 2d (3b) if( number < 1 ) { printf("number is below 1. Please re-enter\n"); valid = 0; A few things to notice here: the first is that we wrote two lines in the if statement. If true, all expressions within the curly braces are executed (what happens if we don t write the braces here?). The other thing to notice is that we are using a further relational operator than we have been using so far; namely >. Below is a list of the relational operators we have available in C: == equal to!= not equal to > greater than < less than >= greater than or equal to <= less than or equal to The third thing to notice is the valid variable: this variable will keep value as to the state of the validity of the input. If it is 0, we need to reenter. The next lines cover one of the remaining possibilities, that is: is number > 10? 2e if2 2e (3b) else if( number > 10 ) { printf("number is above 10. Please re-enter\n"); valid = 0; 1 Taken from 2

3 while the final possibility is that the input is correct, and thus 0 < number < 11, in which case we make valid equal to 1. (We will see why in a minute). 3a the number is valid 3a (3b) else valid = 1; How do we keep asking to enter the correct number? We use the conditional in a while loop, like this: 3b while loop 3b (3d) while( valid == 0 ) { read number 2c if1 2d if2 2e the number is valid 3a which contains the prompt, the ifs, and the else statements in this instantiation of the law of trichotomy. The while loop will terminate on 0 (when the condition is false). However, in this case, we made it so that if number is 0, then the expression evaluates to 1 (not zero!). That is, when valid is 0, then the while expression is true. Conversely, when valid is any number but 0, then valid == 0 is 0! or false. Notice the difference between the zeros here? We finally print the verified number: 3c print the verified number 3c (3d) printf("the number is %d\n", number ); It all comes together like this: 3d verify.c 3d #include <stdio.h> int main(void) { variables 2b while loop 3b print the verified number 3c return 0; 3

4 2 Switch... Case NOTE: This section has been copied verbatim from Brian Kernighan s tutorial, reprinted at lysator.liu.se/c/bwk-tutor.html#switch. The switch statement can be used to replace the multi-way test we used in the last example. When the tests are like this: if( c == a )... else if( c == b )... else if( c == c )... else... testing a value against a series of constants, the switch statement is often clearer and usually gives better code. Use it like this: switch( c ) { case a : aflag++; case b : case c : cflag++; default: printf("%c?\n", c); The case statements label the various actions we want; default gets done if none of the other cases are satisfied. (A default is optional; if it isn t there, and none of the cases match, you just fall out the bottom.) The break statement in this example is new. It is there because the cases are just labels, and after you do one of them, you fall through to the next unless you take some explicit action to escape. This is a mixed blessing. On the positive side, you can have multiple cases on a single statement; we might want to allow both upper and lower case a : case A :... case b : case B :... But what if we just want to get out after doing case a? We could get out of a case of the switch with a label and a goto, but this is really ugly. The break statement lets us exit without either goto or label. switch( c ) { case a : case b :... /* the break statements get us here directly */ 4

5 The break statement also works in for and while statements; it causes an immediate exit from the loop. The continue statement works only inside for s and while s; it causes the next iteration of the loop to be started. This means it goes to the increment part of the for and the test part of the while. We could have used a continue in our example to get on with the next iteration of the for, but it seems clearer to use break instead. Index number,: 2b valid: 2b, 2d, 2e, 3a, 3b 5

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

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

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

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

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

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

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

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006 Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

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

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

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

6.1 Branching (or Selection)

6.1 Branching (or Selection) 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

More information

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

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

Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C

Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C Outline ompiling, interpreting, and running Administrative trivia Goals of the class Introduction to 1 2 The Programming Language Java vs. Systems programming language Originally used to write Unix and

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

More information

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

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

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

The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:

The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements: 1 2 Simple and compound s The if- Simple s: E.g.: expression; Various jumps : break, goto, continue, return. k = a * p + 3; printf("k = %d\n", k); 1 + 2; ; The if- comes in two forms: or E.g.: if (expression)

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

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

Introduction to C Programming

Introduction to C Programming Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

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

Borland C++ Compiler: Hello World Program

Borland C++ Compiler: Hello World Program Introduction Borland C++ Compiler: Hello World Program This article explains how to display text in a console program using the Borland C++ Compiler. The hello program writes Hello World to the console.

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 1: Variables and Data-types 1 Getting Started............................... A1 2 Some Basics.................................

More information

The switch Statement. Multiple Selection. Multiple Selection (cont.)

The switch Statement. Multiple Selection. Multiple Selection (cont.) The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7, 4.12 Multiple Selection So far, we have only seen binary selection. if (

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

Testing! Jennifer Rexford! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Testing! Jennifer Rexford! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6! Testing! Jennifer Rexford! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6! 1 Quotations on Program Testing! On two occasions I have been

More information

Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples

Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples Identifiers and Expressions CSE 130: Introduction to C Programming Spring 2005 Punctuation in C Statements are terminated with a ; Groups of statements are enclosed by curly braces: { and } Commas separate

More information

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Programming and Data Structures

Programming and Data Structures Programming and Data Structures Tutorial sheet: 2 Topics: Decision Making, Looping and Branching Q 1. (a) What is wrong with the following loop while ( n

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

Your first C program. Which one is best?

Your first C program. Which one is best? Your first C program #include void main(void) { printf( Hello, world!\n ); } #include void main(void) { printf( Hello, ); printf( world! ); printf( \n ); } Which one is best? #include

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

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

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

FORMAT MEANING VARIABLE TYPE

FORMAT MEANING VARIABLE TYPE Printf and Scanf Both formatted I/O Both sent to standard I/O location Printf Converts values to character form according to the format string Scanf Converts characters according to the format string,

More information

Algorithms. Introduction to C. Writing C Programs. Topics

Algorithms. Introduction to C. Writing C Programs. Topics Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs the smallest integer. (Hint: Think about keeping

More information

printf() Standard Library Function printf()

printf() Standard Library Function printf() printf() Standard Library Function Used to write text to the "standard output" Normally a computer monitor or printer Often the UART in embedded systems SIM Uart1 window in MPLAB SIM printf() Standard

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

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

Control structures in C

Control structures in C Control structures in C Lecture Topics Conditional constructs Iterative constructs Examples Style Lecture materials Textbook 13.3-13.5 Homework None Machine problem MP1.1 due February 2 at 5pm submitted

More information

Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program

Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

More information

Introduction to C Programming S Y STEMS

Introduction to C Programming S Y STEMS Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

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

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

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis. Birla Institute of Technology & Science, Pilani Computer Programming (CSF111) Second Semester 2015-2016 Lab-4 ---------------------------------------------------------------------------------------------------------------------------------

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

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

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

More information

Arrays. Arrays, Argument Passing, Promotion, Demotion

Arrays. Arrays, Argument Passing, Promotion, Demotion Arrays Arrays, Argument Passing, Promotion, Demotion Review Introduction to C C History Compiling C Identifiers Variables Declaration, Definition, Initialization Variable Types Logical Operators Control

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

Solutions to Assessment: Basic Programming Constructs

Solutions to Assessment: Basic Programming Constructs Solutions to Assessment: Basic Programming Constructs Question 1: Specify the minimum number of comparisons required to find the largest number among a set of 3 integers X, Y and Z. We already know how

More information

/* A program to add 2 integers. Comment statement in C/C++ (0) */ #include /* I/O standard library in C (1) */

/* A program to add 2 integers. Comment statement in C/C++ (0) */ #include <stdio.h> /* I/O standard library in C (1) */ printf and scanf statement: Example for analyses. /* A program to add 2 integers. Comment statement in C/C++ (0) */ #include /* I/O standard library in C (1) */ int main() /* must be first statement

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

Tutorial No. 8 - Solution (Strings and User Defined Functions)

Tutorial No. 8 - Solution (Strings and User Defined Functions) Tutorial No. 8 - Solution (Strings and User Defined Functions) 1. Explain strcat( ), strcpy( ),strncmp( ) and strstr( ) string manipulation function. [6] strcat() Function: The strcat function joins two

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

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc

Standard printing function in C is printf Prints everything numbers, strings, etc. May be complex to use. Standard C library is called libc Arrays and Structs and Pointers, Oh My! Programming in C Input and output Using printf Standard input and output Pointers Arrays Structures Combining these things together Arrays and Structs and Pointers,

More information

Introduction to Programming Block Tutorial C/C++

Introduction to Programming Block Tutorial C/C++ Michael Bader Master s Program Computational Science and Engineering C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers

More information

LESSON 7. ptr = &i; ptr = 0; ptr = NULL; /* equivalent to ptr == 0; */ ptr = (int *) 1999; /* an absolute address in memory */

LESSON 7. ptr = &i; ptr = 0; ptr = NULL; /* equivalent to ptr == 0; */ ptr = (int *) 1999; /* an absolute address in memory */ LESSON 7 POINTERS AND CALL BY REFERENCE When an expression is passed as an argument to a function, a copy of the value of the expression is made, and it is this copy that is passed to the function. Suppose

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

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

Chapter 6: Basic I/O. 6.1 printf

Chapter 6: Basic I/O. 6.1 printf Chapter 6: Basic I/O So far, we've been using printf to do output, and we haven't had a way of doing any input. In this chapter, we'll learn a bit more about printf, and we'll begin learning about character-based

More information

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E.

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK (2110003) [F.Y.B.E. SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR ODD/EVEN ACADEMICSEMESTER (2014-15) ASSIGNMENT / QUESTION BANK Subject: Computer Programming and Utilization (2110003) [F.Y.B.E.: ALL BRANCHES] Unit 1

More information

Tutorials for C ANSI C Programming in Brief

Tutorials for C ANSI C Programming in Brief Tutorials for C ANSI C Programming in Brief From www.cprogramming.com Edited by Capt. Tim Johnson, PE Associate Professor Department of Electronics and Mechanical Wentworth Institute of Technology 450

More information

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering Cohort: BCNS/07/FT- BIS/07/FT- BIS/06/FT -BM/07/FT BSE/07/FT/PT-

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

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

C Primer. Fall Introduction C vs. Java... 1

C Primer. Fall Introduction C vs. Java... 1 CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

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

C Programming Language CIS 218

C Programming Language CIS 218 C Programming Language CIS 218 Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language constructs that map efficiently to machine instructions,

More information

High-Level Language Interface

High-Level Language Interface Chapter 17 High-Level Language Interface Objectives To review motivation for writing mixed-mode programs To discuss the principles of mixed-mode programming To describe how assembly language procedures

More information

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, 2008. 1 Introduction 1. 2 Invoking Shell Scripts 2

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, 2008. 1 Introduction 1. 2 Invoking Shell Scripts 2 Unix Shell Scripts Norman Matloff July 30, 2008 Contents 1 Introduction 1 2 Invoking Shell Scripts 2 2.1 Direct Interpretation....................................... 2 2.2 Indirect Interpretation......................................

More information

What does the following code print? (Assume the program compiles without error). Trick question! Note the semicolon at the end of the second line.

What does the following code print? (Assume the program compiles without error). Trick question! Note the semicolon at the end of the second line. All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

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

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

(5-1) Selection Structures III in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 19, 2016) Washington State University

(5-1) Selection Structures III in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (September 19, 2016) Washington State University (5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O Fallon CptS 121 (September 19, 2016) Washington State University Advantages of switch Statements (1) One issue with nested if

More information

Figure 1.12 Entering, Translating, and Running a High-Level Language Program Pearson Education, Inc. All rights reserved.

Figure 1.12 Entering, Translating, and Running a High-Level Language Program Pearson Education, Inc. All rights reserved. Figure 1.12 Entering, Translating, and Running a High-Level Language Program 1 2007 Pearson Education, Inc. All rights reserved. 2 Figure 2.7 General Form of a C Program 2007 Pearson Education, Inc. All

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

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

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

1 Description of The Simpletron

1 Description of The Simpletron Simulating The Simpletron Computer 50 points 1 Description of The Simpletron In this assignment you will write a program to simulate a fictional computer that we will call the Simpletron. As its name implies

More information

Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments

Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments Reading C Programming Language Types, operators, expressions Control flow, functions Basic IO K&R Chapter 2 Types, Operators, and Expressions K&R Chapter 3 Control Flow K&R Chapter 7 Basic I/O NEWS Assignment

More information

C Control Statements : Branching and jumps

C Control Statements : Branching and jumps C Control Statements : Branching and jumps Using Bloodshed Dev-C++ Heejin Park Hanyang University 2 Introduction The if Statement Adding else to the if Statement Let's Get Logical A Word-Count Program

More information

Python to C/C++ Fall 2011

Python to C/C++ Fall 2011 Python to C/C++ Fall 2011 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. C: Both require #include directives to access libraries

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation Dynamic memory allocation How to allocate memory for variables (esp. arrays/strings) during run time malloc(), calloc(), realloc(), and free() 1 Why dynamic memory allocation?

More information

LAB 1 REPRESENTATION OF NUMBERS AND INTRODUCTION TO C

LAB 1 REPRESENTATION OF NUMBERS AND INTRODUCTION TO C LAB 1 REPRESENTATION OF NUMBERS AND INTRODUCTION TO C 1. LAB OBJECTIVE The objective of this lab is to review binary numbers and to review/introduce you to the C programming language and the MATLAB environment.

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

for Loop Lesson 1 Outline

for Loop Lesson 1 Outline Outline 1. Outline 2. A while Loop That Counts #1 3. A while Loop That Counts #2 4. A while Loop That Counts #3 5. A while Loop That Counts #4 6. Count-Controlled Loops #1 7. Count-Controlled Loops #2

More information

Convert Temperature CMPE-013/L. Constants and Literals. The Code #include Gabriel Hugh Elkaim Winter 2014

Convert Temperature CMPE-013/L. Constants and Literals. The Code #include <stdio.h> Gabriel Hugh Elkaim Winter 2014 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Winter 2014 Convert Temperature Want to write a program to convert the temperature from Fahrenheit to Celsius for a range of temperatures. Demonstrates

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

z = x + y * z / 4 % 2-1

z = x + y * z / 4 % 2-1 1.Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1? A. rem = 3.14 % 2.1; B. rem = modf(3.14, 2.1); C. rem = fmod(3.14, 2.1); D. Remainder cannot be obtain

More information

The Basics of C Programming. Marshall Brain

The Basics of C Programming. Marshall Brain The Basics of C Programming Marshall Brain Last updated: October 30, 2013 Contents 1 C programming 1 What is C?................................. 2 The simplest C program, I........................ 2 Spacing

More information

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47 A Crash Course in Programming with C++ and the Ubuntu O.S. Dr. Daniel A. Ray MCS Dept UVa-Wise June 8, 2010 ()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, 2010 1 / 47 Outline What

More information

Previously we saw that a string constant was just a sequence of characters enclosed within quotation marks. Now we take a look at string variables.

Previously we saw that a string constant was just a sequence of characters enclosed within quotation marks. Now we take a look at string variables. 1 Programming with C Terry Marris November 2010 2 Strings Previously we saw that a string constant was just a sequence of characters enclosed within quotation marks. Now we take a look at string variables.

More information

Data Structures. Arrays. Array Example. Array Initialization. 57:017, Computers in Engineering Review of Arrays, Sorting, Searching

Data Structures. Arrays. Array Example. Array Initialization. 57:017, Computers in Engineering Review of Arrays, Sorting, Searching 57:07, Computers in Engineering Review of Arrays, Sorting, Searching Data Structures Our focus thus far has mainly been on control structures An array is a data structure consisting of data items of the

More information

Part I:( Time: 90 minutes, 30 Points)

Part I:( Time: 90 minutes, 30 Points) Qassim University Deanship of Educational Services Preparatory Year Program- Computer Science Unit Final Exam - 1434/1435 CSC111 Time: 2 Hours + 10 Minutes 1 MG Student name: Select the correct choice:

More information

Python Loops and String Manipulation

Python Loops and String Manipulation WEEK TWO Python Loops and String Manipulation Last week, we showed you some basic Python programming and gave you some intriguing problems to solve. But it is hard to do anything really exciting until

More information

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode?

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode? PE1 Worksheet Problem solving 1) What are the four stages of problem solving by programming? What shall be done in each stage? Stage 1: Stage 2: Stage 3: Stage 4: 2) What is the top-down design strategy

More information