programming -1 (conditional if... conditional sentences â€¢ conditional sentences are statements...

Post on 27-Mar-2020

7 views

Category:

Documents

Embed Size (px)

TRANSCRIPT

• Programming -1

If…. else and Switch

statements

• Objective

In this chapter you’ll:

• The concept of decision making in programming and logical operations.

• Concept of process control and conditional statements.

• Logical operations coefficients

• Conditional test statement if

• Conditional test statement if… Else

• Conditional test statement if Else if

• Conditional test statement nested if

• Conditional test statement Switch

• Decision making

• Decision making

• If you want to get to college and encounter an intersection ,You should now make a logical decision to answer the following questions

• Will you turn right will reach you to college?

• Will you turn left will reach you to college?

• If you continue to the front , will you reach the college?

• All of these questions can answer yes or no to determine the right path

• in Java and the yes/ no value is true / false (Boolean data type ) to make appropriate decisions.

• Process control

in real life..

❖ Determine the success of a student in a course - Is the total greater than or equal to 60 marks. ( Yes / No)

❖ Speed violation - Is the speed greater than the specified speed

e.g. 120 m / s. (Yes / No)

In Java ..

These controls and conditions need to be checked using comparisons (logical operations) whose results are logical (ie yes, no).

• Logical Operations

Logical operations are those processes that are mainly used in decision- making, e.g. these determine they must be walk in one direction and leave another.

Types of logical operations:

• Simple operations (one condition).

• Combined operations (two conditions or more).

• Simple logical operations

• ==

• !=

• >

• <

• >=

• Complex logical operations • Complex logical operations consist of more than one of a simple comparison process

that is combined using the following operations

UsingOperations

When we need to link the processes to both simple comparisons are

correct (And)

&&

When we need to link the processes at least one

of the two simple operations are correct (OR).

||

When we need to reverse the value of comparisons, it means NOT and is

used with one simple comparison

!

• Truth tables for complex logical operations

• An example of logical operations

Note: We use ( = )to specify a value while we use(==) to compare and return a boolean value

• Boolean datatype

Define

Boolean

variables

Assign

value

Print Boolean

variabels

• Boolean datatype

• Boolean datatype

• if value1 and value2 have the same values the result is true, if not the result is false.

• Compare variables

• Compare variables

Why ??

• Compare variables

we need to compare text values rather than memory locations as they are not basic data types

• Compare variables

Use equals() for compare string variables

• Concept of operations in programming

• The execution of any task in our life consists of a series of sometimes sequential and sometimes parallel processes.

• In the world of programming, there is a method followed by the compiler in the implementation of the code and is to follow the code and the execution of commands in the order set by the programmer.

• Did you notice that the translator outputs the results by walking with the sentences of the program one by one, did not provide one over the other and did not repeat one of them more than times exist, and did not ignore any of them ?!

• This is called the sequential process, but !!

• When a programmer forces a compiler to execute strings and leave strings, or execute sentences more than their appearance, this is known as controlling the flow of operations.

• Conditional sentences • Conditional sentences are statements that help to choice of two or more things.

• E.g. She says if the IT specialty has a good field of work I will specialize in it or I will search for another specialty.

• The selection sentences have more than one pattern, as in the following table:

UsingConditional

sentences

interested in one way on one conditionIf

interested in two different ways on one conditionIf ..else

interested in more than two different ways more than one conditionIf ..else if

interested in more than two overlapping roads with more than one

condition

Nested if

When multiple conditions exist, they only have to do with specific valuesSwitch

• Selection sentences

Consists of three parts:

• 1- Decision making statements (IF, IF .. Else,....)

• 2- Condition (logical operations).

• 3 –statments (Executing orders) as a result of whether or not the condition is done.

Enter value condition

statements with true

True

False

statements with false

• IF statement

• If (condition )

One process;

• If (condition )

{

multi process;

}

How to write:

• The value of condition is Boolean

• IF statement

• IF statement example:

What is the output???

Second if statement

• IF statement

When the condition is related to the execution of more

than one sentence, we put these sentences between

the brackets and this makes the translator executes

them together if the condition meets.

• Common error

• Common error: The condition variable does not contain a value that causes the compiler to show a Syntax error when executed.

• Common error: Placing the semicolon immediately after the condition statement shows a logical error (it appears only through the search and test process after execution), because the translator understands this comma as the condition is unanswered and therefore performs the following sentences without restrictions.

• IF…Else

• If (condition )

One process;

Else

Another process;

• If (condition )

{

multi process;

}

Else

{

another multi process;

}

How to write:

• The value of condition is Boolean

• IF…Else example

• IF…Else example

• IF…Else

• Two if statements can be replaced with if ... else

• if .. else better because it is do only one comparison

• Common error • Common error: Placing the semicolon immediately after the else statement shows a logical

error, because the interpreter understands this comma as an exception and therefore performs the following statements without restriction.

• If the answer in the if else clause contains more than one command, they must be enclosed in the brackets of the beginning and the end, otherwise it will show a programmatic error because it will be considered that the condition is expired and else remains without if.

This statment will be executed in this way anyway though

not intended by the programmer

• IF….Else if

• Repeated depend of the number of conditions

• If ( first-condition )

{

multi process;

}

Elseif ( second-condition )

{

second multi process;

}

Else

{

Third multi process;

}

• IF….Else if

• Note in the if… else if all conditions are contradictory and each condition cannot be met with another condition being met.

• Common error: If you want to check the equality of two numbers, use the = sign or use == with a space between them.

• Common error: The separation of the>< sign and the = sign is considered a error.

• Can the If statements be nested?

• in java the if statement may be inside another if statement

• Can the If statements nested?

• nested if example

• Switch statement

• It depends on the existence of alternatives equal to the value being tested and through which we can choose between alternatives, write as follows:

• Switch statement

• Switch statement use the following resaved word:

• switch determine the variable

• case comparison the value of switch variable

• default it is option execute when no match with case value

• break it is the last statement in each case list, word to control process in the end of switch statement

• break

• Switch statement

day =4

????

day =6

????

• IF and Switch Comparison

1. switch simple than if statement

2. switch

Recommended