C Programming Arrays
C Programming Arrays
Lecture 2
Fall 2019
Conversions Booleans Relational Operators Branching Iterations
Type Conversions
I When data of different types are combined (via operators)
some rules are applied
I Types are converted to a common type
I Usually, to the larger one (called promotion)
I Example: while summing an int and a float, the int is
converted into a float and then the sum is performed
I A demotion is performed when a type is converted to a
smaller one
I Example: a function takes an int parameter and you provide
a float
I A demotion implies possible loss of information
I Therefore, be careful with what to expect
I In the above example, the fractional part will be lost
Casting
Casting: An Example
1 int a ;
2 float f1 = 3.456;
3 float f2 = 1.22;
4 /* these operations imply demotions */
5 a = ( int ) f1 * f2 ; /* a is now 3 */
6 a = ( int ) ( f1 * f2 ) ; /* a is now 4 */
1 int a , b ;
2 a = b = 0;
3 a ++; b - - ; ++ a ; --b ;
I Prefix means that first you modify and then you use the value
I Postfix means that first you use and then you modify the value
I int a = 10, b;
Boolean Variables
Boolean Operators
A NOT A A B A AND B A B A OR B
false true false false false false false false
true false false true false false true true
true false false true false true
true true true true true true
Booleans in C
1 int main () {
2 int a , b , c ;
3 a = 0; /* a is false */
4 b = 57; /* b is true */
5 c = a || b ; /* c is true */
6 c = a && b ; /* c is false */
7 a = !a; /* a is now true */
8 c = a && b ; /* c is now true */
9 c = ( a && ! b ) && ( a || b);
10 return 0;
11 }
Relational Operators
I Relational operators are applied to other data types (numeric,
character, etc.) and produce boolean values
(b > 5) --> true
I Relational operators with boolean operators produce boolean
expressions
(b > 5) && (a < 1) --> true && false --> false
Relational operator Meaning
== Equality test
!= Inequality test
> Greater
< Smaller
>= Greater or equal
<= Smaller or equal
Branching
I General syntax:
1 if ( condition )
2 statement 1;
3 else
4 statement 2;
5 other_statement ; /* always executed */
condition
TRUE FALSE
statement 1 statement 2
other_statement
if: Example
1 # include < stdio .h >
2 int main () {
3 int first , second ;
4 printf ( " Type the first number :\ n " ) ;
5 scanf ( " % d " , & first ) ;
6 printf ( " Type the second number :\ n " ) ;
7 scanf ( " % d " , & second ) ;
8 if ( first > second ) {
9 printf ( " The larger one is % d \ n " , first ) ;
10 }
11 else {
12 printf ( " The larger one is % d \ n " , second ) ;
13 }
14 printf ( " Can you see the logical error ?\ n " ) ;
15 return 0;
16 }
C/C++ Fall 2019 19 / 30
Conversions Booleans Relational Operators Branching Iterations
6 case c2 :
7 statement2 ;
8 break ;
9
10 ...
11
12 default :
13 def ault_s tateme nt ;
14 }
expression == c1
True statement 1
False
expression == c2 True statement 2
False
expression == cn True statement n
False
default
statement
switch: Example
1 # include < stdio .h >
2 int main () {
3 int c ;
4 for ( c = 0; c <= 3; c ++) {
5 printf ( " c : % d \ n " , c ) ;
6
7 switch ( c ) {
8 case 1:
9 printf ( " Here is 1\ n " ) ;
10 break ;
11 case 2:
12 printf ( " Here is 2\ n " ) ;
13 /* Fall through */
14 case 3:
15 case 4:
16 printf ( " Here is 3 , 4\ n " ) ;
17 break ;
18 default :
19 printf ( " Here is default \ n " ) ;
20 }
21 }
22 return 0;
23 }
Iterations
Iterations: while
I General syntax:
1 while ( condition ) {
2 statement ;
3 }
condition
FALSE
TRUE
statement
other_statement
Example:
Compute the Sum of the First n Natural Numbers
1 # include < stdio .h >
2 int main () {
3 int idx , n , sum = 0;
4 printf ( " Enter a positive number " ) ;
5 scanf ( " % d " , & n ) ;
6 idx = 1;
7 while ( idx <= n ) {
8 sum += idx ;
9 idx ++;
10 }
11 printf ( " The sum is % d \ n " , sum ) ;
12 return 0;
13 }
Iterations: for
I General syntax:
1 for ( initial - statement ; condition ; iteration -
statement )
2 statement ;
I Example:
1 for ( n = 0; n <= 10; n ++)
2 printf ( " % d \ n " , n ) ;
condition
FALSE
iteration- TRUE
statement
statement
other_statement