22
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Embed Size (px)

Citation preview

Page 1: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 1Copyright © AdaCore

Your First Ada ProgramPresented by Quentin Ochem

University.adacore.com

Page 2: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 2Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

Page 3: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 3Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

Main subprogram name (can be any Ada identifier)

End of main name (optional)

Page 4: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 4Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

Variables declaration, only before begin

Page 5: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 5Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

Statements, only between begin … end

Page 6: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 6Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

The Ada assignment is := The Ada equality operator is =

Page 7: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 7Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

' introduces a special property, called attribute

Page 8: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 8Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

Value is an attribute transforming a String to a value of a type

Image is an attribute transforming a value of a type to a String

Page 9: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 9Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

with allow to use a library unit, here Ada.Text_IO for textual functions

Page 10: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 10Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

Get_Line reads a line on the command line

Put_Line prints text on the command line

Page 11: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 11Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

& is the concatenation operator, used between String

Page 12: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 12Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B, C : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line); C := A + B;

if C = 0 then Ada.Text_IO.Put_Line ("RESULT IS 0"); elsif C > 0 then Ada.Text_IO.Put_Line ("POSITIVE RESULT :" & Integer'Image (C)); else Ada.Text_IO.Put_Line ("NEGATIVE RESULT :" & Integer'Image (C)); end if;end Hello;

elsif introduces an alternative decision

if … then delimitates a decision, no need for parenthesis

Page 13: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 13Copyright © AdaCore

Quiz

Page 14: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 14Copyright © AdaCore

Identify the Errors

with Ada.Text_IO;

procedure Hello is A, B : Integer;

A = Integer'Image (Ada.Text_IO.Get_Line); B = Integer'Image (Ada.Text_IO.Get_Line);

if A == B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

Page 15: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 15Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;

A = Integer'Image (Ada.Text_IO.Get_Line); B = Integer'Image (Ada.Text_IO.Get_Line);

if A == B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

Page 16: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 16Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;

A = Integer'Image (Ada.Text_IO.Get_Line); B = Integer'Image (Ada.Text_IO.Get_Line);

if A == B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

“begin” is needed to introduce a sequence of statements

Page 17: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 17Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;begin A = Integer'Image (Ada.Text_IO.Get_Line); B = Integer'Image (Ada.Text_IO.Get_Line);

if A == B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

The Ada assignment instruction is :=

Page 18: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 18Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;begin A := Integer'Image (Ada.Text_IO.Get_Line); B := Integer'Image (Ada.Text_IO.Get_Line);

if A == B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

Image converts a number into a string,Value would convert a string to a number

Page 19: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 19Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line);

if A == B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

The Ada equality operator is =

Page 20: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 20Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line);

if A = B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & A); end if;end Hello;

A is not a String, need to be converted through Integer’Image (A)

Page 21: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 21Copyright © AdaCore

with Ada.Text_IO;

procedure Hello is A, B : Integer;begin A := Integer'Value (Ada.Text_IO.Get_Line); B := Integer'Value (Ada.Text_IO.Get_Line);

if A = B then Ada.Text_IO.Put_Line ("A EQUALS B, VALUE IS " & Integer'Image (A)); end if;end Hello;

Page 22: Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com

Slide: 22Copyright © AdaCore

university.adacore.com