23
Chapter 4 C Program Control

Chapter 4 C Program Control - Bilgisayar Mühendisliği …compe.hku.edu.tr/Dosyalar/16112015142657-.pdfThe following program prints out integer numbers from 1 to 10. Page 131 This

  • Upload
    lydat

  • View
    223

  • Download
    5

Embed Size (px)

Citation preview

Chapter 4

C Program Control

Objectives of this chapter:Repetitions will be considered in greater detail

• for .. repetition

• do … while repetition

Also multiple selection switch … case statement will be learned.All these are called as program control statements

Usage of “break” and “continue” statements in the program control statements.

•break

•continue

Usage of logical operators (AND – OR) for combining conditions

Repetition Essentials:

Most of the programs involves repetitions or looping.

The instructions placed in a loop executed repeatedly while the condition of repetition remains true.

There are two types of repetitions as mentioned in Chap 3

• Counter controlled repetitions

• Sentinel controlled repetitions

Example: Counter-Controlled Repetitions

The following program prints out integer numbers from 1 to 10.

Page 131

This program can be also written easily using for … repetition Notice that the starting value and end value for the counter is known.

Page 132All counter-controlled repetitions can be handled by using for repetitions

Example: Rewrite the program that produces the following output using “for” repetition.*

Page 136Example - Sum of Even Numbers: for Repetition

Write a C program that determines the sum of the even numbers from 2 to 100. *

Chapter 3 Exercise - Page 124

We have already answered this program using counter-controlled repetition with while statement.

Let’s now rewrite this program using for repetition statement.

Switch .. Case Statements: Multiple Selection StatementsAs an alternative to if .. else when dealing with multi-branching in C programming is the switch … case controls. This statement also known as multiple choice statements. C-Syntax: Flow-Chart:

switch ( test ) {

case 1 : // Process for test = 1

...

break;

.

case 5 : // Process for test = 5

...

break;

default : // Process for all other cases.

...

}

Page 139

Example: Using Switch … Case Statement

Write a C program that reads 10 letter grades for a quiz results. Then writes out the sum of the each letter grades as shown below. The terminal output of the program should look as below when executed.

The program’s output should look like as follow:

Page 140

Page 145

“Do … while” is another type of repetition statement used in C. It is similar to while repetitionwhile Repetition: Loop condition is tested at the beginning of the loopdo … While Repetition: Loop condition is tested at the end of the loop

C-Syntax: C-Syntax:while (condition) do{ {instructions instructions} }

while (condition);

Example: Using Do … While Repetition

Write a C program that prints out the numbers from 1 to 10.

The output of the program should be as shown below.

1 2 3 4 5 6 7 8 9 10

*

Page 146

Page 146break and continue statements are used to alter the flow of the program.

break: Break statement in a switch … case used to skip remainder of statement. Break also can be used in repetition statements. The break statement are used to escape early from a repetition/loop. So if break statement used in a repetition – it terminates the loop immediately.

* Page 147

Page 147break and continue statements are used to alter the flow of the program.

continue: the continue usually used in repetition statements to skip the rest of the actions.

Unlike break statements terminating the repetition, it only skips the rest of the actions. *

Page 147

Page 147So far we have studied programs including simple condition e.g. relation and equality operators (< , >, <=, <=, ==, and !=).

C and other programming languages also provides (AND and OR) logical operators to form more complex conditions by combining simple conditions.

C-Syntax&& : is AND logical operator.|| : is OR logical operator.

Logical operators are written between the simple conditions to be combinedCondition1 && Condition2 : two conditions combined with AND Condition1 || Condition2 : two conditions combined with OR

Example: If ( x > 99 && x <= 999 ) printf(“This is a three digit positive integer”);

If (x > 0 && y > 0) printf (“This vector is in first quadrant.” );

If (gender == 1 && age > 65) seniorFemale++;

TRUTH TABLES FOR LOGICAL OPERATORS ( && and ||)The outcome of a simple condition in C is “1” for true and “0” for false. Then if or any repetition is executed upon this output value.

x==4 gives “1” if true or gives “0” if false.

Tables below shows all possible input values of “0” or “1” and output values for && and II operators. These tables are called truth tables.

Example: int x=123;if ( x > 0 && x <= 999 ) printf(“This is a three digit positive integer”);

Question:What happens if we write x==4 in a line rather then x=4?What happens if we write if (x=4) instead of x==4?

Page 146

Page 162

Page 166

Below is the output of the program when executed:

Or another output of the program when it is executed for only 3 values

5 100 200 300 400 500Sum of the 5 values is 1500

3 50 200 60Sum of the 3 values is 310

Page 167

Below is the output of the program when executed:

Enter the number of integers to be processed: 5Enter an integer: 372Enter next integer: 920Enter next integer: 73Enter next integer: 8Enter next integer: 3433The smallest integer is: 8

Page 167

Below is the output of the program when executed:

Product of the odd integers from 1 to 15 is: 2027025

Page 167

Below is the output of the program when executed:

N N!1 12 23 64 245 120

Page 169