<<

ECE 114 - 3

Control Statements-1

Dr. Z. Aliyazicioglu Cal Poly Pomona Electrical & Computer Engineering

Cal Poly Pomona Electrical & Computer Engineering 1

Algorithms and Pseudocode

„ A procedure for solving a problem in terms of – The actions to be executed and – The order in which these actions are to be executed „ This is called an

„ Pseudocode is an artificial and informal language that helps develop . „ Similar to spoken language „ It is not executed on the computer „ Decribes only executable statement

Cal Poly Pomona ECE 114-3 2

1 Control Structure

„ MC++ has seven control structures – One sequence structure (sequential execution) – Three types of selection structure and – Three types of repetition structure „ In MC++, selection structures are created with selection statements. „ MC++ provides three types of selection statements – if selection statement – if … else selection statement – switch selection statements

Cal Poly Pomona ECE 114-3 3

Control Structure

The if selection statement is called a single-selection structure It selects or ignores a single action (group of actions)

The if…else statement is called a double selection structure It selects between two different action (group of actions)

The switch statement is called a multiple-selection structure. It select among of different actions (group of actions)

Cal Poly Pomona ECE 114-3 4

2 Control Structure

„ In MC++, repetation structures are created with repetation statement „ MC++ provides three repetition statement – while – do…while – For „ We will talk about them later

Cal Poly Pomona ECE 114-3 5

if Selection Statement

„ If selection structure perform an indicated action only when the condition is true otherwise the action is skipped „ Example: Let’s have pseudocode statement » If student’s grade is greater than or equal to 60 » Print “Passed”

if (studentGarde >=60) Console::WriteLine( S”Passed” );

if (Exp) True { studentGrade>=60 Print”Passed” ….. } False

Cal Poly Pomona ECE 114-3 6

3 The if…else Selection Structure

„ The if…else selection structure allows the to specify that different action is to be performed when condition is true and when the condition is false „ Example: false true Grade>=60 if (grade >= 60) Console:: WriteLine( S”Passed”); else Print”Failed” Print”Passed” Console:: WriteLine( S”Failed”);

Cal Poly Pomona ECE 114-3 7

Conditional Operator

„ ++ has conditional operator (?:) that is closely related to the if…else structure

Console::WriteLine (grade >=60 ? ”Passed” : ”Failed”);

Cal Poly Pomona ECE 114-3 8

4 Nested if …else Statement

„ Nested if…else structure test for multiple cases by placing if…else selection structures inside if…else selection structure if (grade >= 90) Console::WriteLine ( S”A” ); else if (grade >= 80) Console::WriteLine ( S”B” ); else if (grade >= 70) Console::WriteLine ( S”C” ); else if (grade >= 60) Console::WriteLine ( S”D” ); else Console::WriteLine ( S”F” );

Cal Poly Pomona ECE 114-3 9

Example The following example includes a compound statement in else part of the if…else structure

if (grade >= 60) Console::WriteLine ( S”Passed.”); else { Console::WriteLine ( S”Failed.”); Console::WriteLine ( S”You must take this course again.”); }

Cal Poly Pomona ECE 114-3 10

5 Example

if (x >5 Wrong if (y>5) representation Console::WriteLine ( S”x any y are <=5.”); else Console::WriteLine ( S”x is <=5.”);

if (x >5 Right if (y>5) representation Console::WriteLine ( S”x any y are <=5.”); else Console::WriteLine ( S”x is <=5.”);

Cal Poly Pomona ECE 114-3 11

Example If originally x=2, y=3, z=4, what is the value of x, y, and z after executing the following code? if (z > x) if ( y > z) { x=x+ 3; y =y-1; } else { x= y + z + 1; y++; } else x = z + y; Cal Poly Pomona ECE 114-3 12

6 The while Repetition Structure

„ A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true.

initialization initialization; while (loop test)

{ test Statements statements; increment; }

Cal Poly Pomona ECE 114-3 13

Examples:

int product = 2; while (product <= 1000) { product =2*product; Console::WriteLine ( S”Product={0},product.ToString()”); }

x=1; while (x<=20) { x=x+3; Console::WriteLine ( S”x={0},x.ToString()”); }

Cal Poly Pomona ECE 114-3 14

7 Example

// Class Average with counter-controlled repetation

#include "stdafx.h"

#using

using namespace System;

int _tmain() { int total, // sum of grades gradeCounter, // number of grades to be entered gradeValue, // grade value average; //average of grades // initialization phase total = 0; // clear total gradeCounter = 1; // prepare to loop

Cal Poly Pomona ECE 114-3 15

Example (Cont.)

// processing phase while (gradeCounter <=10) { Console::WriteLine( S"Enter integer grade : "); gradeValue =Int32::Parse( Console::ReadLine() );

total = total + gradeValue; //add garde to total gradeCounter = gradeCounter + 1; //increment counter } // termination phase average = total / 10; //integer division Console::WriteLine( S"Class average is {0}, average.ToString()");

return 0; }

Cal Poly Pomona ECE 114-3 16

8