Similar presentations:
Principles of programming
1.
PRINCIPLES OF PROGRAMMINGInternational Black Sea University
Faculty of Business and Technology
Lecturer: Nika Narushvili
1ST WEEK
2.
AGENDA• Managing operators and branching structures
• Control flow
• Selection operators - if, if-else
• Nested if and if-else statements
• Selection operator – switch statement
3.
ADDING NUMBERS TO OUTPUT4.
CONTROL FLOW• Control flow refers to how
program statements are
executed
• It determines the order in
which program instructions
are processed
• Control flow allows us to make
decisions and execute code
conditionally
5.
IF STATEMENT• The if statement is a
fundamental control
structure
• Syntax: `if (condition) {
/*code to execute if
condition is true */ }`
• If the condition
evaluates to `true`, the
block
• It enables conditional
execution based on a
specified condition
6.
IF-ELSE STATEMENT• The else statement describes
what gets executed in case
the condition in the if
statement was false
• Syntax `if (condition) { /* Code
to execute if condition is true
*/ } else { /* Code to execute if
condition is false */ }`
• With if-else there are now two
possible execution paths
based on a condition
7.
ELSE-IF STATEMENT• It’s possible to
declare multiple
execution paths
based on different
conditions using the
else-if statement
• Syntax ``if (condition)
{ /* Code to execute
if condition is true */ }
else if(condition2) { /*
Code to execute if
condition2 is true */ }`
8.
NESTED IF AND IF-ELSE STATEMENTS• Nested structures involve placing one
control structure inside another
• Nested if and if-else statements are
used for more complex decision
making
• Proper indentation is important for
readability
9.
10.
SWITCH STATEMENT• The `switch` statement allows
you to select from multiple
cases based on the value of an
expression
• Provides an efficient way to
handle multiple possible values
switch (expression) {
case value1:
// Code to execute for value1
break;
case value2:
// Code to execute for value2
break;
// ...
default:
// Code to execute if none of the
cases match
break;
}
11.
12.
INDENTATION• Indentation is the practice of adding space (usually via tabs) before
lines of code within a block
• Proper indentation is crucial to make your code clean and readable
• Control flow statements (if, switch, for, while) require careful
organization
• Indentation helps to distinguish between branches and nested
structures
13.
PRACTICAL WORK• Declare a number, receive it from input
• Output whether the number is even or odd
14.
PRACTICAL WORK 2• Declare three numbers, receive them from input
• Find out the largest number and print it to the console
15.
PRACTICAL WORK 3• Declare a number, receive it from input
• This number is the grade of the student, decide and output whether
the student got an A, B, C, D, or E, F
• Let’s imagine that 91-100 = A, 81-90 – B … anything below 51 – F
16.
PRACTICAL WORK 4• Design a simple calculator
• Ask the user for the arithmetic operation they want to perform (-,+,*,/)
• Declare some integer variable to store the user’s choice (1,2,3,4)
• Ask the user to enter 2 numbers
• Perform and output the result of the given operation on given numbers
17.
PRACTICAL WORK 5• Design a program that solves a quadratic equation
• Formula is ax^2 + bx + c = 0
• Input: three numbers (a,b,c)
• Calculate the discriminant (D)
• Depending on the discriminant, you have to calculate the X differently
• Use conditional statements and correctly calculate the value of x
• Write clean instructions for the user, use proper formatting