Uploaded by hosamshalghom

lech 2

advertisement
Programming in C language
Lecture 2:Decision Making in C
Omymah sarkez
Fall_2023
Decision Making in C
• Decision making structures require that the programmer specify one
or more conditions to be evaluated or tested by the program.
• statements to be executed if the condition is true
• other statements to be executed if false .
• any non-zero and non-null values as true.
• zero or null, then it is assumed as false value.
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
if and if...else statement
• An if statement consists of a boolean expression followed
by one or more statements
• Syntax: if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is
true */
}
• Flow Diagram
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
Example
Output:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
a is not less than 20;
value of a is : 100
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
The ? : Operator
• can be used to replace if...else statements
• Syntax
Exp1 ? Exp2 : Exp3;
• If Exp1 is true _ Exp2
• If Exp1 is false _ Exp3
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
switch statement
• A switch statement allows a variable to be tested for equality
against a list of values
• Each value is called a case
Syntax switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
rules apply to a switch statement
• a switch statement : integral or enumerated type
• case statements: can have any number within a switch
• When a break statement is reached, the switch terminates,
• that case will execute until a break statement is reached.
• it can have default case, which must appear at the end of
the switch
• The default case can be used for performing a task when
none of the cases is true
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
Example
#include <stdio.h>
1
int main ()
{
/* local variable definition */
char grade = 'B';
switch(grade)
{
case 'A’ :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
2
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade
);
return 0;
}
Well done
Your grade is B
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
C Loops : [ while -do...while - for]
1. While loop in C
A while loop statement in C programming language repeatedly executes a target
statement as long as a given condition is true.
• Syntax
while(condition)
{
statement(s);
}
Flow Diagram
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}
Output:
value
value
value
value
value
value
value
value
value
value
of
of
of
of
of
of
of
of
of
of
a:
a:
a:
a:
a:
a:
a:
a:
a:
a:
10
11
12
13
14
15
16
17
18
19
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
C Loops : [ while -do...while - for]
2. for loop in C
Flow Diagram
allows to efficiently write a loop that needs to execute a
specific number of times.
Syntax
for ( init; condition; increment )
{
statement(s);
}
init step is executed first
condition If it is true, the body of the loop is executed.else
the control jumps to statement after the for loop.
increment update any loop control variables. This
statement can be left blank, as long as a semicolon appears
after the condition.
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
Example
#include <stdio.h>
int main ()
{
/* for loop execution */
for( int a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d\n", a);
}
return 0;
}
Output:
value
value
value
value
value
value
value
value
value
value
of
of
of
of
of
of
of
of
of
of
a:
a:
a:
a:
a:
a:
a:
a:
a:
a:
10
11
12
13
14
15
16
17
18
19
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
C Loops : [ while -do...while - for]
3. do...while loop in C
Flow Diagram
Unlike while loops do...while checks its condition at the
bottom of the loop.
Syntax
do
{
statement(s);
}while( condition );
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Output:
value
value
value
value
value
of
of
of
of
of
a:
a:
a:
a:
a:
10
11
12
13
14
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
‫‪END‬‬
‫جامعة الزاوية كلية الهندسة _ قسم الحاسوب‬
Download