Programming C for Engineers

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

description

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: - PowerPoint PPT Presentation

Transcript of Programming C for Engineers

Page 1: Programming C for Engineers

Programming C for Engineers

An exercise is posted on the web site! Due in one week Single submission

Page 2: Programming C for Engineers

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

Page 3: Programming C for Engineers

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.

Page 4: Programming C for Engineers

An example

/* This program displays the absolute value of a number given by the user */

#include <stdio.h>

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;}

Page 5: Programming C for Engineers

if-else statement

if (expression) statement1

else statement2

if expression is true, statement1 is executed. if expression is false, statement2 is executed both statements can be (and very often are)

replaced by blocks of statements (“compound statements”)

Page 6: Programming C for Engineers

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);

Page 7: Programming C for Engineers

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.

Page 8: Programming C for Engineers

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…

Page 9: Programming C for Engineers

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)

Page 10: Programming C for Engineers

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

Page 11: Programming C for Engineers

An exampleint 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);}

Page 12: Programming C for Engineers

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

Page 13: Programming C for Engineers

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!

Page 14: Programming C for Engineers

Examples

val.c, eqn_sign.c

Page 15: Programming C for Engineers

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

Page 16: Programming C for Engineers

A silly example

#include <stdio.h>

int main(void) {

int grade;

printf("Please enter your grade: ");scanf("%d", &grade);

if (grade < 0 || grade > 100)printf("This is not a valid grade!\n");

elseprintf("This is indeed a grade.\n");

return 0;}

Page 17: Programming C for Engineers

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

Page 18: Programming C for Engineers

else if

if (expression)

statement

else if (expression)

statement

else if (expression)

statement

else

statement

Page 19: Programming C for Engineers

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");

Page 20: Programming C for Engineers

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 < 0 || grade > 100){

printf(“Invalid input!\n”);return 1;

}

Page 21: Programming C for Engineers

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

Page 22: Programming C for Engineers

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 Note –

Remember to check for input validity!

Page 23: Programming C for Engineers

Solution

switch_case.c

Page 24: Programming C for Engineers

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

Page 25: Programming C for Engineers

The ?: operator

#include <stdio.h>

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;}

Page 26: Programming C for Engineers

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}

Page 27: Programming C for Engineers

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

Page 28: Programming C for Engineers

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");

}

Page 29: Programming C for Engineers

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

Page 30: Programming C for Engineers

A more interesting example

operation.c

Page 31: Programming C for Engineers

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!

Page 32: Programming C for Engineers

Solution

coins.c

Page 33: Programming C for Engineers

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

Page 34: Programming C for Engineers

Debugging

Debuggers are software tools designed to help find software bugs

Both Visual C and the lcc compiler include a debugger

Page 35: Programming C for Engineers

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…