prg i

4
PRG I/PRG II 02/06/09 Lecture Notes By Ian Jasper Mangampo Control Structures - control  structures direct the flow of execution of statements or instructions in a computer program. - There are 3 types of control Structures, Sequential  Structure, Selection Structure and Repetition Structure.  1. Sequential Structure - Sequential structure is the most basic of the three. - Basically all programs are done sequentially. - A program runs, executing the each instruction, probably skipping or repeating instructions coded on it as necessary, from the first till the last. Example: 1 void main() 2 { 3 int A, B, C; 4 A = 5; 5 B = 10; 6 C= B/A; 7 cout<<”Quotient of B and A is “<<C; 8 } 2. Selection Structure - Selection structure gives the program a way of rerouting or redirecting the flow of execution of an instruction by providing two or more paths of execution, given 1 or more conditions. - In this structure some instructions may be skipped given that certain conditions are met. Types of Selection Structures a. if Selection Structure – provides a means of skipping a statement by setting out conditions to determine whether instructions are to be executed or not. Syntax: 1 st form: if (Boolean_expression/Condition) Instruction; 2 nd form: if (Boolean_expression/Condition) { Instruction 1; Instruction 2; . . . Instruction N; } Example 1: void main() { int grade = 95; if (grade >= 75) cout<<”YOU PASSED”<<endl; } Example 2: void main() { int grade = 95; if (grade >= 75) { cout<<”You’re grade is greater than 75”<<endl; cout<<”YOU PASSED”<<endl; }

Upload: ian-jasper-mangampo

Post on 08-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PRG I

8/7/2019 PRG I

http://slidepdf.com/reader/full/prg-i 1/4

PRG I/PRG II 02/06/09

Lecture Notes By Ian Jasper Mangampo

Control Structures

- control structures direct the flow of execution of statements or instructions in a computer program.

- There are 3 types of control Structures, Sequential  Structure, Selection Structure and Repetition Structure. 

1. Sequential Structure

- Sequential structure is the most basic of the three.

- Basically all programs are done sequentially.

- A program runs, executing the each instruction, probably skipping or repeating instructions coded on it as necessary

from the first till the last.Example:

1 void main()

2 {

3 int A, B, C;

4 A = 5;

5 B = 10;

6 C= B/A;

7 cout<<”Quotient of B and A is “<<C;

8 }

2. Selection Structure- Selection structure gives the program a way of rerouting or redirecting the flow of execution of an instruction by

providing two or more paths of execution, given 1 or more conditions.

- In this structure some instructions may be skipped given that certain conditions are met.

Types of Selection Structures

a. if Selection Structure – provides a means of skipping a statement by setting out conditions to determine whether

instructions are to be executed or not.Syntax:

1st form:

if (Boolean_expression/Condition)

Instruction;

2nd form:

if (Boolean_expression/Condition)

Instruction 1;

Instruction 2;

.

.

.

Instruction N;

}

Example 1:

void main()

{

int grade = 95;

if (grade >= 75)cout<<”YOU PASSED”<<endl;

}

Example 2:

void main()

{

int grade = 95;

if (grade >= 75)

{

cout<<”You’re grade is greater than 75”<<endl;

cout<<”YOU PASSED”<<endl;

}

Page 2: PRG I

8/7/2019 PRG I

http://slidepdf.com/reader/full/prg-i 2/4

}

b. if/else Selection Structure – provides a means of selecting from two paths of execution, one if the Condition is

TRUE and the other when the condition is FALSE .Syntax:

1st form:

if (Boolean_expression/Condition)

Instruction if Condition is true;

else Instruction if Condition is False;

2nd form:

if (Boolean_expression/Condition)

//Instructions when condition is True

}

else

//Instructions when condition isFalse

}

Example:

void main()

{ int grade = 95;

if (grade >= 75)

cout<<”YOU PASSED”<<endl;

else

cout<<”YOU FAILED”<<endl;

}

c. Multiple Selection Structure– provides a means of selecting from three or more paths of execution, each when the

respective Conditions stated becomes TRUE , and an optional statement once no Condition is TRUE .

Syntax:

if (Condition 1)

//Instruction/s if Condition 1 is true;else if (Condition 2)

//Instruction/s if Condition 2 is true;else if (Condition 3)

//Instruction/s if Condition 3 is true;.

.

.

else if (Condition N)

// Instruction/s if Condition N is true;

else

Instruction/s if none of the above Conditions are true;

Example:

void main()

{

int grade;cout<<”Give a grade: “;

cin>>grade;

if ((grade >= 65) && (grade < 75))

cout<<”You Failed”<<endl;

else if ((grade >= 75) && (grade < 85))

cout<<”Good.. You Passed”<<endl;

else if ((grade >= 85) && (grade <= 95))

cout<<”Excellent.. You Passed”<<endl;

else

cout<<”Not a valid grade”<<endl;

}

Page 3: PRG I

8/7/2019 PRG I

http://slidepdf.com/reader/full/prg-i 3/4

d. Switch Case Structure – almost similar  to the previous multiple selection structure, only that it uses certain cases

which a value in the case must meet to execute its corresponding instruction/s.

Syntax:

switch (case_variable)

case [first]:

//Instruction/s for first case;

break;

case [Second]://Instruction/s for second case;

break;

case [Third]:

//Instruction/s for third case;

break;

.

.

.

case [N]://Instruction/s for Nth case;

break;

default: //Default Instruction if no case is True;

}

Example:

void main(){

char choice;

cout<<”What do you want to do: ”;

cin>>choice;

switch (choice)

{ case ‘1’: cout<<”ONE”;

break;

case ‘2’: cout<<”TWO”;

break;

case ‘3’: cout<<”THREE”;

break;

default: cout<<”Default Clause”;

}

}

3. Repetition Structure

- gives the program a way of repeating instructions or groups of instructions. 

a. For Loop

- a countered-controlled repetition. Contains an initialization of the control variable, the logical condition

which controls the repetition and a decrement or increment the control to increment or decrement the control

variable.Syntax:

Form 1:

for(<initialization>; <condition>; <increment/decrement control>)

//Statement;

Form2:

for(<initialization>; <condition>; <increment/decrement control>)

//Statement/s;

}

Example:

void main(){

for(int i=0; i<10; i++)

{

cout<< i <<endl;

}

}

Page 4: PRG I

8/7/2019 PRG I

http://slidepdf.com/reader/full/prg-i 4/4