Slides adopted from T. Ferguson Spring 2016

Similar documents
Slides adopted from T. Ferguson Spring 2016

Slides adopted from T. Ferguson Spring 2016

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Introduction to C. Systems Programming Concepts

1. The keyword main in C language is used for

Computer Programming. Decision Making (2) Loops

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Engineering 12 - Spring, 1998

The C language. Introductory course #1

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Engineering 12 - Spring, 1999

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

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

printf("this program adds the value 10 to a given integer number.\n\n");

Structured programming

Structured programming. Exercises 3

If Control Construct

1.3b Type Conversion

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

Decisions II. Switch Statement. If else allows a 2 way decision Switch allows for n-way decisions

Slides adopted from T. Ferguson Spring 2016

Structured programming

LAB 6: While and do-while Loops

Repetition Structures II

Decision Making -Branching. Class Incharge: S. Sasirekha

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

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

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

Introduction to Programming

Introduction to Programming

More examples for Control statements

Basic Assignment and Arithmetic Operators

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Module 6: Array in C

Subject: PIC Chapter 2.

Chapter 11 Introduction to Programming in C

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

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

Programming Style Guide v1.1

Number Systems, Scalar Types, and Input and Output

Control Structure: Loop

DECISION CONTROL AND LOOPING STATEMENTS

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Chapter 11 Introduction to Programming in C

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

What we have learned so far

Module 4: Decision-making and forming loops

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Fundamentals of Programming. Lecture 14 Hamed Rasifard

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

BSM540 Basics of C Language

EECE.2160: ECE Application Programming Fall 2017

Programming for Electrical and Computer Engineers. Loops

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

Chapter 11 Introduction to Programming in C

C Programming

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

UIC. C Programming Primer. Bharathidasan University

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

UNIT 4. CONTROL FLOW. Programming Year Grade in Industrial Technology Engineering. Paula de Toledo. David Griol

Control Flow, Functions and Basic Linkage

Worksheet 4 Basic Input functions and Mathematical Operators

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

A brief intro to pointers for the purposes of passing reference parameters

Lecture 10: Recursive Functions. Computer System and programming in C 1

Program Control Structures

Functions. Arash Rafiey. September 26, 2017

Chapter 11 Introduction to Programming in C

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

IPC144 - Introduction to Strings. array of characters is an array, each element of which has a character data type.

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

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

int marks[10]; // fixed size and fixed address No change in Memory address.

Name Roll No. Section

Chapter 13 Control Structures

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

for Loop Lesson 2 Outline

Lecture 6. Statements

COMP 2001/2401 Test #1 [out of 80 marks]

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Chapter 3: Arrays and More C Functionality

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

Conditional Expressions

Programming Language A

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Transcription:

CSE1311 Introduction to Programming for Science & Engineering Students Mostafa Parchami, Ph.D. Dept. of Comp. Science and Eng., Univ. of Texas at Arlington, USA Slides adopted from T. Ferguson Spring 2016

Topics Boolean Expressions If Statement Switch Statement 2

Why do we need conditions? scanf("%d", &pressurevalue); while (pressurevalue <= MAX_PRESSURE) if (pressurevalue == NOMINAL_VALUE) printf("pressure is nominal\n"); else printf("pressure is %d pounds\n", pressurevalue); for (i = 5; i <= pressurevalue; i = i + 10) printf("+"); printf("\n"); scanf("%d", &pressurevalue); // End while 3

What is a conditional Expression? A conditional expression evaluates to a value of true or false These are the two values for the Boolean type named after the mathematician George Boole When a conditional expression is evaluated, it results in a value of type Boolean In the C programming language there is no explicit Boolean type. Instead, false is 0 and true is any non-zero value. Many C programs use a #define macro to set a Boolean variable to true or false #define FALSE 0 #define TRUE 1 Conditional expressions are also called Boolean expressions A conditional expression is used whenever a true or false decision needs to be made about the value of certain variables or constants in a program 4

Expression Contents A conditional expression in C may contain any valid mathematical expression It may also contain relational operators and logical operators In addition, it may contain one or more function calls that return a value 5

Relational Operators ==,!= Equivalent, Not equivalent <, <= Less than, Less than or equals >, >= Greater than, Greater than or equals if (A == B) C = 5; if (A!= B) C = 10; for (i = 0; i < MAX_INDEX; i++) printf("*"); if (A <= B) D = 20; while (A > B) B++; if (A >= B) D = 40; A = B <= C; D = A == B; 6

Logical Operators && Logical AND Logical OR! Logical NOT if ( (A == B) && (C <= D) ) E = 5; if ( (A!= B) (C >= D) ) F = 10; while (!(A == B) ) B++; if (!((A >= B) && (C <= D) (E!= F))) G = 100; A = B C; D = A && B; 7

Boolean Algebra if (0 0) printf("does false OR false = true?"); if (0 && 0) printf("does false AND false = true?"); 8

if, if-else, if-else-if // Variable can be char or int switch(<variable>) case <value1>: // variable==value1 case <value2>: // variable==value2 default: // variable!=value1 and variable!=value2 9

// Variable can be char or int switch(<variable>) case <value1>: // variable==value1 case <value2>: default: if(<condition1>) //Condition1 is true else if(<condition2>) // Condition1=false, Condition 2=true switch if(<condition>) //Condition is true else // Condition is false if(<condition1>) //Condition1 is true else if(<condition2>) // Condition1=false, Condition 2=true 10

Programming Examples 1) Read an integer and print Odd if the number is odd, print Even otherwise. 2) Read an integer and if it is positive print Positive, otherwise print Negative. 3) Read three numbers and print them in decreasing order. 4) Read a character and if the character is equal to + read two integers and print their sum. a, read two numbers and print the average. If the value is n, read one number negative of that number. If the value is e, do nothing. Otherwise, print Entered wrong character! ; 5) Read a character, if it was 1 or 2 or 3 or a or b, print Class A!. If the character was 4 or 5 or 6 or c or d, print Class B!. 11

Problem 1 /*A program that reads an integer is even!*/ #include <stdio.h> int main() int num; printf("please enter an integer: "); scanf("%d", &num); if (num % 2 == 0) // Is it even? printf("even"); else // Is not even? printf("odd"); return 0; 12

Problem 2 /*A program that reads an integer and checks if it is positive or negative!*/ #include <stdio.h> int main() int num; printf("please enter an integer: "); scanf("%d", &num); if (num > 0) // Is it positive? printf("positive"); else if (num < 0) // Is it negative? printf("negative"); else // Not positive or negative?! printf("zero!"); return 0; 13

Problem 3 14

// A simple calculator #include <stdio.h> Problem 4 int main() char ch; float num1, num2, average, sum, negative; printf("please enter the control character: "); scanf("%c", &ch); switch (ch) case '+': printf("please enter the first number: "); scanf("%f", &num1); printf("please enter the second number: "); scanf("%f", &num2); sum = num1 + num2; printf("the sum of %f and %f is: %f", num1, num2, sum); 15

Problem 4 return 0; case 'a': case 'n': case 'e': default: printf("please enter the first number: "); scanf("%f", &num1); printf("please enter the second number: "); scanf("%f", &num2); average = (num1 + num2)/2; printf("the average of %f and %f is: %f", num1, num2, average); printf("please enter the first number: "); scanf("%f", &num1); negative = -num1; printf("the negative of %f is: %f", num1, negative); printf("entered wrong character!"); 16

#include <stdio.h> Problem 5 int main() char ch; printf("please enter the control character: "); scanf("%c", &ch); switch (ch) case '1': case '2': case '3': case 'a': case 'b': printf("class A!"); case '4': case '5': case '6': case 'c': case 'd': printf("class B!"); return 0; 17