Dart program loops & input output function - TechXplore

Friday, 17 February 2023

Dart program loops & input output function

In computer programming, a loop is a control structure that allows a set of instructions to be executed repeatedly based on a specified condition. A loop typically includes a block of code that is executed multiple times, with each execution of the block of code being called an iteration.


For loop: A for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each element in the sequence.

example:

void main(){

for(int i=0;i<5;i++){

    print("hello world");

  }

}

output:

hello world

hello world

hello world

hello world

hello world

While loop: A while loop continues to execute a block of code as long as a specified condition is true. The code in the loop will keep repeating until the condition is no longer true.

example;

void main(){

var a=5;

int i=0;

while(i<=a){

  print('hello world');

  i++;

}

}

output:

hello world

hello world

hello world

hello world

hello world

hello world

Do-while loop: A do-while loop is similar to a while loop, but the block of code is executed at least once before the condition is checked.

example;

void main(){

var a=5;

int i=0;

do{

  print('Hello world');

  i++;

}while(i<=5);

 }

output:

hello world

hello world

hello world

hello world

hello world

hello world

Loops are an essential part of programming and are used in many applications, such as data analysis, simulations, and games. They allow programmers to write code that can repeat tasks efficiently and accurately, saving time and reducing the chance of errors.

INPUT & OUTPUT FUNTIONS:

Input refers to the data or information that a program receives from the outside world, such as from a user, a file. Output, on the other hand, refers to the data or information that a program produces or sends out to the outside world.

examples:

import 'dart:io';

void main(){

print('Enter your name?');

String? name=stdin.readLineSync();

print('hello! $name');

}

output:

Enter your name?

raja

hello! raja

examples:

import 'dart:io';

void main(){

print('Enter your fav number?');

int num=int.parse(stdin.readLineSync()!);

print("This is my fav $num number");

}

output:

Enter your fav number?

3

This is my fav 3 number

There are several ways to perform input and output in programming, depending on the programming language and the specific needs of the program. 








No comments:

Post a Comment