Beruflich Dokumente
Kultur Dokumente
1
CONTROL STATEMENT
Selection, loop, and array structure.
(04 : 09)
5.1 Understand Program Controls
Without any logical control structure statement, the instructions were executed in the same
order in which they appeared within the program. Each instruction was executed once and
only once. Thus, these programs did not include tests to determine if certain conditions are
true or false, they did not require the repeated execution of groups of statements, and did not
involve the execution of individual groups of statements on a selective basis.
The ability to control the flow of your program, letting it make decisions on what code to
execute, is valuable to the programmer.
For examples, a realistic C program may require that a logical test be carried out at some
particular point within the program.
Branching - Depending on the outcome of the logical test, one of several possible
actions will then be carried out.
Selection A special kind of branching, in which one group of statements is
selected from several available groups.
Looping In addition, the program may require that a group of instructions be executed
repeatedly, until some logical condition has been satisfied. Sometimes
the required number of repetitions is known in advance and sometimes
the computation continues indefinitely until the logical condition becomes
true.
2/17/2011
2
a. if
A conditional branching statement
The if statement allows you to control if a program enters a section of code or
not based on whether a given condition is true or false. One of the important
functions of the if statement is that it allows the program to select an action
based upon the user's input. For example, by using an if statement to check a
user-entered password, your program can decide whether a user is allowed
access to the program.
The structure of an if statement is as follows:
if ( expression ) statement
The expression must be
placed in parentheses.
The statement will be executed only if
the expression has a nonzero value
true,
The statement can be either simple or
compound.
Often compound statement which may
include other control statements
2/17/2011
3
To have more than one statement execute after an if statement that
evaluates to true, use braces, like we did with the body of the main function.
Anything inside braces is called a compound statement, or a block. When
using if statements, the code that depends on the if statement is called the
"body" of the if statement.
For example:
if ( TRUE ) {
/* between the braces is the body of the if statement */
Execute all statements inside the body
}
Example Program : IF Statement
#include <stdio.h>
main ()
{ int age, yrs;
printf("Type in your age: ");
scanf ("%d", &age);
if (age < 18)
{ printf ("You cannot vote yet\n");
yrs = 18 - age;
printf("You can vote in %d years.\n", yrs);
}
if (age > 18)
{ printf ("You can vote now.\n");
}
Return 0;
}
The answers:
2/17/2011
4
b. if..else
The general form of if statement which includes the else clause is
if ( expression ) statement 1 else statement 2
If the expression has a nonzero value(true), the
statement 1 will be executed. Otherwise,
statement 2 will be executed.
Sometimes when the condition in an if statement evaluates to false, it
would be nice to execute some code instead of the code executed when
the statement evaluates to true. The "else" statement effectively says that
whatever code after it (whether a single line or code between brackets) is
executed if the if statement is FALSE.
# include <stdio.h>
main()
{
int yrs, age;
printf("Type a age: ");
scanf("%d", &age);
if (age < 18)
{ printf("You cannot vote yet\n");
yrs = 18 - age;
printf("You can vote in %d years. \n", yrs);
}
else
{
printf("You can vote now.\n", yrs);
}
return 0;
}
Example Program : IFElse
The answers :
2/17/2011
5
c. nested if
One property of if else statement is that they can be nested. This means
that if statement can be target of another if statement.
In a nested else statement is associate with nearest if statement. This
association can be change by used of code block. Also you should use
proper indenting in your program to make sure your remove any visual
ambiguity.
General representation of nested if in C can be
If (condition1) statement1;
If (condition2) statement2;
If (condition3) statement3;