0% found this document useful (0 votes)
51 views

t05CProgramControlStandardStatements Pps

This document discusses different control structures in C programming including selection (if/else) and repetition (while). It provides examples of using if/else statements to choose between alternatives based on a condition being true or false. It also demonstrates the use of a while loop with a counter variable to repeatedly execute a block of code a set number of times. The while loop continues iterating as long as the counter variable is less than or equal to a specified maximum value.

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

t05CProgramControlStandardStatements Pps

This document discusses different control structures in C programming including selection (if/else) and repetition (while). It provides examples of using if/else statements to choose between alternatives based on a condition being true or false. It also demonstrates the use of a while loop with a counter variable to repeatedly execute a block of code a set number of times. The while loop continues iterating as long as the counter variable is less than or equal to a specified maximum value.

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Computer and Information Science,

School of Science, IUPUI

CSCI 230

Program Control
- Standard C Statements
Dale Roberts, Lecturer
IUPUI
[email protected]

Dale Roberts

Outline
This Topic Introduces
selection structure
if
if/else

repetition control structures


while

Dale Roberts

Selection Structure: if
Selection structure:
Used to choose among alternative courses of action
Pseudocode:
If (students grade is greater than or equal to 60)
Print Passed

If condition true
Print statement executed and program goes on to next statement
If false, print statement is ignored and the program goes onto the
next statement
Indenting makes programs easier to read
C ignores whitespace characters

Pseudocode statement in C:
if ( grade >= 60 )
printf( "Passed\n" );
C code corresponds closely to the pseudocode
Dale Roberts

The if Selection Structure (cont.)


A decision can be made on any expression.
zero - false
nonzero - true
Example:

(3 4) is true

grade >= 60

true

print Passed

false

Dale Roberts

Selection Structure: if/else


if/else
if: only performs an action if the condition is true
if/else: Specifies an action to be performed both when the
condition is true and when it is false

Pseudocode:
If (students grade is greater than or equal to 60)
Print Passed
else
Print Failed

Note spacing/indentation conventions

C code:
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");

Dale Roberts

The if/else Selection Structure (cont.)


Ternary conditional operator (?:)
Takes three arguments (condition, value if true, value if false)
Creates an if/else expression. Recall that expressions are
computations that yield a single value.
Our pseudocode could be written:
printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );

Or it could have been written:


grade >= 60 ? printf( Passed\n ) : printf( Failed\n
);

Dale Roberts

The if/else Selection Structure


Compound statement:
Set of statements within a pair of braces
Example:
if ( grade
printf(
else {
printf(
printf(
}

>= 60 )
"Passed.\n" );
"Failed.\n" );
"You must take this course again.\n" );

Without the braces,


if ( grade >= 60 )
printf( "Passed.\n"
else
printf( "Failed.\n"
printf( "You must take

);
);
this course again.\n" );

the statement
printf( "You must take this course again.\n" );

would be executed under every condition.

Dale Roberts

The Essentials of Repetition


Loop
Group of instructions computer executes repeatedly while
some condition remains true

Counter-controlled repetition
Definite repetition: know how many times loop will execute
Control variable used to count repetitions

Sentinel-controlled repetition
Indefinite repetition
Used when number of repetitions not known
Sentinel value indicates "end of data

Dale Roberts

Essentials of Counter-Controlled Repetition


Counter-controlled repetition requires
The name of a control variable (or loop counter)
The initial value of the control variable
A condition that tests for the final value of the control variable (i.e., whether
looping should continue)
An increment (or decrement) by which the control variable is modified each
time through the loop
Example:
int counter = 1;
/* initialization */
while ( counter <= 10 ) {
/* repetition condition */
printf( "%d\n", counter );
++counter;
/* increment */
}

The statement

int counter = 1;
Names counter
Declares it to be an integer
Reserves space for it in memory
Sets it to an initial value of 1
This is not an executable statement, it is a declaration.

Dale Roberts

Repetition Structure: while


1
2
3
4
5
6
7 {
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24
25
26
27
}

/* Fig. 3.6: fig03_06.c


Class average program with
counter-controlled repetition */
printf( "Enter grade, -1 to end: " );
#include <stdio.h>
scanf( "%d", &grade );
while ( grade != -1 ) {
int main()
total = total + grade;
counter = counter + 1;
int counter, grade, total, average;
printf( "Enter grade, -1 to end: " );
scanf( "%d", &grade );
}
/* termination phase */
/* initialization phase */
if
(
counter
!= 0 ) {
total = 0;
average
=
( float ) total / counter;
counter = 1;
printf( "Class average is %.2f", average );
}
/* processing phase */
else
while ( counter <= 10 ) {
printf( "No grades were entered\n" );
printf( "Enter grade: " );
scanf( "%d", &grade );
total = total + grade;
Enter grade: 98
Program
Output:
counter = counter + 1;
Enter grade: 76
}
Enter grade: 71
Enter grade: 87
/* termination phase */
Enter grade: 83
printf( "Class average is %d\n", average );
Enter grade: 90
Enter grade: 57
return 0;
/* indicate program ended successfully */
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81

Dale Roberts

You might also like