Golang Control Statements if if x < 0 { … } else { … } if v,err:=eval(me);v > 0 && err != nil { … }

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Air Force Institute of Technology Electrical and Computer Engineering
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
True or false A variable of type char can hold the value 301. ( F )
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CSC 212 Object-Oriented Programming and Java Part 2.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Learning Javascript From Mr Saem
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Golang Functions Functions allow modularization func MySqrt(f float64) (v float64, ok bool) { if f >= 0 { v,ok = math.Sqrt(f), true } return v, ok //or.
History of Computing – Golang
7 - Programming 7J, K, L, M, N, O – Handling Data.
PHP using MySQL Database for Web Development (part II)
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Discussion 4 eecs 183 Hannah Westra.
ECE Application Programming
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
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.
2. Java language basics (2)
Decisions Chapter 4.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Expressions and Control Flow in JavaScript
Arrays, For loop While loop Do while loop
Starting JavaProgramming
.Net Programming with C#
Control Structures: if Conditional
CS1100 Computational Engineering
Selection CSCE 121 J. Michael Moore.
C# Basics These slides are designed for Game Design Class
Control Structures: for & while Loops
Chapter (3) - Looping Questions.
Chapter 7 Conditional Statements
Flow Control Statements
C Programming Getting started Variables Basic C operators Conditionals
Fundamentals of Functional Programming
EECE.2160 ECE Application Programming
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Fundamental Programming
EECE.2160 ECE Application Programming
Decisions, decisions, decisions
Loops CGS3416 Spring 2019 Lecture 7.
EECE.2160 ECE Application Programming
Problem 1 Given n, calculate 2n
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Golang Control Statements if if x < 0 { … } else { … } if v,err:=eval(me);v > 0 && err != nil { … }

Golang Control Statements if var x bool false if x { … } var s string = ”efg” if s > ”abc” //true If s >= ”fgh” //false

Golang Control Statements if Exercise Create variables to hold todays temperature and age write code to print one of the following if age is below 10 and temperature is below 48.4, “Hot chocolate is served” if age is 18+ and less than 60 and temperature is below 48.4, “Coffee is server” for all others “Hot soup is served” Test your logic for all possible combinations

Golang Control Statements for for i:=0; i<10; i++ { … } for ;; { // or for { … break; } break lets you exit a for loop continue starts the next iteration of the for loop from the top

Golang Control Statements for Exercise Write a for loop to print first 10 positive number or first 10 even positive numbers or first 10 prime numbers

Golang Control Statements for for key,value := range map { … } for index,value := range array { … } for inderOrKey := range X { … } //you can drop value in a map or just iterate a string

Golang Control Statements for Exercise create a table of player vs points from the map exercise using for loop with range print… Player Joe scored 22 points Player Alex scored 48 points Player Gary scored 12 points Player Frank scored 32 points Player Mike scored 24 points

Golang Write a program to reverse and print a string var a string = “gnaloG gninrael ot emocleW” Use for loops with range and multi-variate assignments.

Golang Control Statements switch Switch allows to run a specified block of statements based on a value Golang switch does not need a explicit break var a int = 8 switch a%5 { case 0 : fmt.Println( “a 5 multiple”) case 4 : fmt.Println( “top”) case 1 : fmt.Println( “bottom”) default : fmt.Println( “somewhere”) }

Golang Control Statements switch Switch work on character and strings switch c { case '&': esc = "&" case '\'': esc = "&apos;" case '<': esc = "<" case '>': esc = ">" case '"': esc = """ default: panic("unrecognized escape character") }

Golang Control Statements switch Fallthrough allows switch to execute the next statement var a int = 8 switch a%5 { case 0 : fmt.Println( “a 5 multiple”) case 4 : fmt.Println( “top”) fallthrough case 1 : fmt.Println( “bottom”) default : fmt.Println( “somewhere”) }

Golang Control Statements switch Special use of switch to detect type var a int = 8 Switch a.(type) { case float64 : fmt.Println( “float”) case int : fmt.Println( “integer”) case char : fmt.Println( “char”) default : fmt.Println( “unknown”) }

Golang Builtin functions new make len append panic len(“abc”) a := []string{"a","b"} a = append(a,"c") fmt.Println(len(a),a)

Golang Homework: 1.Sort an array of integer in ascending order 2.Create a table of persons with list of hobbies, print the hobby and the list of persons who has the hobby next to it.