0% found this document useful (0 votes)
55 views

Fundamentals of Programming: Course Outline

This document outlines a course on fundamentals of programming. It discusses control structures including single-selection if statements, double-selection if-else statements, and multiple-selection switch statements. Examples are provided for each type of control structure. The objectives are for students to understand algorithms and represent them using flowcharts or pseudocode, and to develop algorithms to solve simple problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Fundamentals of Programming: Course Outline

This document outlines a course on fundamentals of programming. It discusses control structures including single-selection if statements, double-selection if-else statements, and multiple-selection switch statements. Examples are provided for each type of control structure. The objectives are for students to understand algorithms and represent them using flowcharts or pseudocode, and to develop algorithms to solve simple problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Course Outline

Fundamentals of
Programming
Sarjana S1MR

Tahun Ajaran 2018/2019


Week 0
04

 Arie Satia Dharma, S.T., M.Kom


 Institut Teknologi Del – Laguboti, Toba Samosir
Outline

Control Structures
If Single-Selection Statement
If…Else Double-Selection Statement
Switch Multiple-Selection Statement

2 Arie Satia Dharma 01/10/2018


Objectives

 Mahasiswa mengerti tentang algoritma dan


representasi sebuah algoritma dalam bentuk
flowchart maupun pseudocode.
 Mahasiswa mampu menyusun algoritma penyelesaian
masalah-masalah sederhana.

3 Arie Satia Dharma 01/10/2018


Control Structures

4 Arie Satia Dharma 01/10/2018


Control Structures

 Statements in an application are executed one after the


other in the order in which they’re written -> sequential
execution.
 Various programming language statements enable you
to specify that the next statement to execute is not
necessarily the next one in sequence -> transfer of
control.
 In 1960s, it became clear that the indiscriminate use of
transfers of control was the root of much difficulty
experienced by software development groups -> goto
statement.

5 Arie Satia Dharma 5/9/2016


Control Structures –Cont’d

 Structured programming became almost


synonymous with “go to elimination.”
 Three control structures:
 The sequence structure.
 The selection structure.
 The repetition structure.

6 Arie Satia Dharma 5/9/2016


Sequence Structure in C#

 The computer executes C# statements one after


the other in the order in which they’re written.
 The UML activity diagram below illustrates a typical
sequence structure in which two calculations are
performed in order.

7 Arie Satia Dharma 01/10/2018


Selection Structures in C#

 C# has three types of selection structures (refer as


selection statements):
 The if statement either performs (selects) an action if a
condition is true or skips the action if the condition is
false.
 The if…else statement performs an action if a condition is
true or performs a different action if the condition is false.
 The switch statement performs one of many different
actions, depending on the value of an expression.

8 Arie Satia Dharma 01/10/2018


Selection Structures in C# -Cont’d

 The if statement is called a single-selection


statement because it selects or ignores a single
action.
 The if…else statement is called a double-selection
statement because it selects between two different
actions.
 The switch statement is called a multiple-selection
statement because it selects among many different
actions.

9 Arie Satia Dharma 01/10/2018


Repetition Structures in C#

 C# provides four repetition structures, which from


this point forward we shall refer to as repetition
statements: while, do…while, for and foreach
statements. -> discuss in next week.
 The while, for and foreach statements perform the
action in their bodies zero or more times—if the
loop-continuation condition is initially false, the
action will not execute.
 The do…while statement performs the action in its
body one or more times.

10 Arie Satia Dharma 01/10/2018


If Single-Selection Statement

11 Arie Satia Dharma 01/10/2018


Single Selection Statement

 Applications use selection statements to choose among


alternative courses of action.
 For example, suppose that the passing grade on an
exam is 60. The C# statement determines whether the
condition grade >= 60 is true or false.
 If the condition is true, "Passed"is displayed, and the
next C# statement in order is performed.
 If the condition is false, the output statement is
ignored, and the next C# statement in order is
performed.

12 Arie Satia Dharma 01/10/2018


Single Selection Statement –Cont’d

 The if statement is a single-entry/single-exit control


statement. The activity diagrams contain initial
states, transition arrows, action states that indicate
actions to perform and decision symbols that
indicate decisions to be made, and final states.

13 Arie Satia Dharma 01/10/2018


Example-1

14 Arie Satia Dharma 01/10/2018


Example-2

15 Arie Satia Dharma 01/10/2018


If…else Double-Selection
Statement

16 Arie Satia Dharma 01/10/2018


Double Selection Statement

 The if single-selection statement performs an


indicated action only when the condition is true;
otherwise, the action is skipped.
 The if…else double-selection statement allows you
to specify an action to perform when the condition
is true and a different action when the condition is
false.

17 Arie Satia Dharma 01/10/2018


Double Selection Statement –Cont’d

 For example, the C# statement displays “Passed” if


grade >= 60 is true, but displays “Failed” if grade <
60 is true. In either case, after displaying occurs,
the next statement in sequence is performed.

18 Arie Satia Dharma 01/10/2018


Example-3

19 Arie Satia Dharma 01/10/2018


Conditional Operator

 C# provides the conditional operator (?:), which can be


used in place of an if…else statement.
 The first operand (to the left of the ?) is a boolean
expression (i.e., an expression that evaluates to a bool-
type value—true or false),
 The second operand (between the ? and :) is the value
of the conditional expression if the boolean expression
is true,
 The third operand (to the right of the :) is the value of
the conditional expression if the boolean expression is
false.

20 Arie Satia Dharma 01/10/2018


Conditional Operator –Cont’d

 For example, the statement

 displays the value of WriteLine’s conditional-


expression argument. The conditional expression in
the preceding statement evaluates to the string
"Passed“ if the boolean expression grade >= 60 is
true and evaluates to the string "Failed“ if the
boolean expression is false.

21 Arie Satia Dharma 01/10/2018


Nested if…else Statements

 An application can test multiple cases by placing


if…else statements inside other if…else
statements to create nested if…else statements.
 For example, the following nested if…else
statement displays grades from A to E.

22 Arie Satia Dharma 01/10/2018


Nested if…else Statements

 If grade is greater than or equal to 90, the first four


conditions will be true, but only the statement in
the if-part of the first if…else statement will
execute.
 After that statement executes, the else-part of the
“outermost” if…else statement is skipped.
 Most C# programmers prefer to write the
preceding if…else statement as

23 Arie Satia Dharma 01/10/2018


Nested if…else Statements

 The two forms are identical except for the spacing


and indentation, which the compiler ignores. The
latter form is popular because it avoids deep
indentation of the code to the right

24 Arie Satia Dharma 01/10/2018


switch Multiple Selection
Statement

25 Arie Satia Dharma 01/10/2018


Switch Multiple-Selection

 C# provides the switch multiple-selection


statement to perform different actions based on
the possible values of an expression.
 Each action is associated with the value of a
constant integral expression or a constant string
expression that the variable or expression on which
the switch is based may assume.

26 Arie Satia Dharma 01/10/2018


Switch Multiple-Selection –Cont’d

27 Arie Satia Dharma 01/10/2018


Switch Multiple-Selection –Cont’d

 When using the switch statement, remember that


the expression after each case can be only a
constant integral expression or a constant string
expression—that is, any combination of constants
that evaluates to a constant value of an integral or
string type.
 An integer constant is simply an integer value (e.g.,
–7, 0 or 221). In addition, you can use character
constants like are specific characters in single
quotes, such as 'A', '7'or '$'.

28 Arie Satia Dharma 01/10/2018


Example-4

29 Arie Satia Dharma 01/10/2018


break & continue Statement

 In addition to selection and repetition statements,


C# provides statements break and continue to alter
the flow of control.
 The next section showed how break can be used to
terminate a switch statement’s execution and how
to use break to terminate any repetition statement.

30 Arie Satia Dharma 01/10/2018


Terima Kasih

31 Arie Satia Dharma 01/10/2018


Daftar Pustaka

1. Hejlsberg , Anders et al. The C#


Programming Language, Fourth Edition.
Boston: Pearson Education, 2011.

32 Arie Satia Dharma 01/10/2018

You might also like