![Page 1: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/1.jpg)
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 2: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/2.jpg)
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 3: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/3.jpg)
An example/* This program displays the absolute value of a number given by the
user */#include <stdio.h>
int main(void){
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 4: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/4.jpg)
If-else statementif (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 5: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/5.jpg)
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 6: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/6.jpg)
True or false In C, every expression has a numeric
value An expression is ‘true’ when its non-
zero If it is zero, it is false Therefore, in the following –
if (expression) statementstatement is executed if expression is non zero.
![Page 7: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/7.jpg)
More about operators
In C, every expression has a numeric value
When using arithmetical operators (+, -, *, /) this is straight forward The value of A+B is the sum of A and
B And so on…
![Page 8: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/8.jpg)
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 equals zero if A is larger than or equal to B (false), and some non-zero value if A is smaller than B (true)
The exact non-zero value varies (and is not important for that matter)
![Page 9: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/9.jpg)
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 10: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/10.jpg)
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 11: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/11.jpg)
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) equals 4 (y = 0) equals 0
![Page 12: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/12.jpg)
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 call it!
![Page 13: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/13.jpg)
Examples
val.c, eqn_sign.c
![Page 14: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/14.jpg)
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 15: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/15.jpg)
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 16: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/16.jpg)
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 17: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/17.jpg)
Else-if
if (expression)statement
else if (expression)statement
else if (expression)statement
elsestatement
![Page 18: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/18.jpg)
An exampleif (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 19: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/19.jpg)
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 20: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/20.jpg)
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 21: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/21.jpg)
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 22: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/22.jpg)
Solution
switch_case.c
![Page 23: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/23.jpg)
Exercise (difficult!)
Write a program such that – Input – a 3-digit number Output – the same number with digits
sorted Example – if input is 132, output
should be 123 Note – if input is not a 3-digit number,
display an error message and exit!
![Page 24: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/24.jpg)
Solution
Sort_digits.c
![Page 25: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/25.jpg)
Loops
Used to repeat the same instruction(s) over and over again.
C provides some flexible ways of deciding how many times to loop, or when to exit a loop.
for, while, do-while loops.
![Page 26: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/26.jpg)
While loops
while (condition) {
statement(s);}
The statements are executed as long as condition is true
When the condition is no longer true, the loop is exited.
![Page 27: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/27.jpg)
Example - factorial#include <stdio.h>int main(void){
int i,n,fact = 1;printf("Enter a number\n");scanf("%d", &n);i=1; while (i<=n){
fact = fact*i;i++;
}printf("the factorial is %d\n", fact);return 0;
}
This is a counter
Every iteration i is incremented by 1. Equivalent to i=i+1.
![Page 28: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/28.jpg)
Example – fibonacci series
fibonacci.c
![Page 29: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/29.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0
Screen
5
lim0
fib11
fib2---
fib_next
![Page 30: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/30.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0
Screen
5
lim0
fib11
fib2---
fib_next
![Page 31: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/31.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1
Screen
5
lim0
fib11
fib2---
fib_next
![Page 32: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/32.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1
Screen
5
lim0
fib11
fib21
fib_next
![Page 33: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/33.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1
Screen
5
lim1
fib11
fib21
fib_next
![Page 34: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/34.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1
Screen
5
lim1
fib11
fib21
fib_next
![Page 35: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/35.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1
Screen
5
lim1
fib11
fib21
fib_next
![Page 36: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/36.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1
Screen
5
lim1
fib11
fib21
fib_next
![Page 37: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/37.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1
Screen
5
lim1
fib11
fib22
fib_next
![Page 38: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/38.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1
Screen
5
lim1
fib11
fib22
fib_next
![Page 39: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/39.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1
Screen
5
lim1
fib12
fib22
fib_next
![Page 40: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/40.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1
Screen
5
lim1
fib12
fib22
fib_next
![Page 41: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/41.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2
Screen
5
lim1
fib12
fib22
fib_next
![Page 42: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/42.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2
Screen
5
lim1
fib12
fib23
fib_next
![Page 43: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/43.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2
Screen
5
lim2
fib12
fib23
fib_next
![Page 44: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/44.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2
Screen
5
lim2
fib13
fib23
fib_next
![Page 45: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/45.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2
Screen
5
lim2
fib13
fib23
fib_next
![Page 46: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/46.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2 3
Screen
5
lim2
fib13
fib23
fib_next
![Page 47: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/47.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2 3
Screen
5
lim2
fib13
fib25
fib_next
![Page 48: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/48.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2 3
Screen
5
lim3
fib13
fib25
fib_next
![Page 49: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/49.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2 3
Screen
5
lim3
fib15
fib25
fib_next
![Page 50: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/50.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2 3
Screen
5
lim3
fib15
fib25
fib_next
![Page 51: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/51.jpg)
Fibonacci – step by stepfib1 = 0;fib2 = 1;
printf("%d ", fib1);
while(fib2 < lim){
printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;
}
printf("\n");
0 1 1 2 3
Screen
5
lim3
fib15
fib25
fib_next
![Page 52: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/52.jpg)
getchar getchar() gets a single character from
the user. Requires including stdio.h Returns a non-positive number on
failure. Similar to scanf.
char c;
c = getchar();
char c;
scanf(“%c”, &c);
====
![Page 53: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/53.jpg)
putchar
putchar(‘char’) prints out the character inside the brackets.
Requires including stdio.h Similar to printf.
char c;
putchar(c);
char c;
printf(“%c”, c);====
![Page 54: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/54.jpg)
Example – lower-case to upper case.
low2up.c
![Page 55: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/55.jpg)
Low2up – step by step#include <stdio.h>
int main(void){ char c; char upper_c;
printf ("Please enter a string: ");
c = getchar();
Buffer
‘#’
‘@’
c upper_c
Screen
![Page 56: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/56.jpg)
Low2up – step by step#include <stdio.h>
int main(void){ char c; char upper_c;
printf ("Please enter a string: ");
c = getchar();
eS\n
Buffer
‘y’ ‘@’
c upper_c
Screen
![Page 57: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/57.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
eS\n
Buffer
‘y’ ‘@’
c upper_c
Screen
![Page 58: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/58.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
eS\n
Buffer
‘y’ ‘@’
c upper_c
Screen
![Page 59: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/59.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
eS\n
Buffer
‘y’ ‘Y’
c upper_c
Screen
![Page 60: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/60.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
eS\n
Buffer
‘y’ ‘Y’
c upper_c
Y
Screen
![Page 61: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/61.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
S\n
Buffer
‘e’ ‘Y’
c upper_c
Y
Screen
![Page 62: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/62.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
S\n
Buffer
‘e’ ‘Y’
c upper_c
Y
Screen
![Page 63: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/63.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
S\n
Buffer
‘e’ ‘Y’
c upper_c
Y
Screen
![Page 64: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/64.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
S\n
Buffer
‘e’ ‘E’
c upper_c
Y
Screen
![Page 65: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/65.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
S\n
Buffer
‘e’ ‘E’
c upper_c
YE
Screen
![Page 66: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/66.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
\n
Buffer
‘S’ ‘E’
c upper_c
YE
Screen
![Page 67: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/67.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
\n
Buffer
‘S’ ‘E’
c upper_c
YE
Screen
![Page 68: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/68.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
\n
Buffer
‘S’ ‘E’
c upper_c
YE
Screen
![Page 69: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/69.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
\n
Buffer
‘S’ ‘S’
c upper_c
YE
Screen
![Page 70: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/70.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
\n
Buffer
‘S’ ‘S’
c upper_c
YES
Screen
![Page 71: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/71.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
Buffer
‘\n’ ‘S’
c upper_c
YES
Screen
![Page 72: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/72.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
Buffer
‘\n’ ‘S’
c upper_c
YES
Screen
![Page 73: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/73.jpg)
Low2up – step by stepwhile (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;
/* Print the converted character. */ putchar(upper_c);
/* Get the next character */c = getchar();
} putchar('\n');
Buffer
‘\n’ ‘S’
c upper_c
YES
Screen
![Page 74: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/74.jpg)
Exercise
Input – Two integers – A and B
Output – How many times A contains B This is the result of the integer
division A/B Note –
Do not use the division operator!
![Page 75: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/75.jpg)
Solution#include <stdio.h>
int main(void){
int a, b, res;
printf("Please enter two numbers.\n");scanf("%d%d", &a, &b);
res = 0;while ( (res+1) * b <= a)
res = res + 1;
printf("%d / %d = %d", a, b, res);return 0;
}
![Page 76: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/76.jpg)
Break in a loop
When break is encountered, the loop is exited regardless of whether the condition is still true.
The program then continues to run from the first line after the while loop.
If called within a nested loop, break breaks out of the inner loop only.
![Page 77: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/77.jpg)
Example – counting letters
break.c
![Page 78: Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection](https://reader035.vdocuments.us/reader035/viewer/2022062407/56649d005503460f949d30cb/html5/thumbnails/78.jpg)
Continue
When continue is encountered, the rest of the loop is ignored.
The program then continues to run from the beginning of the loop.
Rarely used. Can usually be replaced by an
appropriate if-else statement.