EC-237 Computer System & Programming: Lectures 1-3, Cheat Sheet of Computer Science

This document collection contains the first three lectures for the course EC-237 Computer System & Programming . The slides introduce foundational concepts of computer hardware, software, and C++ programming.Key Topics Covered:Lecture 1: Introduction Overview of computer system components, hardware, and software.Introduction to programming languages, compiler translation, and the history of C/C++.The six basic phases of a C++ program environment: edit, preprocess, compile, link, load, and execute.Writing a simple first C++ program using standard input (cin) and standard output (cout).Lecture 2: Introduction to C++ Programming, Input/Output and Operators Understanding variables, memory concepts, and common data types like int, char, and double.Performing calculations using arithmetic operators and understanding the rules of operator precedence.Decision making in code

Typology: Cheat Sheet

2024/2025

Uploaded on 03/06/2026

unknown user
unknown user 🇵🇰

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EC-237 Computer System & Programming
Lecture 3
Control Statements
Dr. Arslan Shaukat
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download EC-237 Computer System & Programming: Lectures 1-3 and more Cheat Sheet Computer Science in PDF only on Docsity!

EC-237 Computer System & Programming

Lecture 3

Control Statements

Dr. Arslan Shaukat

Control Structures

▪ Sequential execution

– Statements executed in order

▪ Transfer of control

– Next statement executed not next one in sequence

▪ 3 control structures

– Sequence structure

• Programs executed sequentially by default

– Selection structures

• if, if/else, switch

– Repetition structures

• while, do/while, for

if Selection Structure

▪ Selection structure

– Choose among alternative courses of action

– Pseudocode example:

If student’s grade is greater than or equal to 60

Print “Passed”

– If the condition is true

• Print statement executed, program continues to next statement

– If the condition is false

• Print statement ignored, program continues

– Indenting makes programs easier to read

• C++ ignores whitespace characters (tabs, spaces, etc.)

if Selection Structure

▪ Translation into C++

If student’s grade is greater than or equal to 60

Print “Passed”

if ( grade >= 60 )

cout << "Passed";

▪ Diamond symbol (decision symbol)

– Indicates decision is to be made

– Contains an expression that can be true or false

• Test condition, follow path

if/else Selection Structure

▪ if

– Performs action if condition true

▪ if/else

– Different actions if conditions true or false

▪ Pseudocode

if student’s grade is greater than or equal to 60

print “Passed”

else

print “Failed”

▪ C++ code

if ( grade >= 60 )

cout << "Passed";

else

cout << "Failed";

Selection Structure

▪ Ternary conditional operator ( ?: )

– Three arguments (condition, value if true , value if false )

▪ Code could be written:

cout << ( grade >= 60? “Passed” : “Failed” ); false true print “Failed” print “Passed” grade >= 60

Condition Value if true Value if false

if/else Selection Structure

▪ Example

if ( grade >= 90 ) // 90 and above

cout << "A";

else if ( grade >= 80 ) // 80- 89

cout << "B";

else if ( grade >= 70 ) // 70- 79

cout << "C";

else if ( grade >= 60 ) // 60- 69

cout << "D";

else // less than 60

cout << "F";

if/else Selection Structure

▪ Compound statement

– Set of statements within a pair of braces

if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n";

– Without braces,

cout << "You must take this course again.\n";

always executed

▪ Block

– Set of statements within braces

Dangling Else Problem

if ( x > 5)

if ( y > 5)

cout << “x and y are > 5”;

else

cout << “x is <= 5”;

Dangling Else Problem

if ( x > 5)

if ( y > 5)

cout << “x and y are > 5”;

else

cout << “x is <= 5”;

else associated with the first if

while Repetition Structure

▪ Repetition structure

– Action repeated while some condition remains true

– Psuedocode

while there are more items on my shopping list

Purchase next item and cross it off my list

– while loop repeated until condition becomes false

▪ Example

int product = 2;

while ( product <= 1000 )

product = 2 * product;

Formulating Algorithms (Counter-Controlled

Repetition)

▪ Counter-controlled repetition

– Loop repeated until counter reaches certain value

▪ Definite repetition

– Number of repetitions known

▪ Example

A class of ten students took a quiz. The grades (integers in the

range 0 to 100) for this quiz are available to you. Determine the

class average on the quiz.

Formulating Algorithms (Counter-Controlled

Repetition)

▪ Pseudocode for example:

Set total to zero

Set grade counter to one

While grade counter is less than or equal to ten

Input the next grade

Add the grade into the total

Add one to the grade counter

Set the class average to the total divided by ten

Print the class average

▪ Next: C++ code for this example