Učitati cio broj n i štampati njegovu recipročnu vrijednost. Ako je učitan broj 0, štampati 1/0.

Similar documents
Programiranje III razred

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Uputa: Zabranjeno je koristiti bilo kakva pomagala. Rje²enja pi²ete desno od zadatka. Predajete samo ovaj list.

CONDITIONAL EXECUTION: PART 2

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Uvod u programiranje - vežbe. Kontrola toka izvršavanja programa

modifier returnvaluetype methodname(list of parameters) { // Method body; }

Condi(onals and Loops

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

FLOW CONTROL. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

b) program deljiv3; uses wincrt; var i:integer; begin i:=3; while i<100 do begin write(i:5); i:=i+3; end; end.

Naredbe za kontrolu toka

CSC 172 Intermediate Progamming

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

Intermediate Programming

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Sequential Search (Searching Supplement: 1-2)

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

For. 1) program ispis; {ispisuje brojeve od 1 do 5 jedan ispod drugog} uses wincrt; var s,i:integer; begin for i:=1 to 5do writeln(i); end.

Name CIS 201 Midterm II: Chapters 1-8

MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY

Fundamentals of Programming Data Types & Methods

CS Programming I: Arrays

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Tutorial 8 (Array I)

EECS2030 Fall 2016 Preparation Exercise for Lab Test 2: A Birthday Book

Question 1 [20 points]

Date: Dr. Essam Halim

Arrays. Arrays. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

VEŽBA 5 do while petlja, switch case

/*#include <iostream> // Prvi zadatak sa integralnomg ispita

6 COMPUTER PROGRAMMING

Programming Logic and Design Sixth Edition

St. Edmund Preparatory High School Brooklyn, NY

CompSci 125 Lecture 11

... ; ako je a n parno. ; ako je a n neparno

Module 7: Arrays (Single Dimensional)

Arrays. Eng. Mohammed Abdualal

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

import java.util.scanner; public class Assigment12 { int decnumber; public Assigment12(int number,int base){ if (base == 10) this.decnumber = number;

Arrays. What if you have a 1000 line file? Arrays

dr. sc.. Josip Musić Originalne slideove izradio:

International Journal of Advanced Research in Computer Science and Software Engineering

Guernsey Post 2013/14. Quality of Service Report

Question: Total Points: Score:

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Loops and Expression Types

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types. Lecture2: Java Basics. Wrapper Class. Primitive data types. Bohyung Han CSE, POSTECH

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 2 Discussion

Building Java Programs A Back to Basics Approach 4th Edition Reges TEST BANK Full download at:

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Question: Total Points: Score:

1 Short Answer (15 Points Each)

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

MAP OF OUR REGION. About

Data Structure and Programming Languages

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Question: Total Points: Score:

Apple Inc. US 6,587,904 US 6,618,785 US 6,636,914 US 6,639,918 US 6,718,497 US 6,831,928 US 6,842,805 US 6,865,632 US 6,944,705 US 6,985,981

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items.LoadFromFile('d:\brojevi.

Java Coding 3. Over & over again!

Computational Expression

Marketing Opportunities

Tutorial 03. Exercise 1: CSC111 Computer Programming I

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Question 1 [20 points]

Lesson 7 Part 2 Flags

University of Palestine. Mid Exam Total Grade: 100

MAP OF OUR REGION. About

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Selenium Class 9 - Java Operators

PREDMET. Osnove Java Programiranja. Čas JAVADOC

Full file at

1 Pointer Concepts. 1.1 Pointer Examples

Computer Grade 5. Unit: 1, 2 & 3 Total Periods 38 Lab 10 Months: April and May

CSE 142 Sample Midterm Exam #2

Some Practice Midterm Problems

Full file at

CIS October 19, 2017

Lec 3. Compilers, Debugging, Hello World, and Variables

Transcription:

Kontrolne naredbe Primjeri: Opšti oblik razgranate strukture (if sa ) if (uslov) Naredba 1 ili blok naredbi1 Naredba 2 ili blok naredbi2 Učitati broj x i štampati vrijednost double x, z; Scanner in=new Scanner(System.in); x = in.nextint(); if (x>=0) z = 1; z = 0; System.out.println(x+" "+z); Učitati cio broj n i štampati njegovu recipročnu vrijednost. Ako je učitan broj 0, štampati 1/0.

int n; Scanner in=new Scanner(System.in); n = in.nextint(); if (n==0) System.out.println("1/0"); System.out.println(1.0/n); Učitati x1 i x2. Ako je x1<x2 štampati x1-x2, inače štampati x1+x2. double x1, x2, y = 0.0; Scanner in=new Scanner(System.in); x1 = in.nextdouble(); x2 = in.nextdouble(); if (x1 < x2) y = x1-x2; y = x1 + x2; System.out.println(x1+" "+x2+" "+y); Opšti oblik razgranate strukture (if bez ) if (uslov) Naredba 1 ili blok naredbi1 Učitati broj a. Ako je a pozitivan, štampati poruku Pozitivan.

double a; Scanner in=new Scanner(System.in); a = in.nextdouble(); if (a > 0) System.out.println("Pozitivan"); Opšti oblik višestruko razgranate strukture if (Uslov 1) Blok 1 if (Uslov 2) Blok 2 if (Uslov n) Blok n Blok 0 Učitati broj a i štampati vrijednost izraza

double a, z; Scanner in=new Scanner(System.in); a = in.nextdouble(); if (a >= 7) z = 2; if (4 < a) z = 1; z = 0; System.out.println(z); Naredba switch Opšti oblik naredbe switch switch ( promjenljiva ) case value_1: code_here_1; case value_2: code_here_2;... case value_n: code_here_n; default: code_here_default; Testira se vrijednost promjenljive. Ako je jednaka value_1, izvršava se kod code_here1; ako je jednaka value_2, izvršava se kod code_here2;..., ako je jednaka value_n, izvršava se kod code_here_n. Ako nije jednaka nijednoj od vrijednosti value_1,..., value_n, izvršava se kod code_here_default. Ova naredba se koristi kao zamjena za više if- naredbi. Primjer 1: Na osnovu rednog broja mjeseca, štampati ime mjeseca: int month = 8; String monthstring; switch (month) case 1: monthstring = "January"; case 2: monthstring = "February"; case 3: monthstring = "March"; case 4: monthstring = "April"; case 5: monthstring = "May"; case 6: monthstring = "June"; case 7: monthstring = "July";

case 8: monthstring = "August"; case 9: monthstring = "September"; case 10: monthstring = "October"; case 11: monthstring = "November"; case 12: monthstring = "December"; default: monthstring = "Pogresan mjesec"; System.out.println(monthString); Prethodni primjer je ekvivalentan sa: int month = 8; if (month == 1) System.out.println("January"); if (month == 2) System.out.println("February");... // i tako dalje, sve do decembra Moguće je da kod za više slučajeva (case) bude isti vidi primjer 2. Primjer 2: Na osnovu rednog broja mjeseca u godini, štampati broj dana u mjesecu: int month = 2; int year = 2000; int numdays = 0; switch (month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: numdays = 31; // mjeseci sa 31 danom case 4: case 6: case 9: case 11: numdays = 30; // mjeseci sa 30 dana case 2: // februar if (((year % 4 == 0) &&!(year % 100 == 0)) (year % 400 == 0)) numdays = 29; // prestupna godina numdays = 28; // nije prstupna default: System.out.println("Pogresan mjesec."); System.out.println("Number of Days = " + numdays); Promjenljiva u naredbi switch mora biti cjelobrojna (byte, short, int, long), karakter (char) ili string (tipa String, od verzije Java SE 7) Primjer 3: Upotreba tipa String u naredbi switch: switch (month.tolowercase()) case "january": monthnumber = 1; case "february": monthnumber = 2; case "march": monthnumber = 3; case "april": monthnumber = 4; case "may": monthnumber = 5; case "june": monthnumber = 6; case "july": monthnumber = 7; case "august": monthnumber = 8; case "september": monthnumber = 9; case "october": monthnumber = 10; case "november": monthnumber = 11;

case "december": monthnumber = 12; default: monthnumber = 0;

Ciklusi 1. Naredba while a. Opšti oblik naredbe while: while(logicki uslov) Naredbe1 b. Logički uslov (engl. boolean expression) mora imati vrijednost true ili false. Blok Naredbe1, se izvršava sve dok je logički uslov tačan (true); kada uslov postane netačan (false) izvršava se prva naredba iza naredbe while. c. Obratite pažnju da poslije while nema simbola tačka-zapeta. 2. Naredba do... while a. Opšti oblik naredbe do...while: do Naredbe1 while(logicki uslov); b. Logički uslov (engl. boolean expression) mora imati vrijednost true ili false. Blok Naredbe1, se izvršava sve dok je logički uslov tačan (true); kada uslov postane netačan (false) izvršava se prva naredba iza naredbe while. c. Obratite pažnju da poslije while mora da stoji simbol tačka-zapeta. Učitati prirodan broj n i štampati sve prirodne brojeve od 1 do n. import java.util.scanner; public class Hello1 public static void main(string[] args) Scanner in = new Scanner(System.in); int n = in.nextint(); int i = 1; while (i<=n) System.out.println(i); i = i +1;

Učitati prirodan broj n i štampati sve prirodne brojeve od 1 do n. import java.util.scanner; public class Hello2 public static void main(string[] args) Scanner in = new Scanner(System.in); int n = in.nextint(); int i = 1; do System.out.println(i); i = i +1; while (i<=n);

Učitati prirodne brojeve k i n (k n) i štampati: (a) brojeve k i n i zbir svih prirodnih brojeva od k do n; (b) brojeve k i n i zbir svih neparnih prirodnih brojeva od k do n; (c) brojeve k i n i zbir svih neparnih prirodnih brojeva od k do n. Rješenje (a): import java.util.scanner; public class Hello3 public static void main(string[] args) int k= 10, n = 20, s = 0, i = 1; Scanner in = new Scanner(System.in); k = in.nextint(); n = in.nextint(); for (i=k; i<= n; i = i+1) s = s + i; System.out.println(k + " " + n + " " + s ); Rješenje (b): import java.util.scanner; public class Hello4 public static void main(string[] args) int s = 0, i; Scanner in = new Scanner(System.in); int k = in.nextint(); int n = in.nextint(); i = k; while(i <= n)

if (i%2 == 0) s = s + i; i = i+1; System.out.println(k + " " + n + " " + s ); Rješenje (c): import java.util.scanner; public class Hello5 public static void main(string[] args) int k= 10, n = 20, s = 0, i; Scanner in = new Scanner(System.in); k = in.nextint(); n = in.nextint(); i = k; do if (i%2!= 0) s = s + i; i = i+1; while(i <= n); System.out.println(k + " " + n + " " + s );