3. Reserved words
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
4. Data Type Specifiers for printf()
SPECIFIER DATA
TYPES
SAMPLE OUTPUT
c char “%c”, ‘A’ A
d int “%d”, 123 123
i int “%i”, 345 345
f float or
double
“%f”,
123.45
123.45000
0
g float or
double
“%g”,
123.98
123.45
7. Program 4-1.2:
• #include<stdio.h>
• void main(){
• int i;
• long l;
• float f;
• double d;
• printf(“Enter a values for an int and a long: ”);
• scanf(“%i %li”, &I, &l);
• printf(“Your int is %i and long is %linn”, i,l);
•
• printf(“Now, enter values for float and a double: ”);
• scanf(“%f %f”, &f, &d);
• printf(“Your float is %f and double is %fnn”, f,d);
• }
9. Program 4-1.3
• #include<stdio.h>
• void main(){
• int x, y, sum;
• float quotient;
• printf(“Enter a number: “);
• scanf(“%i”, &x);
• printf(“Enter another number: ”);
• scanf(“%i”, &y);
• sum = x + y;
• printf(“Sum: %”, sum);
• }
10. Relational Operators
Operator symbol Operator function/Meaning
< Is less than to
> Is greater than to
<= Is less than or equal to
>= Is greater than or equal to
== Is equal to
!= Not equal to
11. Ex.
x = 45; -> the value of variable x is equal to 45
y = 40; -> the value of variable y is equal to 40
y==x -> is y equal to x?
x<=y -> is x less than or equal to y?
x!=y -> is x not equal to y?
14. The Three Control Structures or
program constructs:
• Sequence – The instructions are executed in a
serial manner, one after another.
• Selection – It provides a decision point that
enables the program to choose one between
two or more pathways.
• Iteration – Repeats a set of instructions a
number of times based on the condition
stated.
16. Selection
If & else statement
Syntax:
Single if – the ody of if statement will be
executed only if the condition’s.
Syntax:
if(condition){
statement/body;
}
18. If & else – the body of “if” will be executed if the
condition’s outcome is true, if false, the body of “else”
statement will be executed.
Syntax:
if(condition){
body/statement;
}
Else{
Body/statement;
}
20. If, else if & else – if the condition’s outcome of
“if” statement is true, its body will be executed
but if not it will test the condition of “else if”
statement, if it is true it will be executed and if
not it will execute the body of “else” statement
23. Example
#include<stdio.h>
#include<conio.h>
main(){
int x;
printf(“Enter a number from 1 - 3: ”);
scanf(“%i”, &x);
switch(x){
case 1: printf(“you entered one”); break;
case 2: printf(“you entered two”); break;
case 3: printf(“you entered three”); break;
default: printf(“invalid input”); break;
}
getch();
}
24. Iteration
Pre-test Loop - while loop is a control
flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The
while loop can be thought of as a repeating if
statement.The while construct consists of a block of
code and a condition. The condition is evaluated,
and if the condition is true, the code within the
block is executed. This repeats until the condition
becomes false. Because while loop checks the
condition before the block is executed.
27. Post-test Loop - is a control flow statement that
allows code to be executed repeatedly based on
a given Boolean condition. The do
while construct consists of a block of code and a
condition. First, the code within the block is
executed, and then the condition is evaluated. If
the condition is true the code within the block is
executed again. This repeats until the condition
becomes false. Because do while loops check
the condition after the block is executed.
30. Counter-controlled Loop syntax – is a
programming statement which allows code to be
repeatedly executed. Unlike many other kinds of loops,
such as the while loop, the for loop is often distinguished
by an explicit loop counter or loop variable. This allows
the body of the for loop (the code that is being
repeatedly executed) to know about the sequencing of
each iteration. For loops are also typically used when the
number of iterations is known before entering the loop.
For loops are shorthand way to make loops when the
number of iterations is known, as a for loop can be
written as a while loop.