Lecture3_Control_the_program_flow
Lecture3_Control_the_program_flow
Lec 3:
Control the program flow
By:
Abdallah M A Saad
Agenda
Logical operators.
Control flow and its types.
Two selection structures:
If
If/else
Two repetition structure.
While
Do/while
Let’s Code!!
2
Agenda
Logical operators.
Control flow and its types.
Two selection structures:
If
If/else
Two repetition structure.
While
Do/while
Let’s Code!!
3
Logical operators
X && Y
&& Logical And
True only if X and Y are True Values.
X || Y
|| Logical Or
False only if X and Y are False Values.
!X
! Logical Not
True if X False and False otherwise.
4
Agenda
Logical operators.
Control flow and its types.
Two selection structures:
If
If/else
Two repetition structure.
While
Do/while
Let’s Code!!
5
Control flow and its types
The order which the instructions follow during program
execution.
3 basic control structures;
Sequence (ordinary order),
Selection (if, if/else, switch),
repetition(while, do/while, for).
Agenda
Logical operators.
Control flow and its types.
Two selection structures:
if
if/else
Two repetition structure.
While
Do/while
Let’s Code!!
7
Two selection structures:
if(condition){
code;
}
Rad < 0?
True
●
if(rad<0){
printf(“Radius can not be negative\n”); Print “rad
False can not be
} -ve”
Two selection structures:
if(condition){
code; Rad < 0?
False True
}else{
Calc circle Area Print “rad
code;} can not be
Print circle Area -ve”
●
if(rad<0){
printf(“Radius can not be negative.\n”);
}else{
printf(“Area of the circle = %f.\n”,3.14*rad*rad);
}
Two selection structures:
if(condition1){
code1; condition1?
}else if(condition2){ False True
code2; condition2? code1
False True
}else if(condition3){
code2
code3; condition3?
True
False
}else{ code3
code4
code4;
}
Agenda
Logical operators.
Control flow and its types.
Two selection structures:
If
If/else
Two repetition structure.
While
Do/while
Let’s Code!!
11
Two repetition structures:
while(condition){
code; Print “entrie
Rad < 0?
} True valid rad”
False
●
while(rad<0){ Input rad
printf(“Please, enter a valid value for radius:”);
scanf(“%d”,&rad);
}
Two repetition structures:
True
do{
code;
Print “entrie
}while(condition); valid rad”
●
do{ Input rad
printf(“Please, enter a valid value for r:\n”);
scanf(“%d”,&rad);
}while(rad<0); Rad < 0?
False
Agenda
Logical operators.
Control flow and its types.
Two selection structures:
If
If/else
Two repetition structure.
While
Do/while
Let’s Code!!
14
A simple C program
●
Ask the user to enter two positive numbers
and define the relationship between them.
If one or two of the numbers is negative
ask the user to re-enter it.
The relationship is either “equal”, “greater
than” or “less than”.
A simple C program
Many thanks for
your kind listening