You are on page 1of 23

UNIT 5

Logical Structures And Basic Instructions


HAFIZUDDIN ZIRA MASRAH
15DKE07F2035 15DKE07F2036
SYAHIDAH RIZUWAN SAAD
15DKE07F2037 15DKE07F2040
NASRULLAH ASYIKIN
15DKE07F2044 15DKE07F2048
ABDUL HAKIM
15DKE07F2052
5.1 LOGICAL STRUCTURES
AND BASIC INSTRUCTIONS

 Sequence structure
 Selection structure
 Repetition or iteration structure
 Compiles and run program
5.1.1 SEQUENCE STRUCTURE
Example:
x=5 (SI)
y = 10 (S2)
Total = x*y (S3)
cout<< “\n"<< "Total =" <<Total< \n"; (S4)
5.1.2 SELECTION STRUCTURE

 if statement
 if-else statement
 Switch statement.
A. IF STATEMENT
 The basic form of the if statement is:
If (expression) statement

Example 5.2:

The output:

20 is greater then 5
B. IF-ELSE STATEMENT
 The basic form of the if-else statement is:
if (expression) statement_1, else statement_2
Example 5.3:

Example 5.4:
NESTED IF – ELSE STATEMENT
 The basic form of the if-else, statement is:
Example 5.5:
C. SWITCH STATEMENT
5.1.3 REPETITION (OR
ITERATION) STRUCTURE

 The while loop


 The do-while loop
 The for loop
THE WHILE LOOP
 The while loop repeats the body of the loop as long as
the loop condition holds. The basic form of the while
statement is as below:
while (expression) statement;
Example 5.7:
THE DO-WHILE LOOP
 The do-while loop executes a statement as long as the
loop condition holds.
 The basic form of the do-while statement as below:
Example 5.8:
THE FOR LOOP
 The for loop repeats the body of the loop as long as the
loop condition holds.
 The basic form of the for loop as below:

for (initialization; condition test; incrementation)


{
statements;
}
Example 5.9:

Program output:

1 4 9 16 25 36 49 64 81 100
COMPILES AND RUN PROGRAM
SEKIAN
TERIMA KASIH

You might also like