Java Petlje i logika - 1.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

The if-else Statements
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Chapter 4: Control Structures I
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CSI 3125, Preliminaries, page 1 Control Statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Selections Java.
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Compound Condition Break , Continue Switch Statements in Java
Introduction to programming in java
Control Structures.
Petlje WHILE – WEND.
RP3/predavanje08 Ugniježdeni tipovi Iznimke 10/11/2018
Nadgradnja klasa i nasljeđivanje – 3
Programi,Podaci,Varijable,Računanje- Uvod
Programiranje - Blokovi naredbi i logički tipovi –
Naredbe ciklusa.
Petlje FOR - NEXT.
Programi,Podaci,Varijable,Računanje - 2
Java Iznimke - exceptions.
Conditional Statements
Java Klase (Classes).
Arrays and strings -2 (nizovi i znakovni nizovi)
Selection (if-then-else)
Programi,Podaci,Varijable,Računanje - 1
Arrays and strings -1 (nizovi i znakovni nizovi)
Selection CSCE 121 J. Michael Moore.
MessageBox.
The most important decision statement
Visual Basic – Prvi primjer
Do While ... Loop struktura
Flow Control Statements
Control structures Chapter 3.
Naredbe u php-u.
Programiranje - Naredbe za kontrolu toka programa – 1. dio
Programiranje - Naredbe za kontrolu toka programa – 3. dio
Control structures Chapter 3.
Control Structure Chapter 3.
Conditionals.
Program Flow.
PROGRAM FLOWCHART Selection Statements.
Control structures Chapter 3.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Programiranje - Naredbe za kontrolu toka programa – 1. dio
Control Structure.
Arrays Introduction to Arrays Reading for this Lecture:
Vježbenica 2: struktura grananja – 2.dio
Presentation transcript:

Java Petlje i logika - 1

Relacijski operatori Operator Korištenje Vraća true ako je: > op1 > op2 op1 je veći od op2 >= op1 >= op2 op1 je veći ili jednak op2 < op1 < op2 op1 je manji od op2 <= op1 <= op2 op1 je manji ili jednak op2 == op1 == op2 op1 i op2 su jednaki != op1 != op2 op1 i op2 nisu jednaki Java © - Eugen Mudnić

Izvođenje usporedbe Java © - Eugen Mudnić

Relacijski operatori Svaki operator daje vrijednost true ili false Za spremanje rezultata usporedbe koristite varijablu tipa bolean boolean state=false; state= x-y < a+b; Java © - Eugen Mudnić

if-else kontrolna struktura Opća sintaksa: if(uvjetni izraz) statement or compound statement; else //optional if(a>10) c=1; if(a>10) c=0; else c=1; Java © - Eugen Mudnić

Blokovi naredbi (Statement blocks) if(conditional expression) { statement1; statement2; ... statementn; } else statement block statement block Java © - Eugen Mudnić

Ugnježđeni (nested) if-else if (conditional expression) // . . . statement; if (conditional expression) statement; else if (conditional expression) statement; // . . . else statement; Java © - Eugen Mudnić

if-else primjeri analiziraj, prevedi,izvrši Ch3\01_If-Else\NumberCheck.java Ch3\ 02_Nested-If\NumberCheck.java Java © - Eugen Mudnić

Logički (boolean) operatori Korištenje Vraća true ako je: && op1 && op2 op1 i op2 oba true, uvjetno(conditionally) računa op2 (uvjetni(conditional) AND) || op1 || op2 op1 ili op2 su true, uvjetno računa op2 (uvjetni OR) ! ! op op je false (logička negacija) & op1 & op2 op1 i op2 su oba true, uvijek evaluira i op1 i op2 (logički AND) | op1 | op2 op1 ili op2 su true, uvijek evaluira op1 i op2 (logički OR) ^ op1 ^ op2 Ako su op1 i op2 različiti, tj ako je jedan od operanada true a drugi false Java © - Eugen Mudnić

Logički (boolean) operatori if(symbol >= 'A' && symbol <= 'Z') // Is it a capital letter System.out.println("You have the capital letter " + symbol); if(age <16 || age>65) ticketPrice *=0.9; if(!(age >=16 && age<=65)) ticketPrice *=0.9; if(count>0 && total/count >5) // Do something Bez greške ako je count = 0 Java © - Eugen Mudnić

Logički (boolean) operatori U nekim slučajevima drugi operator u izrazu s uvjetnim operatorom neće biti evaluiran. Razmortimo slijedeći kod:: (numChars < LIMIT) && (...) desni operand može imati popratne efekte poput čitanja iz datoteke, ažuriranja neke vrijednosti ili izvođenja izračuna !!! Java © - Eugen Mudnić

Logički (boolean) operatori primjeri: Ch3\03_DecipheringCharacters\01_TheHardWay\LetterCheck.java Ch3\03_DecipheringCharacters\02_TheEasyWay\LetterCheck.java Ch3\03_DecipheringCharacters\03_Trivially\LetterCheck.java Java © - Eugen Mudnić

Uvjetni (ternarni) operator Expr1 ? Expr2 : Expr3 (operator s 3 izraza) primjer: Ch3\04_ConditionalPlurals\ConditionalOp.java Uvjetni operator if (x > y) max = x; else max = y; max = x > y ? x : y ; Java © - Eugen Mudnić

Switch kontrolna struktura switch ( test expression ) { case value1 : statement 1; statement 2; break; case value1 : statement 1; statement 2; break; default: statement; break; } Mora proizvesti rezultat tipa char, byte,short ili int optionalno konstantna vrijednost Izvršava se kad testni izraz nije ni value1, niti value2 Java © - Eugen Mudnić

Switch kontrolna struktura char yesNo = ’N’ ; . . . switch(yesNo) { case ’N’: case ’n’: System.out.println(”No selected ”); break; case ’Y’: case ’y’: System.out.println(”Yes selected ”); break; } Java © - Eugen Mudnić

Doseg varijabli (Variable Scope) Varijable deklarirane unutar metoda nazivamo lokalne varijable lokalne varijable su dostupne jedino unutar metode nisu uvijek dostupne u svim dijelovima metode u kojoj su deklarirane Java © - Eugen Mudnić

Doseg varijabli primjer: Ch3\05_Scoping\Scope.java { int a = 1; // Reference to a is OK here // Reference to b here is an error // Reference to b here is still an error int b = 2; // References to a and b are OK here - b exists now } // Reference to b here is an error - it doesn't exist // Reference to a is still OK primjer: Ch3\05_Scoping\Scope.java Java © - Eugen Mudnić

Kategorije dosega(scope categories) Java © - Eugen Mudnić