Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Conditional Statement
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Flow of Control Part 1: Selection
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
1 C Programming Week 2 Variables, flow control and the Debugger.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4 – C Program Control
Chapter 4: Making Decisions.
Chapter 4 (Conditional Statements)
Chapter 4 - Program Control
Condition Statements.
Chapter 4: Making Decisions.
Introduction to Programming
- Additional C Statements
C Programming Variables.
CS1100 Computational Engineering
Week 2 Variables, flow control and the Debugger
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Flow of Control.
Presentation transcript:

Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission

Selection Statements Selects statements to execute based on the value of an expression  The expression is sometimes called the controlling expression Selection statements:  if statement  switch statement

Selection statements: if used to execute conditionally a statement or block of code. if (expression) statement If expression is true, statement is executed (what is true?). statement can be replaced by a block of statements, enclosed in curly braces.

An example /* This program displays the absolute value of a number given by the user */ #include int main() { double num; printf("Please enter a real number: "); scanf("%lf", &num); if (num<0) num = -num; printf("The absolute value is %g\n", num); return 0; }

if-else statement if (expression) statement 1 else statement 2 if expression is true, statement 1 is executed. if expression is false, statement 2 is executed both statements can be (and very often are) replaced by blocks of statements (“compound statements”)

An example (fragment) int first, second, min; /* … */ if (first < second) { min = first; printf ("The first number is smaller than the second.\n"); } else { min = second; printf ("The second number is smaller than the first\n"); } printf("The smaller number is equal to %d\n", min);

True or false In C, every expression has a numeric value An expression is ‘true’ when its value is non-zero If it is zero, it is false Therefore, in the following – if (expression) statement statement is executed if expression is non zero.

More about operators In C, every expression has a numeric value When using arithmetical operators (+, -, *, /) this is straightforward  The value of A+B is the sum of A and B  And so on…

More about operators Expressions with relational operators (, >=, etc.) have values as well (intuitively, we are used to thinking about them as ‘true’ or ‘false’) A < B evaluates to zero if A is larger than or equal to B, and some non-zero value if A is smaller than B The exact non-zero value varies (and is not important for that matter)

Relational operators They are:  A == B (Note the difference from A = B!!!!!)  A != B  A < B  A > B  A <= B  A >= B The value of the expression is non-zero if it’s true, zero if it’s false

An example int a, b; printf("Enter two numbers\n"); scanf("%d%d", &a, &b); if (a == b) { printf("The numbers equal %d\n", a); printf("The expression a == b is %d\n", a == b); } else { printf("The numbers are not equal\n"); printf("The expression a == b is %d\n", a == b); }

The assignment operator = The assignment operator is also an operator. Hence, expressions involving it have a numeric value. This value equals to whatever appears on the right of the assignment operator For example:  (x = 4) evaluates to 4  (y = 0) evaluates to 0

A very common mistake Very often a programmer might confuse between the equality operator and the assignment operator:  if (x==4) …  if (x=4) … The second is usually a mistake, but legal in C so the compiler doesn’t warn us about it!

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); }

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); }

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); } i= 2

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); }

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); } (i==4) = 0

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); }

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); } i= 2

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); }

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); } (i=4) = 4

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); }

Example /* This program exemplifies the difference between the equality relational operator and the assignment operator. */ #include int main() { int i = 2; printf("i = %d\n", i); printf("(i==4) = %d\n", i==4); printf("i = %d\n", i); printf("(i=4) = %d\n", i=4); printf("i = %d\n", i); } i= 4

Logical operators Allows to evaluate two or more expressions -  !A – ‘not’ - True when A is not, and vice versa.  A && B – ‘and’ - True when both A and B are true  A || B – ‘or’ (inclusive or) - True when either A or B (or both) are true

A silly example #include int main(void) { int grade; printf("Please enter your grade: "); scanf("%d", &grade); if (grade 100) printf("This is not a valid grade!\n"); else printf("This is indeed a grade.\n"); return 0; }

else if if statements distinguish between exactly 2 cases and execute different code in each case The else-if construction allows for a multi-way decision

else if if (expression) statement else if (expression) statement else if (expression) statement else statement

An example if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n");

Validating input When getting input from the user, it is highly recommended to check whether it is valid. If it’s not, you should display an appropriate message and return a non-zero value. For example – if (grade 100) { printf(“Invalid input!\n”); return 1; }

The return keyword For now, used to terminate the program and return a value to the operating system If the program is successful the return value should be zero; non-zero otherwise The exact nature of this keyword will become clear in the future

Exercise Input –  An English letter Output –  If input is a lowercase letter – the corresponding uppercase letter  If input is an uppercase letter - corresponding lowercase letter Notes – 1. You do not need the specific ASCII values!! 2. Remember to check for input validity!

Solution #include int main( ) { char c; printf("Please enter an english letter: "); scanf("%c", &c); if (c = 'a') printf("%c in uppercase is %c\n", c, c-'a'+'A'); else if (c = 'A') printf("%c in lowercase is %c\n", c, c-'A'+'a'); else { printf("%c is not an english letter!\n", c); return 1; } return 0; }

Exercise Input  Two integers, A and B Output  Their relation (i.e. displays A==B, A B)

Solution #include int main() { int A, B; printf("Enter two Numbers\n"); scanf("%d%d", &A, &B); if (A == B) printf("A==B\n"); else if (A > B) printf("A>B\n"); else printf("A<B\n"); return 0; }

The ?: operator expr1 ? expr2 : expr3 Nicer way to write:  (expr1)? expr2 : expr3 If expr1 is true (non-zero), expr2 is evaluated. Otherwise, expr3 is evaluated

The ?: operator #include int main() { int i, j, min; printf("Please enter two numbers: "); scanf("%d%d", &i, &j); min = (i < j)? i : j; printf("The minimum between %d and %d is %d\n", i, j, min); return 0; }

The switch statement a multiway conditional statement  similar to if-else if-else  allows the selection of an arbitrary number of choices based on an integer value switch (expression) { case const-expr: statements case const-expr: statements … default: statements }

The switch statement expression must have an integer value when the switch statement is executed:  the expression is evaluated  if a case matches the value of the expression, the program jumps to the first statement after that case label  otherwise, the default case is selected  the default is optional

That grade example again switch (grade/10) { case 10: case 9: printf ("A\n"); break; case 8: printf ("B\n"); break; case 7: printf ("C\n"); break; case 6: printf ("D\n"); break; default: printf ("F\n"); }

Give me a break when the switch transfers to the chosen case, it starts executing statements at that point it will “fall through” to the next case unless you “break out” break causes the program to immediately jump to the next statement after the switch statement

A more interesting example #include int main( ) { double n1, n2, res; char op; printf("Please enter two numbers: "); scanf("%lf%lf", &n1, &n2); printf("Please enter an arithmetical operator (+, -, * or /): "); scanf(" %c", &op); /* The space before the %c will be explained... */

A more interesting example switch(op) { case '+': res = n1+n2; break; case '-': res = n1-n2; break; case '*': res = n1*n2; break; case '/': /* We're not checking for division by zero for clarity... */ res = n1/n2; break; default: printf("%c is an invalid arithmetical operator!\n", op); return 1; } /* Display the expression and its result */ printf("%g %c %g = %g\n", n1, op, n2, res); return 0; }

Exercise Write a program that accepts a number between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin 1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar Remember to check for the validity of the input!

Solution /#include int main() { int num; printf("Please enter a number from 1 to 100: "); scanf("%d", &num); /* Make sure the input is valid */ if (num 100) { printf("Invalid input!\n"); return 1; } /* Display the correct coin name, or a default message if there's no such coin */ switch (num) { case 1: printf("It's a cent!\n"); break; case 5: printf("It's a nickel!\n"); break; case 10: printf("It's a dime!\n"); break; case 25: printf("It's a quarter!\n"); break; case 100: printf("It's a whole dollar!\n"); break; default: printf("It's not a coin!\n"); } return 0; }

Debugging It is virtually impossible to program without errors Syntax errors are detected by the compiler However, often a program has no syntax errors and compiles, but still doesn’t perform as desired

Debugging Debuggers are software tools designed to help find software bugs Both Visual C and the lcc compiler include a debugger

Debugging The debugger allows us to –  Execute the program one line at a time  At each step see the values of all variables and expressions  Run the program up to a pre-specified point  And more…