En Course Tests
En Course Tests
Mohamed MESSABIHI
mohamed.messabihi@gmail.com
University of Tlemcen
Department of Computer Science
https://sites.google.com/site/informatiquemessabihi/
Objectves
Example:
if ( condition )
{
// List of instructions ;
}
else
{
// Another block of instructions ;
}
Conditions
Example:
int age = 20;
Example:
int main () {
int x = 5;
int y = 10;
int z = 5;
int i1 = 1;
int i2 = 0;
if ( x == y && x == z ) {
printf ( " x is equal to y and z .\ n " ) ;
} else if ( x == y || x == z ) {
printf ( " x is equal to either y or z ( or both ) .\ n " ) ;
} else if (!( x == y ) ) {
printf ( " x is not equal to y .\ n " ) ;
}
if ( value > 0) {
printf ( " The value is positive .\ n " ) ;
}
if ( value < 0) {
printf ( " The value is negative .\ n " ) ;
}
if ( value == 0) {
printf ( " The value is zero .\ n " ) ;
}
return 0;
}
Nested conditional statements 2/3
• A nested conditional statement refers to the practice of placing one
if-else statement inside another if-else statement.
• This is done to create more complex and structured decision-making
processes based on multiple conditions.
Example:
int main () {
int value ;
if ( value > 0) {
printf ( " The value is positive .\ n " ) ;
} else if ( value < 0) {
printf ( " The value is negative .\ n " ) ;
} else {
printf ( " The value is zero .\ n " ) ;
}
return 0;
}
Nested conditional statements 3/3
• A nested conditional statement refers to the practice of placing one
if-else statement inside another if-else statement.
• This is done to create more complex and structured decision-making
processes based on multiple conditions.
Example:
int main () {
int value ;
if ( value > 0) {
printf ( " The value is positive .\ n " ) ;
} else {
if ( value < 0) {
printf ( " The value is negative .\ n " ) ;
} else {
printf ( " The value is zero .\ n " ) ;
}
}
return 0;
}
The switch Statement (1/3)
The switch statement allows multiple tests on the values of the same
variable.
Syntax:
switch ( Variable ) {
case Value1 :
// List of instructions ;
break ;
case Value2 :
// List of instructions ;
break ;
case Values ... :
// List of instructions ;
break ;
default :
// List of instructions ;
}
The switch Statement (1/3)
Example :
int main () {
int choice ;
printf ( " Menu :\ n " ) ;
printf ( " 1. New Game \ n " ) ;
printf ( " 2. Load Game \ n " ) ;
printf ( " 3. Options \ n " ) ;
printf ( " 4. Quit \ n " ) ;
printf ( " Enter your choice : " ) ;
scanf ( " % d " , & choice ) ;
switch ( choice ) {
case 1:
printf ( " Starting a new game ...\ n " ) ;
break ;
case 2:
printf ( " Loading a saved game ...\ n " ) ;
break ;
case 3:
printf ( " Opening options menu ...\ n " ) ;
break ;
case 4:
printf ( " Quitting the application ...\ n " ) ;
break ;
default :
printf ( " Invalid choice . Please select a valid option .\ n " ) ;
break ;
}
}
A Shorter Way to Test
You can use a more concise structure to perform tests.
(condition) ? instruction if true : instruction if false
• The condition should be enclosed in parentheses.
• When the condition is true, the left instruction is executed.
• When the condition is false, the right instruction is executed.
• Additionally, the ?: structure returns the value resulting from the
test.
Example:
( average >= 10) ? printf ( " Admitted " ) : printf ( " Failed " ) ;
Example 1:
if (1)
{
printf ( " This is true " ) ;
}
else
{
printf ( " This is false " ) ;
}
An example to conclude
Example :
int main () {
int hour = 0;
return 0;
}