2. Flow of Control
• Unless specified otherwise, the order of statement execution through
a function is linear: one statement after another in sequence
• Some programming statements allow us to:
• decide whether or not to execute a particular statement
• execute a statement over and over, repetitively
• These decisions are based on boolean expressions (or conditions) that
evaluate to true or false
• The order of statement execution is called the flow of control
3. Conditional Statements
• A conditional statement lets us choose which statement will be
executed next
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• The C conditional statements are the:
• if statement
• if-else statement
• switch statement
4.
5. The if-else Statement
• An else clause can be added to an if statement to make an if-else
statement
• If the condition is true, statement1 is executed; if the condition is false, statement2 is executed
• One or the other will be executed, but not both
6. Boolean Expressions
• A condition often uses one of C's equality operators or relational operators, which all return boolean results:
• == equal to
• != not equal to
• < less than
• > greater than
• <= less than or equal to
• >= greater than or equal to
• Note the difference between the equality operator (==) and the assignment operator (=)
7. Nested if Statements
• The statement executed as a result of an if statement or else clause
could be another if statement
• These are called nested if statements
• An else clause is matched to the last unmatched if (no matter what
the indentation implies)
• Braces can be used to specify the if statement to which an else clause
belongs
11. Example
#include <stdio.h>
int main() /* Most important part of the
program! */
{
int age; /* Need a variable... */
printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in
age */
if ( age < 100 ) { /* If the age is less than
100 */
printf ("You are pretty young!n" ); /* Just to show
you it works... */
}
else if ( age == 100 ) { /* I use else just to
show an example */
printf( "You are oldn" );
}
else {
printf( "You are really oldn" ); /* Executed if no
other statement is */
}
return 0;
}
#include<stdio.h>
main()
{
int grade;
char student[25],section[25];
clrscr();
printf(“Enter the name of a student: “);
scanf(“%s”, &student);
printf(“Enter the section of a student: “);
scanf(“%s”, §ion);
printf(“Enter the grade of a student: “);
scanf(“%d”, &grade);
if(grade>75)
printf(“PASSED”);
else
print(“FAILED”);
getch();
}
17. #include <stdio.h>
int main()
{
int a, b, c, max;
printf("Enter Three Integersn");
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
if(a > c)
max = a;
else
max = c;
} else
{
if(b > c)
max = b;
else
max = c;
}
printf("Maximum Number is = %dn", max);
return 0;
}